My Notes on the Cron directory "/etc/cron.d" on Ubuntu 11.10

February 7, 2012 | 12:37 p.m. CST

The following are some important notes and issues I ran into when using the /etc/cron.d directory for loading cron jobs.

  • If the filename in the /etc/cron.d directory contains a period in the filename, then cron won't load it.

  • You can see if cron is running your task by viewing the syslog.

    $ sudo less /var/log/syslog
  • If for some reason cron isn't running one of the cron jobs located in the directory /etc/cron.d, you might need to restart cron to get your new crons to load (sudo restart cron). Cron should auto reload new files found in /etc/cron.d every minute, but I had a situation where it didn't load my new cron file until I restarted cron.

  • Don't forget to add a newline at the end of your command in your cron file.

  • Don't forget to add the username you want the cron job to run as.

Example Cron Format

The following would run your command as root every five minutes:

0/5 * * * * root /usr/local/bin/mycommand

The following is an example on how to run a Django management command as the deploy user. You might notice that cron is run as root and then uses sudo to run as the deploy user with the argument -i. This is because we need to "simulate initial login" so that the source command works.:

0/5 * * * * root sudo -u deploy -i source /usr/local/virtualenvs/mysite/bin/activate && export DJANGO_SETTINGS_MODULE=config.settings && /usr/local/virtualenvs/mysite/bin/django-admin.py my_management_command

Related tags: cron, Django


Disco Babu
1.   At 5:29 a.m. CST on Dec. 13, 2012, Disco Babu wrote:

You saved me a lot of debugging time. Merci!

2.   At 4:52 a.m. CDT on May 13, 2013, Tank wrote:

Thanks for summarising all the issues you've had, there's precious little documentation on /etc/cron.d/, or if there is, it's not easy to find.

Comments are closed.

Comments have been close for this post.