【发布时间】:2014-03-24 20:25:40
【问题描述】:
我有一个 python webscraping 程序需要在程序终止后连续报废。我的技术如下
crontab -e(设置)
* * * * * /home/ahmed/Desktop/run.sh
运行.sh
TMP_FILE=/tmp/i_am_running
[ -f $TMP_FILE ] && exit
touch $TMP_FILE
/usr/bin/python /home/ahmed/Desktop/python.py
rm $TMP_FILE
bash 代码一定有问题,或者可能是我在 crontab 中的命令错误。程序没有运行。请指导
在标记建议后,我修改了这样的脚本
#!/bin/bash
PATH=$PATH:/bin:/usr/bin
date +'%H:%M:%S Started' >> /home/ahmed/Desktop/log.txt
TMP_FILE=/tmp/i_am_running
[ -f $TMP_FILE ] && exit
touch $TMP_FILE
date +'%H:%M:%S Starting Python' >> /home/ahmed/Desktop/log.txt
/usr/bin/python /home/ahmed/Desktop/python.py
rm $TMP_FILE
date +'%H:%M:%S Ended' >> /home/ahmed/Desktop/log.txt
我使用的 cron 命令是* * * * * /home/ahmed/Desktop/run.sh
创建的日志文件是这样的
15:21:01 Started
15:21:02 Starting Python
15:22:02 Started
15:23:01 Started
15:24:01 Started
15:24:30 Ended
15:25:01 Started
15:25:01 Starting Python
15:26:01 Started
15:27:18 Started
15:28:01 Started
15:29:01 Started
15:30:01 Started
15:31:01 Started
15:31:16 Ended
15:32:01 Started
15:32:01 Starting Python
15:33:01 Started
15:34:01 Started
程序似乎在结束之前重新启动。日志文件应该有启动程序、启动、结束、启动程序、启动、结束等。
有人可以指导我吗?
【问题讨论】:
-
什么没有运行? crontab?剧本本身?提供更多详细信息。
-
在
run.sh中使用logger;出于调试目的,可能以#!/bin/bah -vx开头。 -
错字我的意思是
#!/bin/bash -vx -
如果你想让 Python 脚本在崩溃的情况下仍然运行,那么你可以使用
supervisord之类的东西或创建一个新贵工作