首页 > 解决方案 > Ubuntu crontab 没有一直运行

问题描述

我希望有人可以帮助我,我正在尝试使用 crontab 运行一些自动 python 脚本,并且它可以工作,但由于某种原因它不会按预期运行脚本。如果我长时间不建立 SSH 连接,crontab 中的 python 脚本将不会运行,如果我再次登录,它们将在我注销后运行一段时间然后停止。

30 03 * * * systemctl reboot > /home/ajorge/server/python/logs/crontab_reboot.log 2>&1
45 03 * * * sudo python /home/ajorge/server/python/startup.py > /home/ajorge/server/python/logs/crontab_startup.log 2>&1
*/15 * * * * sudo python /home/ajorge/server/python/backup_auth.py > /home/ajorge/server/python/logs/crontab_auth.log 2>&1
*/15 * * * * sudo python /home/ajorge/server/python/backup_characters.py > /home/ajorge/server/python/logs/crontab_characters.log 2>&1
35 03 * * * sudo python /home/ajorge/server/python/backup_world.py > /home/ajorge/server/python/logs/crontab_world.log 2>&1
*/15 * * * * sudo python /home/ajorge/server/python/backup_clean.py > /home/ajorge/server/python/logs/crontab_clean.log 2>&1

对于下面的示例,脚本假设每 15 分钟运行一次,但我们可以看到它没有运行,并且存在间隙

在此处输入图像描述

如果我与服务器建立 SSH 连接,它将再次开始运行脚本,而无需我做任何事情。

任何人都可以帮助我吗?

标签: ubuntucron

解决方案


将您的 crontab 生成为用户“root”,然后将它们更改为:

30 03 * * * systemctl reboot > /home/ajorge/server/python/logs/crontab_reboot.log 2>&1
45 03 * * * python /home/ajorge/server/python/startup.py > /home/ajorge/server/python/logs/crontab_startup.log 2>&1
*/15 * * * * python /home/ajorge/server/python/backup_auth.py > /home/ajorge/server/python/logs/crontab_auth.log 2>&1
*/15 * * * * python /home/ajorge/server/python/backup_characters.py > /home/ajorge/server/python/logs/crontab_characters.log 2>&1
35 03 * * * python /home/ajorge/server/python/backup_world.py > /home/ajorge/server/python/logs/crontab_world.log 2>&1
*/15 * * * * python /home/ajorge/server/python/backup_clean.py > /home/ajorge/server/python/logs/crontab_clean.log 2>&1

推荐阅读