【问题标题】:running the program after it terminates using cron使用cron终止后运行程序
【发布时间】:2014-05-01 19:37:01
【问题描述】:

我需要在 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))

脚本如下: 运行.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 脚本需要一些更改?如何将程序设置为启动、启动、结束等

【问题讨论】:

  • 您是否要确保 Cron 不会执行 run.sh(如果它仍在运行),还是要确保 run.sh 旨在确保 python.py 不会同时运行多次?
  • 考虑在再次运行脚本之前使用 IF NOT 查询按名称检查 PID,或者在可以接受的情况下将 cron 调用减慢到每三或五分钟一次。
  • 每次python程序结束时都会运行run.sh。老实说,这是别人做的,据他说,他回答如下stackoverflow.com/questions/22554572/…
  • 这已经是正在发生的事情了。该日志可能具有误导性:“已启动”仅表示调用了 run.sh。仅当您看到“正在启动 Python”时才会调用 python 脚本。
  • 好吧。所以你的意思是开始意味着尝试调用python程序但它实际上并没有启动程序而只是调用它。只有当我看到启动 python 时才会调用它。这是你说的吗

标签: python linux bash terminal cron


【解决方案1】:

您可以使用另一个 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

【讨论】:

  • 我可以在一个 run.sh 程序中完成吗?可以帮我修改一下吗?
  • 或者还有其他方法可以完成这项任务吗?在终止程序之前目标没有启动
【解决方案2】:

程序在结束之前重新启动,因为您每天每时每刻都在运行 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分钟就够了。

【讨论】:

  • 明确一点,你的程序是指python程序还是sh脚本?
  • sh 程序,因为您正在锁定检查您创建的文件的 sh 脚本,所以您没有执行 python 但如果我是您,我不会使用这个 cron 示例,因为我告诉过您循环将是更好的方法。
  • 添加并开发了我的答案。有任何疑问请告诉我。
猜你喜欢
  • 1970-01-01
  • 2014-04-28
  • 1970-01-01
  • 2012-08-20
  • 1970-01-01
  • 2018-03-31
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多