HOWTO: Use cron to schedule tasks
This will be the first in an intermittent series of howtos, ranging in difficulty from “beginner” to “high-intermediate.” They will mostly focus on, predictably, things I myself have had trouble with in the past; hopefully some other green n00b will find this and have an easier time of it than I did. Of course, if you’d like to request a howto, drop it in the comments (this also lets me know that I’m not just talking to myself), I’ll do the best I can.
Okay, mark this one as “beginner.” Today we’re going to learn about task scheduling on GNU/Linux systems. Let’s say that you want to open Kontact to the calendar at 7:00 every morning, so it’s open when you sit down with your coffee. We already know that the terminal command to open kontact to the calendar is kontact –module korganizer. (I found that by typing kontact –help) So far so good, how do we get it to run at a scheduled time?
Here’s where it starts to get all elegant and Unix-y. (Yes, I know there are graphical frontends to do this stuff on Gnome and KDE, but frankly, I found them to be much more of a pain in the ass than doing it the “hard” way, so I’m gonna teach you right.)
Thus sayeth Wikipedia: “In computing, cron is a time-based scheduling service in Unix-like computer operating systems. The name is derived from Greek chronos (χρόνος), meaning time… cron is driven by a crontab, a configuration file that specifies shell commands to run periodically on a given schedule.”
Get that? No? Don’t worry, all shall become clear. Here’s how it works. From your shell of choice, type crontab -e. This opens up your user’s crontab file for editing; the first time you open it you’ll have a blank document because you haven’t added anything yet. Each line in this document represents a seperate crontab entry, or “cron job.” The syntax is as follows:
minute hour day_of_month month day_of_week command
Using numerical values for all, Sunday being 1 and Saturday being 7. The * may be used in any field to say “every day” (or every month, or whatever). It’s always best in the crontab file to give it the whole path to the command. If you’re unsure, you can check with whereis -b command. So, we run whereis -b kontact, and it gives us this: “kontact: /usr/bin/kontact”. So continuing with our example, the crontab entry would be:
00 07 * * * /usr/bin/kontact –module korganizer
One last thing before I dismiss the class. If you’re scheduling a graphical application, you need to tell cron to run it on the X display, using export DISPLAY=:0 && before the name of the command. So when you put it all together it looks like this:
00 07 * * * export DISPLAY=:0 && /usr/bin/kontact –module korganizer
That’s it, you have just made your first cron job! Exit the editor, save the file in the location it gives you. When you exit the editor, if everything’s kosher you’ll get a message before your prompt saying “crontab: installing new crontab”. If you want to add another job, just reopen the file with crontab -e, and add another line to the bottom of the file. One job per line, one line per job, always.
If you want to run more than one command at a specified time, you can append with && (which we did above). So, for instance, if you want to open Kontact and Firefox at 7:30 every Monday morning, the line would read:
30 07 * * 2 export DISPLAY=:0 && /usr/bin/kontact && /usr/bin/firefox
I find it helpful to include some hints in the crontab file, by using # to comment out lines. You can do this in any configuration file, btw. So my crontab, using the first example, looks like this:
#min hr date mo wkday command
#remember “export DISPLAY=:0″ if it’s an X program
00 07 * * * export DISPLAY=:0 && /usr/bin/kontact –module korganizer
That’s it! You are now a cron master! Go forth, spread the good news, and godspeed! Questions? Clarifications? Nitpicks? Want to bitch about my writing style? Got a howto request? Let’s hear it!
(A quick note on my syntax: I use bold text to specify that this is a command or line in a file that you should type as-is. I use bold-italicized text to indicate that this is a variable. You should modify this for your particular case, as above where I said whereis -b command. What that means is that you should type “whereis -b” followed by the command you’re looking for. I use blockquoted text to specify that this is a whole file or script, not just one-shot commands, as above where I indented my crontab file.)
January 26, 2008 at 6:36 am
[...] Posted by greggrossmeier on January 25, 2008 Pete, from Guerrilla Tech Support, has written a nice little howto on cron jobs. What are cron jobs? Read his post! [...]