Feb 13, 2009
Using Cron to Automate Tasks on Linux
Note: This was originally posted on my *old* blog. Posting here to add some content while I write a couple of things on my mind.
An exteremely useful feature of most *nix based systems is the ability to use a tool called cron (or crontab) to create what are called “cron jobs”. These are actions that can be made to be executed at a specific time on a specific day.
The two most useful commands for most users are:
crontab -l (list the crontab file for the current user)
and
crontab -e (edit the crontab file for a particular user)
Once you are editing the crontab file (you will generally need to be root to do this) you simply need to follow this format:
min hour day month dayofweek command
So if you wanted to run a command at 3am every morning, then you would do something like this:
0 3 * * * pacman -Syu
This is all well and good, and nothing more that you would get from the crontab man file, but there are a couple of handy tricks I can share. For starters, if you want to run a graphical application you need to send it to a screen. This can be done using a command like:
DISPLAY=:0 deluge
One of the main applications that I have for cron tab isn’t a system administration task at all, it’s to open and close a bittorrent client to download files while I’m asleep. Since most Australian ISPs now offer larger download quotas overnight it’s quite handy to be able to start downloads at 3am and kill them at 9am. It’s easy to do to. You simply use the DISPLAY=:0 trick to start the graphical bittorrent application, and then use killall to make sure that it’s shutdown at the right time:
0 3 * * * DISPLAY=:0 deluge
0 9 * * * killall deluge
I’m sure there are at least a few people out there that will find this helpful.