简体   繁体   中英

Running Python Script from Cron.d

I'm trying to run a Django management command via a crontab installed to /etc/cron.d/mycron on Ubuntu.

I first tested my basic setup by writing the following to /etc/cron.d/mycron:

* * * * * root command echo "Test $(date)" 2>&1 >> /tmp/mycron.log

And I confirmed /tmp/mycron.log was updated once a minute and contains the expected text.

I then tried the following variations using my actual management command:

* * * * * 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

And even though the log file is generated, it contains none of the expected logging text my management command outputs.

After each variation, I run sudo touch /etc/cron.d/ to reload cron. Even if there was an error in my Python code, I'd expect some sort of error message to get logged.

I've checked tail -f /var/log/syslog , and the only errors it shows are for crontabs that don't start with "root", for which it gives me the error:

Error: bad username; while reading /etc/cron.d/mycron

For all the others, I see something like:

Oct  7 10:54:01 localhost CRON[27805]: (root) CMD (/path/to/my/script/manage.py mycommand 2>&1 >> /tmp/mycron.log)

But nothing's written to the log, and the database changes I expect to happen aren't occuring. What am I doing wrong?

The output you need is either in /tmp/mycron.log , or mailed to root . Type mail from under root to check.

Also, why are you trying different variations? The syntax of crontab files is strictly defined ( man crontab -S 5 ), for /etc/crontab it is:

min hour day month weekday user cmdline

for personal crontabs, it is:

min hour day month weekday cmdline

You can't omit user name in /etc/crontab, neither you can use /usr/bin/python in place of user name.

You also don't need the word command , it only works because there is built-in bash function with this name which apparently just runs a command from it's arguments.

The correct line must be either of:

* * * * * root /path/to/my/script/manage.py mycommand 2>&1 >> /tmp/mycron.log
* * * * * root /usr/bin/python /path/to/my/script/manage.py mycommand 2>&1 >> /tmp/mycron.log

In the first case, manage.py must be executable ( chmod +x manage.py ) and it's first line must be #!/usr/bin/python , or whatever your python interpreter path is.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM