Scheduling tasks with cron

  • March 13, 2007
  • Avatar for peter
    Peter
    Upfold

When you want to run a particular task repeatedly, or run it at a scheduled time, Linux and other Unix-like operating systems offer you an easy way to do this and plenty more. It's called cron and it's a multi-purpose task scheduler on steroids. It's installed and enabled on virtually every Unix-like OS out there by default.

In this tutorial, I'm going to walk you through a couple of ways to get whatever you want to run at regular intervals. Quick advanced warning - this tutorial is aimed towards people who are getting proficient using the command line and want to take it further. It's unlikely to be friendly to the new user, so if you're not interested, you might want to skip over this post.

Using the /etc/cron.* folders

This is probably the easiest way to get started running scheduled tasks on your machine. Provided you have administrator (root) access to the machine, you should be able to simply drop scripts into one of these folders:

  • /etc/cron.hourly
  • /etc/cron.daily
  • /etc/cron.weekly
  • /etc/cron.monthly

As the names suggest, placing scripts in these folders schedules them to run hourly, daily, weekly and monthly respectively.

All you need to do is write a script and copy it into the relevant directory to get it scheduled. In most cases, this works fine and is probably the easiest way to do it. (It doesn't have to be a script either - it could be an actual executable file, or a symbolic link or basically anything that your OS will execute).

One minor pitfall that you might encounter is that you'll need to set execute permissions on anything you drop in these folders, otherwise your commands won't get run. For example:

# cp myscript /etc/cron.daily
# chmod a+x /etc/cron.daily/myscript

Using the crontab

Sometimes, however, you'll want more flexibility over exactly when your task runs, or you aren't root on the machine you want to run your task on. In this case, we use the slightly more complex, but overall much more powerful crontab system.

In the same way, either make a script which automates your task, or just remember what commands you'll need.

Head over to your terminal, and type the following:

$ crontab -e

This opens your text editor (whatever you've set it as) and loads in the crontab. If you haven't seen the syntax before, it can be quite tricky, but here's quick explanation of how it works. First, an example of what a crontab entry might look like:

0 23 * * * /home/peter/bin/myscript

  • The first number identifies the minute of the hour you want the command to run. If you want to run it at half past the hour, enter '30', for example. As with all the other fields, enter * to run it every single time (your command will execute every minute).
  • The second number identifies the hour of the day. To have your command execute at 11pm, enter '23' (24 hour clock). Once again, a * stipulates that the command be run every single hour.
  • The third number is exactly the same idea, but for the day of the month. To run your command only on the 27th of the month, enter 27, or * for every day of the month.
  • Fourth up is the month number. You know the deal - enter 1 for January, for example, or * to run it every month.
  • And finally, the last number is for the day of the week. Enter 0-6, where 0 is Sunday, 1 is Monday and so on, or * for every day of the week.
  • The final thing is the command, or location of the script you want to run. Again, check the file has execute permissions.

You simply combine all of these numbers together to make your schedule. It can be a bit confusing at first, but if you think it out, you'll soon quickly understand exactly what numbers need to go where. Still stuck with the syntax? Try this link.

So as you can see, in the example above, I want myscript to execute at 23:00 every single day of the year.

Once you're done, save the file with your editor and quit out of the editor. cron will save your crontab, and next time it rolls around to your schedule, your command should get executed.

Finishing up

You might want to add a logging function to your task for a while, just so you can check it's working properly for the first couple of times, then it should be happy to go and run itself.

While I'm by no means a cron genius, if you do get stuck at any point, why not ask in the comments and I'll try and see if I can help you out!

Avatar for peter Peter Upfold

Home » Articles »