gpt4 book ai didi

python - 使用 cron 终止后运行程序

转载 作者:太空宇宙 更新时间:2023-11-04 04:02:03 25 4
gpt4 key购买 nike

我需要在 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 作业...

* * * * * /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 - 使用 cron 终止后运行程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22609532/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com