使用cron终止后运行程序

时间:2014-03-24 12:34:31

标签: python linux bash terminal crontab

我需要在cron中设置一个程序,该程序设置为每次程序终止时重新启动

为什么我想做这份工作?

该程序实际上是使用网络报废从网站中提取信息,当它到达信息是最新的点时终止 这是python代码的一部分

    sql = """SELECT Short_link FROM Properties WHERE Short_link=%s"""
            rows = cursor.execute(sql,(link_result))
            print rows
            if rows>=1:
                print "Already present"
                sys.exit()
            else:
        query="""INSERT INTO Properties (Published_Date, Title,Price,Bedroom,Agency_Fee, Bathroom, Size,Prop_ref,Furnished_status,Rent_payment,Building_info,Amenities,Trade_name,Licence, RERA_ID,Phone_info,Short_link) values (%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s,%s)"""
        cursor.execute(query,(date_result, title_result, price_result, bedroom_result, agencyfee_result, bathroom_result, size_result, propertyref_result, furnished_result, rent_is_paid_result, building_result, Amenities_result, tradename_result, licencenum_result, reraid_result, phone_result, link_result))

脚本如下: run.sh

#!/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

似乎程序在结束之前重新启动。日志文件应该有启动程序,启动,结束,启动程序,启动,结束等等。

有人可以指导我吗?也许在bash脚本中需要进行一些更改?如何将程序设置为启动,启动,结束等

2 个答案:

答案 0 :(得分:0)

您可以使用另一个shell脚本来运行您自己的run.sh.例如

$ cat runner.sh
while [ 1 ]; do
    ./run.sh
    if [ -e /tmp/stop_run ]; then
       exit
    fi
    sleep 1
done

如果你需要停止这个脚本,只需触摸/ tmp / stop_run而不是杀死run.sh。

此解决方案您不需要cron。只需将runner.sh添加到/etc/rc.local

即可

答案 1 :(得分:0)

程序在结束之前重新启动,因为你每天每小时都在运行cron作业......

* * * * * /home/ahmed/Desktop/run.sh

你需要做的是运行程序的无限循环,直到它完成并在循环结束时再次开始运行。只要你知道任务不会花费超过一分钟,每分钟运行一次程序就可以了。

所以我会这样做:

while(true){

    PATH=$PATH:/bin:/usr/bin

    date +'%H:%M:%S Started' >> /home/ahmed/Desktop/log.txt

    # TMP_FILE=/tmp/i_am_running # Don't need this
    # [ -f $TMP_FILE ] && exit # (Don't need to check if a the script is running)
    # touch $TMP_FILE # (You don't need to create the file)

    date +'%H:%M:%S Starting Python' >> /home/ahmed/Desktop/log.txt
    /usr/bin/python /home/ahmed/Desktop/python.py
    # rm $TMP_FILE # (I commented this as you don't need it)

    date +'%H:%M:%S Ended' >> /home/ahmed/Desktop/log.txt

    # You can add a 'sleep XX' to delay the script between executions in case you don't 
    # want it to be executed one time and another as soon as an execeution finishes.
    # Remember to comment (add '#') or remove the line of cron (crontab -e)

}

你正在做的是每分钟运行一个程序实例,但它们只会在运行所花费的时间后完成:

15:21:01 Started (Start 1st program)
15:21:02 Starting Python (python script is executed, called from bash script)
15:22:02 Started (cron runs 2nd program, not execute python, exit)
15:23:01 Started (cron runs 3rd program, not execute python, exit)
15:24:01 Started (cron runs program, not execute python, exit)
15:24:30 Ended (End 1st program)
15:25:01 Started (cron runs 5th program)
15:25:01 Starting Python (execute 5th program's python script, called from bash script)
15:26:01 Started (etc..)
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

正如我告诉你的那样,在你的情况下,最好的方法是无限循环。但是如果你还是喜欢cron(我个人喜欢,因为无限循环并不总是最好的选择,你总是可以设置cron来每10,15或20分钟执行一次脚本,这是一个更好的选择。

*/10 * * * * /home/ahmed/Desktop/run.sh # This will exceute every 10 minutes
*/15 * * * * /home/ahmed/Desktop/run.sh # This will exceute every 15 minutes
  • 请记住,如果选择cron,则需要取消注释并保留检查文件的行(如果文件存在则表示程序仍在执行)。但是由于整个bash脚本(包括python)需要3-4分钟,因此两个执行不可能相互执行。差异为10分钟就足够了。