我不知道为什么当我使用 cron 运行 Python 脚本时它没有运行。我花了几个小时寻找解决方案

我不知道为什么当我使用 cron 运行 Python 脚本时它没有运行。我花了几个小时寻找解决方案

所以我一直在尝试使用 cron 作业运行我的脚本 NotifyServer.py。这只是一个向 discord 服务器发送消息的简单脚本。我在 rpi3 上运行该脚本,这是我的系统日志文件。我不知道该怎么做,因为我还不太擅长编码。

//www.rsyslog.com"] rsyslogd was HUPed
May  4 06:57:38 raspberrypi systemd[1]: Starting Daily apt upgrade and clean activities...
May  4 06:57:39 raspberrypi systemd[1]: Started Daily apt upgrade and clean activities.
May  4 06:57:39 raspberrypi systemd[1]: apt-daily-upgrade.timer: Adding 29min 40.308844s random time.
May  4 06:57:39 raspberrypi systemd[1]: apt-daily-upgrade.timer: Adding 10min 1.139309s random time.
May  4 07:17:01 raspberrypi CRON[1425]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May  4 07:59:01 raspberrypi CRON[1454]: (pi) CMD (pkill python)
May  4 08:00:01 raspberrypi CRON[1463]: (pi) CMD (usr/bin/python home/pi/InvestorBot/NotifyServerV1.0.py &)
May  4 08:17:01 raspberrypi CRON[1479]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May  4 09:17:01 raspberrypi CRON[1511]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May  4 10:17:01 raspberrypi CRON[1545]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May  4 11:17:01 raspberrypi CRON[1580]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May  4 11:59:01 raspberrypi CRON[1606]: (pi) CMD (pkill python)
May  4 12:00:01 raspberrypi CRON[1615]: (pi) CMD (usr/bin/python home/pi/InvestorBot/NotifyServerV1.0.py &)
May  4 12:17:01 raspberrypi CRON[1631]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May  4 13:17:01 raspberrypi CRON[1664]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May  4 13:27:23 raspberrypi systemd[1]: Starting Daily apt download activities...
May  4 13:27:24 raspberrypi systemd[1]: Started Daily apt download activities.
May  4 13:27:24 raspberrypi systemd[1]: apt-daily.timer: Adding 5h 45min 49.014375s random time.
May  4 13:27:24 raspberrypi systemd[1]: apt-daily.timer: Adding 7h 16min 18.482369s random time.
May  4 13:42:38 raspberrypi systemd[1]: Starting Cleanup of Temporary Directories...
May  4 13:42:38 raspberrypi systemd[1]: Started Cleanup of Temporary Directories.
May  4 14:17:01 raspberrypi CRON[1758]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May  4 15:17:01 raspberrypi CRON[1791]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May  4 15:59:01 raspberrypi CRON[1817]: (pi) CMD (pkill python)
May  4 16:00:01 raspberrypi CRON[1826]: (pi) CMD (usr/bin/python home/pi/InvestorBot/NotifyServerV1.0.py &)
May  4 16:17:01 raspberrypi CRON[1843]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May  4 17:17:01 raspberrypi CRON[1876]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May  4 18:17:01 raspberrypi CRON[1909]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May  4 19:17:01 raspberrypi CRON[1941]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May  4 19:59:02 raspberrypi CRON[1969]: (pi) CMD (pkill python)
May  4 20:00:01 raspberrypi CRON[1979]: (pi) CMD (usr/bin/python home/pi/InvestorBot/NotifyServerV1.0.py &)
May  4 20:17:01 raspberrypi CRON[1994]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May  4 21:17:01 raspberrypi CRON[2027]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May  4 22:17:01 raspberrypi CRON[2059]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May  4 23:17:01 raspberrypi CRON[2093]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May  4 23:59:01 raspberrypi CRON[2120]: (pi) CMD (pkill python)
May  5 00:00:01 raspberrypi CRON[2130]: (pi) CMD (usr/bin/python home/pi/InvestorBot/NotifyServerV1.0.py &)
May  5 00:17:01 raspberrypi CRON[2145]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May  5 01:16:23 raspberrypi systemd[1]: Starting Daily apt download activities...
May  5 01:16:24 raspberrypi systemd[1]: Started Daily apt download activities.
May  5 01:16:24 raspberrypi systemd[1]: apt-daily.timer: Adding 6h 59min 49.631939s random time.
May  5 01:16:24 raspberrypi systemd[1]: apt-daily.timer: Adding 3h 30min 56.222065s random time.
May  5 01:17:01 raspberrypi CRON[2225]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
May  5 02:03:07 raspberrypi systemd[1]: Started Session c14 of user pi.
May  5 02:11:09 raspberrypi crontab[2314]: (pi) BEGIN EDIT (pi)
May  5 02:16:58 raspberrypi crontab[2314]: (pi) END EDIT (pi)
May  5 02:17:01 raspberrypi CRON[2332]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)

我目前crontab -l没有评论:

0 0   * * * usr/bin/python home/pi/InvestorBot/NotifyServerV1.0.py &
59 3  * * * pkill python
0 4   * * * usr/bin/python home/pi/InvestorBot/NotifyServerV1.0.py &
59 7  * * * pkill python
0 8   * * * usr/bin/python home/pi/InvestorBot/NotifyServerV1.0.py &
59 11 * * * pkill python
0 12  * * * usr/bin/python home/pi/InvestorBot/NotifyServerV1.0.py &
59 15 * * * pkill python
0 16  * * * usr/bin/python home/pi/InvestorBot/NotifyServerV1.0.py &
59 19 * * * pkill python
0 20  * * * usr/bin/python home/pi/InvestorBot/NotifyServerV1.0.py &
59 23 * * * pkill python

答案1

这是因为你提供给 crontab 的路径是不完整

在 Linux 中,每个绝对路径都以 开头。您的 crontab 行在每个路径的开头都/缺少。第一行应按以下方式工作:/

0 0 * * * /usr/bin/python /home/pi/InvestorBot/NotifyServerV1.0.py &

另外,你不需要写 6 行代码来每 4 小时运行一次命令。你可以告诉 cron每 4 小时运行一次命令这边走:

0 */4 * * * /usr/bin/python /home/pi/InvestorBot/NotifyServerV1.0.py &

这个关于 cron 的答案或者直接使用以下命令生成 crontab 配置crontab大师

另外,你的第二条命令pkill python可能很危险,因为可能会杀死系统上运行的与您的命令完全无关且系统需要的其他 python 实例。

正如所说在这个答案中,最好指定你的脚本名称:

pkill -1 -f NotifyServerV1.0.py

相关内容