cron

In the last post we talked about scheduling things one at a time, which doesn’t work if we have things we want to repeat. Like I want my computer to make a beep every hour on the hour, I can either sit down and repeatedly run the at command until I’m sick of running the at command, or I can use a different system. That different system is cron.

That’s a weird name for a time keeping system. The story is (and who knows if this is true) it was named after the Greek god of time, Cronus.

Cronus

Cronus

Cron is very flexible. You can run things every hour, every day, every month, every week, every 5 minutes, thirty minutes after the hour, you get the idea. Any kind of regular time interval you want, you can run your program. It’s managed by a file called crontab.

Every user on a Linux system can operate a crontab, there’s no restrictions on that. It has a format you have to follow, otherwise it won’t work. The first five entries in the file are time related.
These  entries are minute, hour, day of month, month, and day of week.  Each of them have values you can put in:

field Allowed Values
minute 0-59
hour 0-23
day of month 1-31
month 1-12 (or names, see below)
day of week 0-7 (0 or 7 is Sun, or use names)

If I want something to run at midnight, then the first field is 0, the second is 0, but what about the third? I can use a ‘*’ to mean a wildcard. This means that on any day of the month, the job will run if the time matches the first two fields. I want it to run at midnight every day, so I put a * in the last two fields. If I want my computer to beep every night at midnight, my crontab would look like:

0 0 * * * /home/computerlamp/beep.sh

That program looks weird, right? Well in order for them to work, you have to give the full path of the program, otherwise the computer doesn’t know where to look.  Just like with at!

We’ll look at more examples of crontab next time.

Leave a Reply

Your email address will not be published. Required fields are marked *