This provides a way to run programs automatically in the future, once or many times.
The entries that are used by 'root' are in the file called '/var/spool/cron/crontabs/root', which can be edited directly, (but see below). Make a copy of the file first, called 'root.def', then, after editing the file, make 'cron' read root's altered cron file by typing in a shell:
/etc/init.d/cron stop; /etc/init.d/cron start
It is preferable for users to run their own cron jobs. To do so, first, type in the user's home directory:
crontab -l > cronfile
The 'l' is an 'elle' to 'list' the jobs already set up. If you leave out the 'elle', all of this user's cron jobs already setup will be deleted, so take care! There may be none, in which case, there will be an error message but an empty file called 'cronfile' will be created in ~loginname. Edit '~loginname/cronfile' and add lines of commands for cron to execute. Read 'man cron' to learn more. The first part of the line contains the minute, hour, day, month, and day of the week when the job is to run. These are separated by 'tabs'. The rest of the line contains the commands. A command can be tested by entering it in a Bourne shell (type 'sh' <return> in any other type of shell, to start the Bourne shell) and seeing if it runs. 'cron' is run by default in the Bourne shell, so you should always test cron jobs using the Bourne shell, not other shells which may give misleading errors messages. Several commands can be entered on one line, with a ';' (semicolon) between them. e.g.:
0 20 * * 1 cp /tmp/aa /tmp/bb; cp /tmp/bb /tmp/aa
will copy 'aa' to 'bb' and back again at 20:00 on Mondays. The '*' means 'all' and hours are '1-24'. Several entries are separated by ',' ('comma') , so '1,2' in the month section means 'January' and 'February'. Use the full path names for files, or use 'cd directory;' at the start of the list of commands, if all the commands are to run in 'directory'.
When you are sure that all the jobs are entered correctly, type 'crontab cronfile'. This will overwrite all the cron jobs that you have already setup, with the jobs listed in 'cronfile'. Then make a copy of 'cronfile' called 'cronfile.def' in case you forget to type 'crontab' with an 'elle', later on. Type 'crontab -l' to list all your jobs. Do not forget that 'elle'!. Finally restart the 'cron' daemon with the command:
/etc/init.d/cron stop; /etc/init.d/cron start
The standard output and standard error of commands in cron jobs, which are not piped or redirected elsewhere, are sent as emails to the owner of the cron job. You can prevent emails being sent by redirecting to '/dev/null' the command's standard output: '1>/dev/null' or the command's standard error: '2>/dev/null' or both: '>/dev/null 2>&1'.
You can run these kinds of jobs just once, at a set time using the 'at' command. See how to do this by typing 'man at'.
Using cron to back up users' files daily
Here is an example of how I use cron. I set up cron so that each weekday evening, at 19:00 a compressed 'tar' file called '/usr/people.tar.gz' is made of all the users' files in '/usr/people'. The files are compressed by about 50%. The program it uses is GNU's 'gtar'. '/usr/people.tar.gz' is then copied at 20:00, using Samba's 'smbtar', to a PC running Windows NT, elsewhere on the network. I use WinZip v 7 service pack 1 on the PC to open 'people.tar.gz' and extract any files that I need to recover. The entry in my '/var/spool/cron/crontabs/root' to create the '/usr/people.tar.gz' file is:
0 19 * * 1,2,3,4,5 '/usr/bin/gtar czf /usr/people.tar.gz /usr/people > /dev/null 2>&1 (one long line)
The entry in '/var/spool/cron/crontabs/myuser' (my login is 'myuser') that copies on Mondays at 20:00 the '/usr/people.tar.gz' file to my PC (called 'mypc', share 'mypc_d', password 'mypassword', user 'myuser') is:
0 20 * * 1 cd /usr; /usr/local/samba/bin/smbclient //mypc/mypc_d mypassword -U myuser -c 'put people.tar.gz ; del people.tar.gz.Mon ; rename people.tar.gz people.tar.gz.Mon' >/dev/null 1>&2' (one long line)
I have four more similar lines in '/var/spool/cron/crontabs/myuser' for the other four weekdays, so that there are five daily backups of the users' files on the PC at any one time. In them, I have replaced the '1' with '2', '3', '4' and '5'. You can make more backup files, if you need to, by making a line that specifies the month and so on. If you want to follow this approach, to save time you can use 'nedit' as 'root', to open your cron files. Make a backup of the original files first! Then cut and past the above lines into the files. Customize the lines to suit your needs.
Share your opinion