linux - 每次程序终止时运行的 cron 中的 Bash 程序

标签 linux bash terminal cron web-scraping

我想用这个作业在 ubuntu 中设置一个 cron 作业

我有一个python网页抓取程序,程序终止后需要不断抓取。也就是说流程是这样的

If program is terminated, set the cron job again (until infinity in cron's method)

something like * * * * * /python.py (but only when the python.py is terminated/finished)

有人可以指导我编写一个 bash 程序来完成这项工作吗?程序为python.py

谢谢

最佳答案

当程序运行时,在某处写入一个临时文件,并确保在程序终止时删除该文件。

然后在程序每次运行时测试文件是否存在。如果存在则退出。

运行.sh

TMP_FILE=/tmp/i_am_running
[ -f $TMP_FILE ] && exit
touch $TMP_FILE
./python.py
rm $TMP_FILE

在您的 crontab 中,调用此 run.sh 而不是 python.py

请记住,如果脚本提前退出(被杀死等)并且 tmp 文件保留在文件系统中,它将不会再次运行 python.py。您也可以采取一些措施来防止或检测此类情况。

关于linux - 每次程序终止时运行的 cron 中的 Bash 程序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22554572/

相关文章:

java - Ubuntu - 为什么我有这么多 Oracle Java 7,我应该使用哪一个?

linux - 在 awk 的 BASH shell 中使用 bc 作为守护进程

bash - 通过TCP套接字桥接标准输入/输出

linux - Shell 脚本 - 从用户输入 4 行

macos - 我如何从 bash 脚本中找出当前的 osx-terminal 主题

macos - OS X 钥匙串(keychain)通过终端的密码助手功能

linux - QT 维护工具在 Linux 上的文件路径位置是什么?

python - iPython:gnome 终端(linux)中的 unicode

c++ - 我的程序在 Windows 机器上崩溃但在 Linux 上运行正常

python - 如果可能的话,如何阻止打印到 stdout 打断输入的内容?