User cron tasks

If you’ve used POSIX-friendly systems like Linux or BSD for a while, you’ll be familiar with the use of the /etc/crontab file, which defines scheduled tasks. You’ll probably be used to editing this file simply by invoking it in your editor.

However, there’s also a crontab binary that’s expressly designed for users with appropriate permissions to run their own scheduled tasks. The files are created, edited, listed, and removed through calls to this binary with the appropriate options, and stored in /var/spool/cron/crontabs.

You can edit the contents of your personal crontab on a machine like so:

$ crontab -e

That will bring up a crontab file in your chosen EDITOR. If you haven’t created one yet, it’ll be empty, possibly with a template for you to edit including explanatory notes in comments.

The syntax is the same as for the system /etc/crontab and /etc/cron.d/* files, except that the field for the username is excluded.

Saving this file in your editor and quitting will instate it as your personal crontab.

You can print the contents of the file thus:

$ crontab -l

And if you no longer need it, it can be erased:

$ crontab -r

I find this is useful for running git pull -q on my dotfiles repository, to automatically keep my configuration files up to date across systems. This one runs at midnight every day:

$ crontab -l
0 0 * * * cd ~/.dotfiles && git pull -q