Under SUSE Linux, there's one main way of scheduling tasks: via the cron daemon. This works on behalf of the user in order to schedule individual tasks, and it is also used by the system in order to run vital system tasks, although a different way of working is used in each case.
For cron to run user-scheduled tasks, it reads a file called crontab. Each user has his or her own version of this, which is stored in the /var/spool/cron/tabs directory. You should use a special command to edit this file in vi (the text editor covered in Chapter 16), rather than edit it directly.
For systemwide tasks, a series of scripts are placed in the /etc/crontab/ directories, which contain subdirectories relating to whether the task is to run each day, week, month, and so on. The average user never needs to bother with systemwide cron jobs. These are handled by the internal system, and programs create their own entries as and when necessary.
The cron daemon starts at bootup and simply sits in the background while you work, checking every minute to see if a task is due. As soon as one comes up, it starts the task going, and then goes back to waiting.
Adding a scheduled task is relatively easily and is done via the shell. Entering the following command will cause your personal crontab file to be loaded into vi, ready for editing:
If this is the first time you've edited your crontab file, it will most likely be completely empty. However, don't be put off. Adding a new entry is relatively easy and normally takes the form of something like this:
01 12 15 * * tar -cjf /home/knthomas/mybackup.tar.bz2 /home/knthomas
Let's examine the line piece by piece. The first part—the numbers and asterisks—refers to when the task should be run. From left to right, the fields refer to the following:
Minutes, from 0 to 59
Hours, in 24-hour time, so from 0 to 23
Day dates, for the day of the month, from 1 to 31 (assuming the month has that many days)
Months, from 1 to 12
Day, for a particular day, either from 0 to 6 (0 is Sunday), or specified as a three-letter abbreviation (mon, tue, wed, and so on)
In the example, the task is set to run at the first minute at the twelfth hour (midday) on the fifteenth day of the month. But what do the asterisks stand for? They're effectively wildcards and tell cron that every possible value applies. Because an asterisk appears in the month field, this task will be run every month. Because an asterisk appears in the day field, the task will be run every day.
You might have noticed a logical contradiction here. How can we specify a day if we also specify a date in the month? Wouldn't this seriously limit the chances of the task ever running? Yes, it would. If you were to specify sat, for example, and put 15 in the date field, the task would run on only the fifteenth of the month if that happened to be a Saturday. This is why the two fields are rarely used in the same crontab entry, and an asterisk appears in one if the other is being used.
After the time and date fields comes the command itself: tar. As you learned in the previous chapter, tar is designed to back up your personal data.
Only standard BASH shell commands can be used in the command section. cron isn't clever enough to interpret symbols such as the tilde (~) as a way of referring to your home direc-tory. For this reason, it's best to be very thorough when defining a cron job and always use absolute paths.
Let's take a look at another example (shown in Figure 33-1):
59 23 * * 0-3 tar -cjf /home/knthomas/mybackup.tar.bz2 /home/knthomas
Figure 33-1. Editing crontab lets you schedule tasks using the vi text editor.
The first field says that this task will run at the fifty-ninth minute of the twenty-third hour (that is, one minute before midnight). The date and month field have asterisks, so this implies that the task should run every day and every month. However, the day field contains 0-3. This says that the task should run on only days 0 through to 3, or Sunday through Wednesday.
You can have as many cron entries as you like; simply give each a separate line. There's no need for them to be in date or time order. You can just add them as and when you see fit.
When you're finished, save the file and quit vi in the usual way (by entering :wq).
SUSE Linux is unusual among Linux distributions in that it doesn't make use of another form of task scheduling called anacron. anacron is designed to periodically schedule tasks, so that, for example, a task could be started every other day, regardless of the date. However, cron can be used to duplicate this functionality.
The SUSE Linux disk includes the vcron program, which provides a simple method of editing your crontab file using a GUI. You can also add at jobs, described in the next section, using the program. You can install vcron by searching for the program using the Install and Remove Software component of YaST2 (see Chapter 29).
Using the vcron program is similar to manually editing crontab. Click the Add button to add a scheduled job according to the time format outlined in the previous section. The only difference is that you can simply click radio buttons rather than typing dates and times manually.