Let's get started. I'll use the user called 'generic.' Make sure to put your own info in when required. Copy and paste the commands. You use crontab to set up your cron jobs. It brings up a text editor and lets you edit your crontab file. It uses vi as its text editor by default. If you are not familiar with vi, you will need to choose a different editor. If you are ok using vi, skip to the section Setting up Cron Jobs. If you want to change your default editor, the first thing to do is determine your shell. The environment variable SHELL contains this information. You access it as $SHELL. generic@froody:~> echo $SHELL /bin/tcsh generic@froody:~> Or it might look like: [generic@froody generic]$ echo $SHELL /usr/local/bin/bash [generic@froody generic]$ The first instance indicates that your shell is tcsh. The second that it is bash. These shells both have their pros and cons. On Rupture, new users get tcsh by default. Some old users have bash. This is only important in that to use cron effectively, you need to set your environment variable EDITOR. You do this a different way in each shell. If you're not using vi, you're probably best off using pico. It is a text editor with a fairly straightforward navigation system (using the arrow keys) and command system (using Ctrl+key). To set the environment variable EDITOR to pico, do: With tcsh: generic@froody:~> echo $EDITOR vi generic@froody:~> setenv EDITOR pico generic@froody:~> echo $EDITOR pico generic@froody:~> With bash: [generic@froody generic]$ echo $EDITOR vi [generic@froody generic]$ export EDITOR=pico [generic@froody generic]$ echo $EDITOR pico [generic@froody generic]$ This sequence of commands shows you your current EDITOR, sets EDITOR to a different value, and then shows you the value of EDITOR again. Now that we have a good EDITOR, we can actually set up our cron jobs. You should at least browse the crontab manual page. You can view it on the web or type: generic@froody:~> man crontab Use the spacebar to page through it. So now you see the -e option. That means 'edit my crontab.' So run: generic@froody:~> crontab -e That puts you into the EDITOR we talked about earlier. If you have never used crontab before, it will give you a blank file. In this blank file, you put specially formatted cron requests. Here are some examples:
# Generic user's cron jobs. He's crazy. PATH="/sbin:/bin:/usr/sbin:/usr/bin:/usr/local/bin:/home/generic/bin" 15 3 * * 1-5 du -k $HOME | sort -rn 0 13 18 8 * echo Happy Birthday | mail billg@microsoft.com */10 * * * 6,7 finger generic 30 3 * * * tmda-pending -q -b -d -O 14d The first line is a comment. Any lines starting with a # character are considered comments and not processed by crontab (or cron). The second line sets up the PATH. By default, cron has a very restricted PATH. That means it can't find things. Doing this makes the whole thing more manageable. The first example calculates the amount of disk space, in Kilobytes, that you're using in your home directory and then sorts it in descending order. $HOME is an environment variable referring to /home/generic (or /home/you, in your case). The command is run at 3:15AM every weekday (days 1-5 of the week). The second one sends an email to billg@microsoft.com with the body 'Happy Birthday' at 1PM on August 18. The third one finger's user: generic every 10 minutes on the weekend (days 6 and 7 of the week). He should be outside playing, not dorking around on Rupture. The fourth example runs at 3:30AM every day and clears from TMDA's pending email queue any messages that are unconfirmed and older than 14 days. It will only work if you have gone through the TMDA documentation. Here is some more formal documentation to skim: crontab Entry Format A crontab file consists of lines of six fields each. The fields are separated by spaces or tabs. The first five are integer patterns that specify the following: minute (0-59), hour (0-23), day of the month (1-31), month of the year (1-12), day of the week (0-6 with 0=Sunday). Each of these patterns may be either an asterisk (meaning all legal values) or a list of elements separated by commas. An element is either a number or two numbers separated by a minus sign (meaning an inclusive range). Note that the specification of days may be made by two fields (day of the month and day of the week). Both are adhered to if specified as a list of elements. See EXAMPLES. The sixth field of a line in a crontab file is a string that is executed by the shell at the specified times. A per- cent character in this field (unless escaped by \) is translated to a NEWLINE character. Only the first line (up to a `%' or end of line) of the com- mand field is executed by the shell. Other lines are made available to the command as standard input. Any line begin- ning with a `#' is a comment and will be ignored. The file should not contain blank lines. The shell is invoked from your $HOME directory with an arg0 of sh. Users who desire to have their .profile executed must explicitly do so in the crontab file. cron supplies a default environment for every shell, defining HOME, LOGNAME, SHELL(=/bin/sh), TZ, and PATH. The default PATH for user cron jobs is /usr/bin; while root cron jobs default to /usr/sbin:/usr/bin. The default PATH can be set in /etc/default/cron; see cron(1M). If you do not redirect the standard output and standard error of your commands, any generated output or errors will be mailed to you. These examples and the documentation above should be enough to get you rolling. Keep in mind that cron will email you the output of the commands. Check your Rupture email for this. If crontab won't let you exit because of syntax errors, compare your format to the examples above and re-try. If things get bad, you can always put a # character at the front of the line. Commented lines are not processed. You can also email the admin with specific questions. |
<admin*rupture.net>
File
/cron/setup.shtml
was last updated :
10:25 PM 11/03/04