我試圖通過在Ubuntu上安裝到/etc/cron.d/mycron的crontab運行Django管理命令。從Cron.d運行Python腳本
我第一次測試我的基本設置通過編寫以下到/etc/cron.d/mycron:
* * * * * root command echo "Test $(date)" 2>&1 >> /tmp/mycron.log
我證實/tmp/mycron.log被更新一次一分鐘,包含預期的文本。
* * * * * root command python /path/to/my/script/manage.py mycommand 2>&1 >> /tmp/mycron.log
* * * * * root command /usr/bin/python /path/to/my/script/manage.py mycommand 2>&1 >> /tmp/mycron.log
* * * * * root command /path/to/my/script/manage.py mycommand 2>&1 >> /tmp/mycron.log
* * * * * python /path/to/my/script/manage.py mycommand 2>&1 >> /tmp/mycron.log
* * * * * /path/to/my/script/manage.py mycommand 2>&1 >> /tmp/mycron.log
* * * * * /usr/bin/python /path/to/my/script/manage.py mycommand 2>&1 >> /tmp/mycron.log
而且即使生成的日誌文件,它包含沒有預期的記錄文本我的管理命令輸出:
然後我用我的實際管理命令嘗試了以下的變化。
每次更改後,我運行sudo touch /etc/cron.d/
以重新加載cron。即使在我的Python代碼中有錯誤,我也希望得到某種錯誤消息來記錄。
我檢查tail -f /var/log/syslog
,它顯示的唯一錯誤是不以「根」開始的crontab中,它給我的錯誤:
Error: bad username; while reading /etc/cron.d/mycron
對於所有其它的,我看到類似的東西:
Oct 7 10:54:01 localhost CRON[27805]: (root) CMD (/path/to/my/script/manage.py mycommand 2>&1 >> /tmp/mycron.log)
但沒有寫入日誌,並且我預期發生的數據庫更改不會發生。我究竟做錯了什麼?
您是否看了我的文章?我說我沒有在syslog或/tmp/mycron.log中看到錯誤。 – Cerin
@Cerin在問題中沒有關於在/tmp/mycron.log裏面尋找的東西。請更新它以更清楚您的問題。 – hamstergene
@Cerin看到我更新的答案。輸出可能被郵寄;運行'mail'來檢查。 – hamstergene