如何让 cron 运行我的 Python 脚本?

如何让 cron 运行我的 Python 脚本?

我写了一个小脚本,询问 Yobit 有关比特币并发性的问题,并在 Ubuntu 17 中发送一些通知。如果我运行命令,它就可以正常工作python coinrates.py。我已将其设为可执行文件并放在#!/usr/bin/python文件顶部,但它不会通过自动运行cron

我试过:

* * * * * ~/test/yobit/checker.py

* * * * * python ~/test/yobit/checker.py

但它不起作用。

这是我的系统日志

Dec  2 12:01:00 plombir-N53SV systemd[1]: Started Run anacron jobs.
Dec  2 12:01:00 plombir-N53SV anacron[1303]: Anacron 2.3 started on 2017-12-02
Dec  2 12:01:00 plombir-N53SV anacron[1303]: Normal exit (0 jobs run)
Dec  2 12:17:01 plombir-N53SV CRON[1888]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Dec  2 13:01:04 plombir-N53SV systemd[1]: Started Run anacron jobs.
Dec  2 13:01:04 plombir-N53SV anacron[3601]: Anacron 2.3 started on 2017-12-02
Dec  2 13:01:04 plombir-N53SV anacron[3601]: Normal exit (0 jobs run)
Dec  2 13:17:01 plombir-N53SV CRON[3997]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Dec  2 14:00:38 plombir-N53SV systemd[1]: Started Run anacron jobs.
Dec  2 14:00:38 plombir-N53SV anacron[5171]: Anacron 2.3 started on 2017-12-02
Dec  2 14:00:38 plombir-N53SV anacron[5171]: Normal exit (0 jobs run)
Dec  2 14:17:01 plombir-N53SV CRON[5846]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Dec  2 15:04:01 plombir-N53SV systemd[1]: Started Run anacron jobs.
Dec  2 15:04:01 plombir-N53SV anacron[7072]: Anacron 2.3 started on 2017-12-02
Dec  2 15:04:01 plombir-N53SV anacron[7072]: Normal exit (0 jobs run)
Dec  2 15:17:01 plombir-N53SV CRON[7374]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Dec  2 16:01:41 plombir-N53SV systemd[1]: Started Run anacron jobs.
Dec  2 16:01:41 plombir-N53SV anacron[8489]: Anacron 2.3 started on 2017-12-02
Dec  2 16:01:41 plombir-N53SV anacron[8489]: Normal exit (0 jobs run)
Dec  2 16:17:01 plombir-N53SV CRON[8881]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Dec  2 17:04:01 plombir-N53SV systemd[1]: Started Run anacron jobs.
Dec  2 17:04:01 plombir-N53SV anacron[10177]: Anacron 2.3 started on 2017-12-02
Dec  2 17:04:01 plombir-N53SV anacron[10177]: Normal exit (0 jobs run)
Dec  2 17:17:01 plombir-N53SV CRON[10489]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Dec  2 18:01:03 plombir-N53SV systemd[1]: Started Run anacron jobs.
Dec  2 18:01:03 plombir-N53SV anacron[11610]: Anacron 2.3 started on 2017-12-02
Dec  2 18:01:03 plombir-N53SV anacron[11610]: Normal exit (0 jobs run)
Dec  2 18:17:01 plombir-N53SV CRON[12238]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Dec  2 19:01:15 plombir-N53SV systemd[1]: Started Run anacron jobs.
Dec  2 19:01:15 plombir-N53SV anacron[13850]: Anacron 2.3 started on 2017-12-02
Dec  2 19:01:15 plombir-N53SV anacron[13850]: Normal exit (0 jobs run)
Dec  2 19:10:57 plombir-N53SV crontab[14200]: (plombir) BEGIN EDIT (plombir)
Dec  2 19:11:19 plombir-N53SV crontab[14200]: (plombir) REPLACE (plombir)
Dec  2 19:11:19 plombir-N53SV crontab[14200]: (plombir) END EDIT (plombir)
Dec  2 19:12:01 plombir-N53SV cron[770]: (plombir) RELOAD (crontabs/plombir)
Dec  2 19:17:01 plombir-N53SV CRON[14450]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Dec  2 19:19:01 plombir-N53SV CRON[14641]: (root) CMD (   test -x /etc/cron.daily/popularity-contest && /etc/cron.daily/popularity-contest --crond)
Dec  2 19:26:45 plombir-N53SV crontab[14932]: (plombir) BEGIN EDIT (plombir)
Dec  2 19:27:00 plombir-N53SV crontab[14932]: (plombir) END EDIT (plombir)
Dec  2 19:27:02 plombir-N53SV crontab[14950]: (plombir) BEGIN EDIT (plombir)
Dec  2 19:28:52 plombir-N53SV crontab[14950]: (plombir) REPLACE (plombir)
Dec  2 19:28:52 plombir-N53SV crontab[14950]: (plombir) END EDIT (plombir)
Dec  2 19:28:59 plombir-N53SV crontab[15043]: (plombir) LIST (plombir)
Dec  2 19:29:01 plombir-N53SV cron[770]: (plombir) RELOAD (crontabs/plombir)
Dec  2 19:45:29 plombir-N53SV crontab[15654]: (plombir) LIST (plombir)
Dec  2 19:56:06 plombir-N53SV crontab[16106]: (plombir) BEGIN EDIT (plombir)
Dec  2 19:56:28 plombir-N53SV crontab[16120]: (plombir) BEGIN EDIT (plombir)
Dec  2 19:56:50 plombir-N53SV crontab[16120]: (plombir) REPLACE (plombir)
Dec  2 19:56:50 plombir-N53SV crontab[16120]: (plombir) END EDIT (plombir)
Dec  2 19:57:01 plombir-N53SV cron[770]: (plombir) RELOAD (crontabs/plombir)
Dec  2 19:59:07 plombir-N53SV crontab[16250]: (plombir) BEGIN EDIT (plombir)
Dec  2 19:59:48 plombir-N53SV crontab[16250]: (plombir) REPLACE (plombir)
Dec  2 19:59:48 plombir-N53SV crontab[16250]: (plombir) END EDIT (plombir)
Dec  2 20:00:01 plombir-N53SV cron[770]: (plombir) RELOAD (crontabs/plombir)
Dec  2 20:00:19 plombir-N53SV crontab[16278]: (plombir) BEGIN EDIT (plombir)
Dec  2 20:04:38 plombir-N53SV systemd[1]: Started Run anacron jobs.
Dec  2 20:04:38 plombir-N53SV anacron[16445]: Anacron 2.3 started on 2017-12-02
Dec  2 20:04:38 plombir-N53SV anacron[16445]: Normal exit (0 jobs run)
Dec  2 20:08:25 plombir-N53SV crontab[16598]: (plombir) LIST (plombir)
Dec  2 20:17:01 plombir-N53SV CRON[16931]: (root) CMD (   cd / && run-parts --report /etc/cron.hourly)
Dec  2 20:28:20 plombir-N53SV crontab[17378]: (plombir) BEGIN EDIT (plombir)
Dec  2 20:29:11 plombir-N53SV crontab[17378]: (plombir) REPLACE (plombir)
Dec  2 20:29:11 plombir-N53SV crontab[17378]: (plombir) END EDIT (plombir)
Dec  2 20:30:01 plombir-N53SV cron[770]: (plombir) RELOAD (crontabs/plombir)
Dec  2 20:32:19 plombir-N53SV crontab[18553]: (plombir) LIST (plombir)
Dec  2 20:37:00 plombir-N53SV crontab[19748]: (plombir) LIST (plombir)
Dec  2 20:37:17 plombir-N53SV crontab[19765]: (plombir) LIST (plombir)

答案1

您需要使用完整路径到你的 python bin 文件。在命令行中输入:

whereis python

您应该会看到类似以下内容的内容:/usr/bin/python以及可能还有其他一些内容。使用该完整路径在你的 cron 文件中,例如:

* * * * * /usr/bin/python ~/test/yobit/checker.py

然后还使用你的主目录的完整路径:

* * * * * /usr/bin/python /home/USERNAME/test/yobit/checker.py

相关内容