The crontab
George Lewis (schvin@bigfoot.com),
portico.org
Okay, a certain moron friend of mine, aires, has requested that someone explain the crontab to him.
Some of you may be familiar with at, a collection of programs that lets you setup programs for programmed running, i.e., to run it at a certain time, or to put various jobs in a queue. The cron daemon (crond) allows you to assign times to various jobs.
There is a "crontab" for each user on your system, the list of programs and when they should be executed. The cron system is really a great thing, because it allows you to have the system do various tasks for you at set times, even if you aren't there or logged in. And like most other things under Linux, other users of your computer can't mess with your settings, unless they happen to have root access (administrative rights) on your computer.
I am running crond v2.2. Here are the basic options:
Now, let's create a situation, say I'm user joe and want to have my system play an MP3, named paranoid_android.mp3 every morning at 6:30 AM.
I'd go to a prompt, and type crontab -e to open up your crontab. Unless you are root, there will probably be nothing there, it will just open up vi with your new crontab file.
If you are not familiar with vi, it is a very powerful text editor that exists
on nearly all unix-based computers out there. It is a good thing to be able to use, if not
know thoroughly. Now that you have a file opened in it, you can press i to
put you into insert mode. Once you are done with a file in vi, press the escape key
and then type :x to exit and save the changes. If you want to discard the
changes, put in :q! instead.
Now, back to editing our crontab. The crontab's format goes like this:
For fields that aren't relevant, such as day of week in this example, we can just put an asterisk (*). I play my MP3s with mpg123, so this is how the line should be entered in the crontab:
30 6 * * * mpg123 /home/joe/paranoid_android.mp3
I put in 30 for minutes, and 6 for hours, and asterisks for the rest, so it will run that command every day of the year at 6:30 AM. Note that the time is in 24-hour format, so if you wanted it to play at 6:00 PM you would put 18 in the hours field. Those are just spaces in between the fields, and several in between the day of week and command. It is just sort of standard to put multiple spaces there, but isn't really necessary as far as I know.
mpg123 is the program I'm using to play my MP3 again, and the file, paranoid_android.mp3 is stored in my home directory /home/joe/ -- so hopefully that makes sense.
Now, let's get fancy, what if you want the MP3 to play at 6:45 to wake you up after you've ignored the first one. You can just add a comma (without a space) and put the other minutes or hours that you want it to play at. For example:
30,45 6 * * * mpg123 /home/joe/paranoid_android.mp3
By default, crond will email you the results of the cronjob, so if you don't want an email every morning reminding you of how the computer had the nerve to wake you up, you can have it port the results to your system log. This is generally stored as /var/log/messages.
To port something to your system log, you would just put the normal command, and then at the end you would add >> /var/log/system. So this is what our finished command would look like:
30,45 6 * * * mpg123 /home/joe/paranoid_android.mp3 >> /var/log/messages
One thing to note about the porting thing, if you put one bracket (>), it will completely overwrite the existing file, whereas, if you put two brackets (>>), it will append the output to the end of the existing file. That is now going to run at 6:30 and 6:45, and play paranoid_android.mp3, and not tell us about it later, but just do it. This should give you the basics of the crontab, it is a very useful device. Besides an alarm clock, I have also use it to automatically synchronize my system's time with the NIST servers, so that every day my system time is accurate within a second or so. These are just basic uses of the cron daemon, but it can be used for just about anything.