shell下定时监测程序的死锁

这几天在处理微博爬虫。由于各种原因,爬虫经常为会出现网络错误断开连接,有时莫名其妙的程序锁死(程序在运行,但其实没有任何输出结果显示在运行)。因此就产生了一个定时检查所运行脚本的脚本,或者就在脚本中定时检查。但又不能将检测程序放入cron中,因为程序kill掉后还要继续向前运行。

处理方法如下:
1. 爬虫脚本将结果输出到某log文件中
2. 若log文件在一定时间内未发生变化,而且爬虫脚本还在运行,那可以认定此时程序锁死。
3. 处理锁死程序,直接kill掉,然后继续时间循环。

脚本运用了pgrep(获取某运行程序的pid)、pkill(杀死某程序)。

#!/bin/bash

#only works in Linux shell
startdate='2016-07-03'
for i in {0..7};do
echo "Running time:"`date`
tododate=`date -d "${startdate} $i days" +%Y-%m-%d`
logfile="weibo-${tododate}.log"
python -u weibo-num.py $tododate >$logfile 2>&1 &
while sleep 120;do
cmd=`pgrep -f "weibo-num.py ${tododate}"`
delta=$((`date +%s`-`date -r $logfile +%s`))
if [ ! -z $cmd ] && [ $delta -ge 900 ];then
pkill -f "weibo-num.py ${tododate}"
break
elif [ -z $cmd ]; then
break
fi
done
done


已发布

分类

来自

标签:

评论

《 “shell下定时监测程序的死锁” 》 有 13 条评论

  1. order androxal uk order

    purchase androxal cost usa

  2. canadian enclomiphene with no prescription

    buy enclomiphene online cheapest

  3. buy cheap rifaximin generic united states

    buy rifaximin australia discount

  4. how to buy xifaxan generic health

    discount xifaxan uk buy over counter

  5. discount staxyn generic ireland

    buy cheap staxyn usa suppliers

  6. how to buy avodart cheap in canada

    buying avodart generic drug india

  7. discount dutasteride generic pharmacy canada

    purchase dutasteride cheap no prescription

  8. cheapest buy flexeril cyclobenzaprine buy san francisco

    how to buy flexeril cyclobenzaprine buy virginia

  9. how to order gabapentin buy dublin

    how to order gabapentin generic free shipping

  10. buy cheap fildena generic online cheapest

    order fildena canada fast shipping

  11. cena kamagra 100 mg ト催ュslo 30

    kamagra dodání následující den

  12. générique kamagra en afrique du sud

    indian pharma kamagra

  13. cheapest buy itraconazole uk how to get

    discount itraconazole on line

回复 cheap rifaximin cheap uk 取消回复

您的邮箱地址不会被公开。 必填项已用 * 标注