HowTo: Use Cron Tables (Crontab) to Schedule Events in Suse Linux (openSUSE) 10.x, 11.x

Introduction: Cron is the Unix/Linux event scheduler. It's great for automating a multitude of administrative tasks like backing up your data. See Wikipedia: Crontab. When I examined the man pages I found that it is relatively straightforward but has complicated options and rules that take quite some unraveling to appreciate properly. And they're so hard for me to remember. So I thought I would put a reference page here, for myself and others.

Background: Cron is the daemon to execute scheduled commands. It's actions are determined by a Cron Table or "crontab". In Suse there's an array of crontabs associated with regular administrative tasks for system operations. We leave those strictly alone. The personal crontabs for the root user and for normal users are text files, located in the folder /var/spool/cron/tabs and we DO NOT edit those directly. They contain system variables along with a line entry for each task that the relevant user has scheduled.

Who Can Access Cron Tables: There are two files controlling who uses cron tables. These are cron.allow and cron.deny, located in directory /etc. See the man pages. Access in Suse is given by default to all Linux users and to root.

Note than if you want to set up a cron job as root, you do not invoke sudo; that confuses the application. Rather you become the root user, either by logging on as root (cumbersome), or opening a root terminal, or opening a terminal as an ordinary user and entering "su" to become root.

A Look at a Cron Table Entry: Table entries are comprised of single lines with two pieces of information; the first piece is the timing string and the second is the command sequence.

I'll just make one up so I can illustrate. This one copies all files from one directory to another, periodically:

0 20 * * * cp /home/myname/source/* /home/myname/destination

The timing string is "0 20 * * *" and this particular string means "every day at 2000 hours". The command sequence is "cp /home/myname/source/* /home/myname/destination" and it means "copy all files from the directory called source to the directory called destination".

That's a somewhat trivial example but it shows how we might approach backing up data when away from the workstation.

The Timing String: This is fairly easy. At its simplest it is five integers, in this order: minutes, hours, days of the month, months and days of the week:

Minutes: 0 -> 59

Hours: 0 -> 23

Days of month:1 -> 31

Months: 1 -> 12 or jan -> dec

Days of week: 0 -> 7 or sun -> sun

The man pages detail a very large range of timing options. I've copied them on this link. Here's my interpretation of the man pages - but don't rely on my interpretation - I very well might be wrong - work it out for yourself if it's important:

Straightforward numbers:
32 18 17 11 *
This means at 32 minutes past 1800 hours on the 17th of November (for days falling between Sunday and Sunday inclusive; i.e. regardless of the day of the week) each year
Range/s:
32 18-20 17 11 *
This means at 6.32 p.m., 7.32 p.m. and 8.32 p.m. on the 17th of November (regardless of the day of the week) each year
Stepped Range:
32 10-23/3 12,18 11 *
This means at 1032, 1332, 1632, 1932 and 2232 on the 12th and 18th of November (regardless of the day of the week) each year
Lists:
32 18 17,21,29 11 *
This means at 6.32 p.m. on the 17th, 21st and 29th of November each year (regardless of the day of the week)
Names:
32 18 * 11 mon,wed
This means at 6.32 p.m. on each Monday and Wednesday in November
More Names:
32 18 * nov 1,3
This also means at 6.32 p.m. on each Monday and Wednesday in November
Dual specs for days:
32 18 17,21,29 11 mon,wed
This means at 6.32 p.m. on the 17th, 21st and 29th of November (plus each Monday and Wednesday in November) each year

The Command Sequence is the second part of the crontab entry.

One Command: You can use a simple command like the file-copy command we looked at above. Here's another example. Let's set an alarm clock. Suppose you have a nice music file located at this address: /home/myname/Music/"When She's Gone.mp3". You can play this file on the linux music player "mpg123" using this command line instruction:

myname@hostname:~> mpg123 /home/myname/Music/"When She's Gone.mp3"

So lets suppose you want to create an early morning wake up alarm at 6.15 a.m. Well here's a crontab entry to do just that with "When She's Gone":

15 6 * * * mpg123 /home/myname/Music/"When She's Gone.mp3"

Multiple Commands: You will often need a series of commands to carry out administrative functions. These are stored in a simple text file called a script file. Here's a scenario so I can explain how it all works: Imagine that I'm writing a book using the openoffice writer and a wordprocessor file at /home/myname/Documents/book/mybook.odt. When I back it up I copy the book file to a folder "bookbackups" on a separate hard drive (/mnt/hdb2/bookbackups). I want the backups to be stored in folders named for the date of the backup. And archived as compressed files so I have a complete, historically sequenced backup of all my hard won progress.

Here then are the steps that could be used to perform this task in real time:

  1. Store the date as a six-digit string in the variable xx:
    xx=`date +%y%m%d`
  2. Make a directory on a different hard drive to archive the data, named as today's date ($xx):
    mkdir /mnt/hdb2/bookbackups/$xx
  3. Make an archive copy of the contents of the "book" directory:
    tar -cf mybooks.tar /home/myname/Documents/book
  4. Move the archive copy over to the date-coded folder on the second hard drive:
    mv mybooks.tar /mnt/hdb2/bookbackups/$xx

If you wanted to schedule these operations for late at night, when you're not around, just pop the commands in a script file, which is simply a text file containing the separate commands on different lines, with an appropriate name; e.g. backup.scr:

xx=`date +%y%m%d`
mkdir /mnt/hdb2/bookbackups/$xx
tar -cf mybooks.tar /home/myname/Documents/book
mv mybooks.tar /mnt/hdb2/bookbackups/$xx

Once you have made the file backup.scr executable with the chmod command, it becomes a script that you can execute from the command line. Here's chmod to make it executable:

myname@hostname:~> chmod 755 /home/myname/Documents/book/backup.scr

Suppose this operation is scheduled for 1 a.m. every night. The timing string is "0 1 * * *". So if we put the timing string together with the command represented by a pathway to the script file, the crontab entry becomes like this:

0 1 * * * /home/myname/Documents/book/backup.scr

Editing the Cron Table: All we need to finish is to insert the crontab entry, the line shown above, into the crontab file located in the folder /var/spool/cron/tabs. There's a text file there for each user, including root. They must not be edited directly because they contain system code in addition to the crontab lines. Just enter this command in a terminal to open the special crontab editor:

crontab -e

The editor will open the crontab files and you can add the line shown above and change any other lines (i.e. tasks) as you see fit. Note that the task lines are entered on separate lines, one for each task -and- the final line is a blank line.

Now every morning at 1 a.m. a new directory named for the date, e.g. /mnt/hdb2/bookbackups/070628, will be constructed and a tar file of the working directory will be placed in the folder named e.g. 070628.

Choosing your editor: Suse's default editor is Vim. You can change this either just for yourself or globally, for everyone (including root).

Default editor just for yourself: There's a hidden file called .bashrc in directory /home/myname/ where you may configure your favourite editor for adjusting crontab entries. Open the file .bashrc you'll find that the place to configure the default editor is obvious. My favourite editor is kwrite. Other popular editors are gedit, kate vim and so on. You enter a line where indicated in .bashrc like one of these to set your default editor:

For Gedit:
export EDITOR=/opt/gnome/bin/gedit
For Kwrite:
export EDITOR=/opt/kde3/bin/kwrite
For Kate:
export EDITOR=/opt/kde3/bin/kate

Default editor globally: If you want to change everone's default editor you change this file: /etc/profile.local. If profile.local doesn't exist then you create it in directory /etc. Just add the appropiate line illustrated above to "export" your favourite editor for everyone, including root.

Aministration: Viewing and Purging Cron Jobs: Some minor console commands to manipulate a crontab are illustrated in the following console sessions:

If you want to view your current crontab, enter "crontab -l".

myname@hostname:~> crontab -l
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/crontab.XXXXKlGTk8 installed on Sat Jun 23 21:07:58 2007)
# (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)
0 20 * * * cp /home/myname/source/* /home/myname/destination
myname@hostname:~>

Those are the contents of the crontab for user named "myname". The last line, commencing "0 20 * *...." is the actual task instruction; the rest is commented system information. Don't try to understand the contents. I'm just setting the ambience here.

If you want to display the crontab for another user named "othername", you first su to root and then issue this command "crontab -u othername -l". You wouldn't use this much:

myname@hostname:~> su
Password:
hostname:/home/myname # crontab -u othername -l
# DO NOT EDIT THIS FILE - edit the master and reinstall.
# (/tmp/kde-root/kcron6EA0na.tmp installed on Sat Jun 23 19:14:56 2007)
# (Cron version V5.0 -- $Id: crontab.c,v 1.12 2004/01/23 18:56:42 vixie Exp $)
hostname:/home/myname #

To remove all your cron tasks you issue this command: "crontab -r"

myname@hostname:~> crontab -r
myname@hostname:~>

Errors: are usually recorded in mail messages sent by default to the user and lodged at /var/spool/mail/username.

Take it easy.
Swerdna: 24 June 2007