Last post we talked about cron and the basic format of the crontab file. This time we’re going to talk about more advanced examples of crontab. Remember, the fields are:
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) |
I want my computer to beep on Sunday afternoon at 3:15 pm. The first field is 15 and the second field is 15. It isn’t 3, because that would be 3:15AM. I don’t know about you, but I’m asleep then. crontab uses military time where you add 12 to any time after noon. So 6pm becomes 18 for the crontab. I don’t care about the actual day of the month or the month, I want it every Sunday. So the third and fourth entries are * for the wildcard. For the last entry I can use either 0, 7, or Sun. I like Sun because it’s more descriptive. My crontab looks like:
15 15 * * Sun /home/computerlamp/beep.sh
I’ve decided I want my computer to beep on January 1 at midnight. It’s a way to celebrate the New Year! Happy New Year *beep*. The first two entries are 0, because I want 12:00AM, which is 0 minutes and 0 hours. The third entry is 1, for the first day of the month and the fourth is 1 or Jan for the first month of the year. I don’t really care what day of the week the first day of the year is, so I can put a wildcard for the fifth column. That makes my crontab look like:
0 0 1 1 * /home/computerlamp/beep.sh
I could also make it look like:
0 0 1 Jan * /home/computerlamp/beep.sh
We’ve talked about the format of the crontab but I haven’t said anything about how to edit it. That’s a more complex problem and we’ll talk about it in the next post.