0

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?

1 Answer 1

3

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.

Sign up to request clarification or add additional context in comments.

3 Comments

Did you read my post? I said I'm not seeing errors in either syslog or /tmp/mycron.log.
@Cerin there is nothing about looking inside /tmp/mycron.log in the question. Please update it to make it more clear what is your problem.
@Cerin see my updated answer. It is possible the output is mailed; run mail to check.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.