Wednesday, 18 August 2010
Administrator
Setting up cron jobs in Unix and Solaris
cron is a unix, solaris utility that allows tasks to be automatically run in the background at regular intervals by the cron daemon. These tasks are often termed as cron jobs in unix , solaris. Crontab (CRON TABle) is a file which contains the schedule of cron entries to be run and at specified times.
This document covers following aspects of Unix cron jobs
1. Crontab Restrictions
2. Crontab Commands
3. Crontab file – syntax
4. Crontab Example
5. Crontab Environment
6. Disable Email
7. Generate log file for crontab activity
1. Crontab Restrictions
You can execute crontab if your name appears in the file /usr/lib/cron/cron.allow. If that file does not exist, you can use
crontab if your name does not appear in the file /usr/lib/cron/cron.deny.
If only cron.deny exists and is empty, all users can use crontab. If neither file exists, only the root user can use crontab. The allow/deny files consist of one user name per line.
2. Crontab Commands
export EDITOR=vi ;to specify a editor to open crontab file.
crontab -e Edit your crontab file, or create one if it doesn’t already exist.
crontab -l Display your crontab file.
crontab -r Remove your crontab file.
crontab -v Display the last time you edited your crontab file. (This option is only available on a few systems.)
3. Crontab file
Crontab syntax :
A crontab file has five fields for specifying day , date and time followed by the command to be run at that interval.
* * * * * command to be executed
- - - - -
| | | | |
| | | | +----- day of week (0 - 6) (Sunday=0)
| | | +------- month (1 - 12)
| | +--------- day of month (1 - 31)
| +----------- hour (0 - 23)
+------------- min (0 - 59)
|
* in the value field above means all legal values as in braces for that column.
The value column can have a * or a list of elements separated by commas. An element is either a number in the ranges shown above or two numbers in the range separated by a hyphen (meaning an inclusive range).
Notes
A. ) Repeat pattern like /2 for every 2 minutes or /10 for every 10 minutes is not supported by all operating systems. If you try to use it and crontab complains it is probably not supported.
B.) The specification of days can be made in two fields: month day and weekday. If both are specified in an entry, they are cumulative meaning both of the entries will get executed .
4. Crontab Example
A line in crontab file like below removes the tmp files from /home/someuser/tmp each day at 6:30 PM.
30 18 * * * rm /home/someuser/tmp/*
Changing the parameter values as below will cause this command to run at different time schedule below :
| min |
hour |
day/month |
month |
day/week |
Execution time |
| 30 |
0 |
1 |
1,6,12 |
* |
– 00:30 Hrs on 1st of Jan, June & Dec. |
| |
| 0 |
20 |
* |
10 |
1-5 |
–8.00 PM every weekday (Mon-Fri) only in Oct. |
| |
| 0 |
0 |
1,10,15 |
* |
* |
– midnight on 1st ,10th & 15th of month |
| |
| 5,10 |
0 |
10 |
* |
1 |
– At 12.05,12.10 every Monday & on 10th of every month |
| : |
Note : If you inadvertently enter the crontab command with no argument(s), do not attempt to get out with Control-d. This removes all entries in your crontab file. Instead, exit with Control-c.
5. Crontab Environment
cron invokes the command from the user’s HOME directory with the shell, (/usr/bin/sh).
cron supplies a default environment for every shell, defining:
HOME=user’s-home-directory
LOGNAME=user’s-login-id
PATH=/usr/bin:/usr/sbin:.
SHELL=/usr/bin/sh
Users who desire to have their .profile executed must explicitly do so in the crontab entry or in a script called by the entry.
6. Disable Email
By default cron jobs sends a email to the user account executing the cronjob. If this is not needed put the following command At the end of the cron job line .
>/dev/null 2>&1
7. Generate log file
To collect the cron execution execution log in a file :
30 18 * * * rm /home/someuser/tmp/* > /home/someuser/cronlogs/clean_tmp_dir.log
Saturday, 28 August 2010
Administrator
Crontab Examples
To use this command on cron job line using 'crontab -e', I need to fill in all of the fields:
0 12 * * * /usr/bin/top -n 1 -b -S
This command will execute every day at noon. The first five fields can be values, lists, ranges, or ranges with step values.
The first and second fields in the example are values. They are just simple integers that exist in the range of the field. The '0' in the first field could be any integer between '0' and '59' (minutes of the hour). The '12' in the second field could be any integer between '0' and '23' (hours of the day).
A list is more than one value separated by commas (with no spaces). An example using lists would be:
0 0,6,12,18 * * * /usr/bin/top -n 1 -b -S
which adds midnight (0), 6 am (6), and 6 pm (18) to the times this job will execute, in addition to the original time of noon (12).
A range is just a lower number (in the range of possible values) and a higher number (also in the range of possible values) separated by a dash ('-'). A cron job using a range might be:
0 0,6,12,18 * * 1-5 /usr/bin/top -n 1 -b -S
making the 'top' cron job execute four times a day, but only on weekdays (Monday through Friday). A list may also include a range in place of one (or more) of its values, like this:
0 0,6,9-15,18 * * 1-5 /usr/bin/top -n 1 -b -S
which would allow this cron job to execute every hour from 9 am (9) until 3 pm (15), instead of just at noon (12).
The asterisk (*) fields in the example above are ranges, consisting of every value in the range from the first possible value through the last possible value, which are different for each field. In the original example, the third fields asterisk means '1-31' (days of the month), the fourth fields asterisk means '1-12' (months of the year), and the fifth fields asterisk means '0-7' (days of the week, Sunday being both values zero (0) and seven(7)).
Ranges with step values express lists like this:
0 0,6,12,18 * * * /usr/bin/top -n 1 -b -S
in this abbreiviated way:
0 */6 * * * /usr/bin/top -n 1 -b -S
meaning all possible values ('*' or '0-23') that are evenly divisible by 6 (0, 6, 12, and 18). This way of expressing lists makes it much easier to run a cron job every 5 minutes:
*/5 * * * * /usr/bin/top -n 1 -b -S
or every other day:
0 12 */2 * * /usr/bin/top -n 1 -b -S
in a way that is short, easy to read, and interpret.
Cron Job Output
Sometimes I never want to receive the output of a command I put in my crontab. There are a couple of way to do this. The "all or nothing" way is to set MAILTO="" on the first line of my crontab, but then I will get no output (that I don't arrange to get somehow via the command) for any of my cron jobs. I typically do not do this.
I regularly use output redirection to funnel my command output to a file (if I want a more permanent record of the cron job than an email), or to the 'electronic wastebin' (or 'bitbucket') which is /dev/null (if I want no record of the cron job). If I want to ping a list of hosts every 10 minutes, I can write a script called 'pinghosts.sh' that provides nicely formatted output from the 'ping' command. The script could automatically put it's output to a file, but if I want to use 'pinghosts.sh' from the command line too, that might be a problem. Instead, I could do this:
*/10 * * * * /home/myuser/bin/pinghosts.sh > /home/myuser/pinghosts.log
and every ten minutes the output of 'pinghosts.sh' will replace the contents of the file 'pinghosts.log' ( '>' causes the output of the 'pinghosts.sh' command to overwrite the contents of the 'pinghosts.log' file) in my home directory (if 'myuser' is my username). If I want 'pinghosts.log' to be more like a regular log file, tracking output over time, I need to use a different redirection symbol:
*/10 * * * * /home/myuser/bin/pinghosts.sh >> /home/myuser/pinghosts.log
( '>>' cause the output of the 'pinghosts.sh' command to be appended to the 'pinghosts.log' file). If I plan to keep 'pinghosts.sh' output over time, I should make sure it doesn't produce more output that I have disk (or quota) space. I could just throw away the file when it gets to large, or maybe just every month or so (hmm...sounds like a job for cron).
If 'pinghosts.sh' produces any error messages, 'pinghosts.log' will not contain them. Instead, an email message containing the error messages will be sent to my user. If that is what I want, I am set, but many times I want error messages to be recorded in my logs of output, so the above examples will not do. To put all output into the file I need to do this[1]:
*/10 * * * * /home/myuser/bin/pinghosts.sh >> /home/myuser/pinghosts.log 2>&1
This works for shells like 'bash' (sh, ksh, zsh) and redirects STDERR (error output) to STDOUT (normal output), just after STDOUT has been redirected to the file 'pinghosts.log'.
Sometimes I just want a cron job script to do what I want and never tell me about it. If I have a script that notifies me by email of "garbage day" every week, I don't want another email telling me it sent the first email successfully. To make this happen, instead of a log file I use /dev/null as the target of my output:
0 19 * * 2 /home/myuser/bin/garbage-day-email.sh > /dev/null 2>&1
but this will never alert me if the email is unsuccessful, so I may opt for this version:
0 19 * * 2 /home/myuser/bin/garbage-day-email.sh > /dev/null
instead. This way, at 7 pm on Tuesday evenings I will get an email cheerfully reminding me of "garbage day", or an uglier error message that does the same (if email is working).
Cron Job Scripts
Sometimes what I want to do from a cron job is a whole lot more complicated that a single command. When that kind of situation occurs, it is time to make a script (usually a shell script) to run from my crontab.
A simple backup of my home directory could be one command [2]:
0 3 * * * /bin/tar czvf /backup/myuser-`/bin/date +%Y%m%d`.tgz /home/myuser
which executes a backup of my home directory every day at 3 am (and uses a date code on the archive filename), but I may want to do some more work in the cron job to report more information and set some file attributes after the backup occurs. So, I would write a script to do these tasks:
#!/bin/bash
ARCHIVE=/backup/myuser-`/bin/date +%Y%m%d`.tgz
/bin/tar czvf $ARCHIVE /home/myuser
/bin/chown myuser:mygroup $ARCHIVE
/bin/chmod 640 $ARCHIVE
/bin/echo " "
/bin/echo -n "Disk Space Used: "
/bin/ls -sh $ARCHIVE
/bin/echo "File Details:"
/bin/ls -l $ARCHIVE
I could name it backup-myuser.sh and call it from my crontab:
0 3 * * * /home/myuser/bin/backup-myuser.sh
and get my backups, control file ownership and permissions, and report on the resulting archive file. In addition to that, I can change the details of "what gets done" by editting a script and not messing around with my crontab unless I want to change the script name or when it runs.
In my opinion there is something wrong with this backup script. It uses only one shell variable ($ARCHIVE) to keep from refiguring the archive file name every time it is used, but it uses no others. When I am writing a cron script, I like to overuse shell variables. I like to put every executable path, every file path, and any text used in more than one location into shell variables. This can make the script file larger and it can make it smaller, depending on a few things, but I do not do it for saving some space in the script file. I do it for portabability, maintainability, and (if I choose representative [3] variable names) readability reasons. If I copy or move the script where files are in different locations, for whatever reason, I can change the script quickly and easily and adapt it to it's new environment.
An rewrite of the above backup example that follows the 'overuse shell variables' philosophy is here:
#!/bin/bash
# backup-myuser.sh
# file paths
TAR=/bin/tar
CHOWN=/bin/chown
CHMOD=/bin/chmod
ECHO=/bin/echo
LS=/bin/ls
DATE=/bin/date
# script variables
DATECODE=`$DATE +%Y%m%d`
ARCHIVE=/backup/myuser-$DATECODE.tgz
BACKDIR=/home/myuser
BUSER=myuser
BGROUP=mygroup
BMODE=640
# do the work
$TAR czvf $ARCHIVE $BACKDIR
$CHOWN $BUSER:$BGROUP $ARCHIVE
$CHMOD $BMODE $ARCHIVE
# report the results
$ECHO " "
$ECHO -n "Disk Space Used: "
$LS -sh $ARCHIVE
$ECHO "File Details:"
$LS -l $ARCHIVE
This example includes a few other "good things" (in my opinion) like whitespace (the blank lines separating different parts of the script), comments (the lines with pound signs (#) followed by explanitory text), and some single use shell variables ( $BUSER, $BGROUP, and $BMODE). The first two things are aimed at improving the ability of others (or me, six months after writing it) to quickly read and understand this script. The single use shell variable are an effort to provide a single place (the 'script variables' section) to make any forseen and simple changes to the script, hopefully reducing the probability of future editors (myself included) injecting bugs into the script.
Better Cron Job Scripts
I like to have up to date local mirrors of things like CPAN, Project Gutenberg, and Fedora Core installation and update files. These are all potentially long running processes, so I will want to use some sort of locking mechanism. These "locks" will be used to tell new cron job processes that an older process is already on the job (it's just taking a while). The "locking" I use will check for a file in a directory (used only for cron job locks) and create it if it does not exist. Immediately afterward, I use shell trap commands to setup the removal of that lock file on exit (regular script end and various errors).
I will also want to run the same script on a few different hosts, hopefully without having to do a lot of editing and setup beforehand. To make this happen I will need to test for the existence of some directories and files I need, and create and set their permissions correctly if they do not exist. Some errors take a lot of effort to handle, and if you must handle them it may make sense to handle them in Perl or Python instead of a command shell like bash. However, bash is more than capable of handling the "low hanging fruit" of directory and file existence and creation.
Here is the example:
#!/bin/sh
# rsync-fedora4-mirror
# common source and destination
CMNSRC=mirror.cs.wisc.edu::fedora-linux-core
CMNDST=/var/ftp/pub/fedora
# user
CUSER=`/usr/bin/whoami`
# directories
CRONDIR=/home/$CUSER/cron
LOCKDIR=$CRONDIR/lock
EXFILE=$CRONDIR/fc4-excludes.txt
# options
GETSOURCES=0
RSYNCOPTS="-rlHtS --delete --delete-excluded --exclude-from=$EXFILE"
# files
RSYNC=/usr/bin/rsync
ECHO=/bin/echo
RM=/bin/rm
TOUCH=/bin/touch
MKDIR=/bin/mkdir
FIND=/usr/bin/find
CHMOD=/bin/chmod
# directory and file modes for cron and mirror files
CDMODE=700
CFMODE=600
MDMODE=755
MFMODE=644
# testing for directories and files needed
if [ ! -d $CRONDIR ]; then $MKDIR -p $CRONDIR; $CHMOD $CDMODE $CRONDIR; fi
if [ ! -d $LOCKDIR ]; then $MKDIR -p $LOCKDIR; $CHMOD $CDMODE $LOCKDIR; fi
if [ ! -f $EXFILE ]; then $TOUCH $EXFILE; $CHMOD $CFMODE $EXFILE; fi
# lock file creation and removal
LOCKFILE=$LOCKDIR/`basename $0`.lock
[ -f $LOCKFILE ] && $ECHO $LOCKFILE exists && exit 0
trap "{ $RM -f $LOCKFILE; exit 255; }" 2
trap "{ $RM -f $LOCKFILE; exit 255; }" 9
trap "{ $RM -f $LOCKFILE; exit 255; }" 15
trap "{ $RM -f $LOCKFILE; exit 0; }" EXIT
$TOUCH $LOCKFILE
# now mirror and set permissions for each group of files
# release
RSYNCSRC=$CMNSRC/4/i386/os/
RSYNCDST=$CMNDST/4/i386/os/
if [ ! -d $RSYNCDST ]; then $MKDIR -p $RSYNCDST; fi
cd $RSYNCDST
$RSYNC $RSYNCOPTS $RSYNCSRC $RSYNCDST
$FIND $RSYNCDST -type d -exec $CHMOD $MDMODE \{\} \;
$FIND $RSYNCDST -type f -exec $CHMOD $MFMODE \{\} \;
# updates
RSYNCSRC=$CMNSRC/updates/4/i386/
RSYNCDST=$CMNDST/updates/4/i386/
if [ ! -d $RSYNCDST ]; then $MKDIR -p $RSYNCDST; fi
cd $RSYNCDST
$RSYNC $RSYNCOPTS $RSYNCSRC $RSYNCDST
$FIND $RSYNCDST -type d -exec $CHMOD $MDMODE \{\} \;
$FIND $RSYNCDST -type f -exec $CHMOD $MFMODE \{\} \;
[ $GETSOURCES -lt 1 ] && exit 0
# release sources
RSYNCSRC=$CMNSRC/4/SRPMS/
RSYNCDST=$CMNDST/4/SRPMS/
if [ ! -d $RSYNCDST ]; then $MKDIR -p $RSYNCDST; fi
cd $RSYNCDST
$RSYNC $RSYNCOPTS $RSYNCSRC $RSYNCDST
$FIND $RSYNCDST -type d -exec $CHMOD $MDMODE \{\} \;
$FIND $RSYNCDST -type f -exec $CHMOD $MFMODE \{\} \;
# updates sources
RSYNCSRC=$CMNSRC/updates/4/SRPMS/
RSYNCDST=$CMNDST/updates/4/SRPMS/
if [ ! -d $RSYNCDST ]; then $MKDIR -p $RSYNCDST; fi
cd $RSYNCDST
$RSYNC $RSYNCOPTS $RSYNCSRC $RSYNCDST
$FIND $RSYNCDST -type d -exec $CHMOD $MDMODE \{\} \;
$FIND $RSYNCDST -type f -exec $CHMOD $MFMODE \{\} \;
This is a rather trusting example, as many problem could occur if the mirror you are using has "problems". Eliminate the "--delete" and "--delete-excluded" arguments from the RSYNCOPTS variable definition to be less trusting.
Wednesday, 04 November 2009
linux
Here's a crontab example that shows how to run a command from the cron daemon once every day
0 1 * * * /var/www/backup.sh
crontab: invalid option -- -
crontab: usage error: unrecognized option
usage: crontab [-u user] file
crontab [-u user] [ -e | -l | -r ]
(default operation is replace, per 1003.2)
-e (edit user's crontab)
-l (list user's crontab)
-r (delete user's crontab)
-i (prompt before deleting user's crontab)
Sunday, 01 November 2009
linux
Like most Linux users, you may find it necessary to schedule repetitive tasks to be run at a certain time. Such tasks can occur as frequently as once a minute, to as infrequently as once a year. This scheduling can be done by using the ``cron'' facilities.
The cron facilities as implemented in Linux are fairly similar to those available in other Unix implementations. However, Red Hat has adopted a slightly different way of scheduling tasks than is usually done in other distributions of Linux. Just as in other distributions, scheduling information is placed in the system ``crontab'' file (locating in the ``/etc/'' directory), using the following format:
minute hour day month year command
|
You can specify each time component as an integer number (eg. 1 through 12 for the months January through December), or specify one or more components as ``*'' characters which will be treated as wildcards (eg. * in the month component means the command will run at the given day and time in every month. Here are some examples:
# Mail the system logs at 4:30pm every June 15th.
30 16 15 06 * for x in /var/log/*; do cat ${x} | mail postmaster; done
# Inform the administrator, at midnight, of the changing seasons.
00 00 20 04 * echo 'Woohoo, spring is here!'
00 00 20 06 * echo 'Yeah, summer has arrived, time to hit the beach!'
00 00 20 10 * echo 'Fall has arrived. Get those jackets out. :-('
00 00 20 12 * echo 'Time for 5 months of misery. ;-('
|
Note that commands which produce output to standard out (ie. a terminal) such as the examples above using ``echo'' will have their output mailed to the ``root'' account. If you want to avoid this, simply pipe the output to the null device as follows:
00 06 * * * echo 'I bug the system administrator daily at 6:00am!' >/dev/null
|
In addition to the standard ``crontab'' entries, Red Hat adds several directories:
/etc/cron.hourly/
/etc/cron.daily/
/etc/cron.weekly/
|
As their names suggest, executable files can be placed in any of these directories, and will be executed on an hourly, daily, or weekly basis. This saves a bit of time when setting up frequent tasks; just place the executable script or program (or a symbolic link to one stores elsewhere) in the appropriate directory and forget about it.
From :http://tldp.org/LDP/lame/LAME/linux-admin-made-easy/using-cron.html
Saturday, 31 October 2009
linux
Examples of crontabs
# record the memory usage of the system every monday # at 3:30AM in the file /tmp/meminfo 30 3 * * mon cat /proc/meminfo >> /tmp/meminfo # run custom script the first day of every month at 4:10AM 10 4 1 * * /root/scripts/backup.sh
|
As you can see from the /etc/crontab file, it uses the run-parts script to execute the scripts in the /etc/cron.hourly, /etc/cron.daily, /etc/cron.weekly, and/etc/cron.monthly files on an hourly, daily, weekly, or monthly basis respectively. The files in these directory should be shell scripts.
If a cron tasks needs to be executed on a schedule other than hourly, daily, weekly, or monthly, it can be added to the /etc/cron.d directory. All files in this directory use the same syntax as /etc/crontab.
The cron daemon checks the etc/crontab file, the etc/cron.d/ directory, and the /var/spool/cron directory every minute for any changes. If any changes are found, they are loaded into memory. Thus, the daemon does not need to be restarted if a crontab file is changed.
Users other than root can configure cron tasks by using the crontab utility. All user-defined crontabs are stored in the /var/spool/cron directory and are executed using the usernames of the users that created them. To create a crontab as a user, login as that user and type the command crontab -e to edit the user's crontab using the editor specified by the VISUAL or EDITOR environment variable. The file uses the same format as /etc/crontab. When the changes to the crontab are saved, the crontab is stored according to username and written to the file /var/spool/cron/username.
*/1 * * * * php /var/www/html/poller.php > /dev/null 2>&1
0 1 * * * nice -n 15 /var/www/backup.sh
*/1 * * * * mysql -h localhost -u root -p123456
Saturday, 31 October 2009
linux
The main configuration file for cron, /etc/crontab, contains the following lines:
SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root HOME=/ # run-parts 01 * * * * root run-parts /etc/cron.hourly 02 4 * * * root run-parts /etc/cron.daily 22 4 * * 0 root run-parts /etc/cron.weekly 42 4 1 * * root run-parts /etc/cron.monthly
|
The first four lines are variables used to configure the environment in which the cron tasks are run. The value of the SHELL variable tells the system which shell environment to use (in this example the bash shell), and the PATH variable defines the path used to execute commands. The output of the cron tasks are emailed to the username defined with the MAILTOvariable. If the MAILTO variable is defined as an empty string (MAILTO=""), email will not be sent. The HOME variable can be used to set the home directory to use when executing commands or scripts.
Each line in the /etc/crontab file has the format:
minute hour day month dayofweek command
|
-
minute — any integer from 0 to 59
-
hour — any integer from 0 to 23
-
day — any integer from 1 to 31 (must be a valid day if a month is specified)
-
month — any integer from 1 to 12 (or the short name of the month such as jan, feb, and so on)
-
dayofweek — any integer from 0 to 7 where 0 or 7 represents Sunday (or the short name of the week such as sun, mon, and so on)
-
command — the command to execute. The command can either be a command such as ls /proc >> /tmp/proc or the command to execute a custom script that you wrote.
For any of the above values, an asterisk (*) can be used to specify all valid values. For example, an asterisk for the month value means execute the command every month within the constraints of the other values.
A hyphen (-) between integers specifies a range of integers. For example, 1-4 means the integers 1, 2, 3, and 4.
A list of values separated by commas (,) specifies a list. For example, 3, 4, 6, 8 indicates those four specific integers.
The forward slash (/) can be used to specify step values. The value of an integer can be skipped within a range by following the range with /<integer>. For example, 0-59/2 can be used to define every other minute in the minute field. Step values can also be used with an asterisk. For instance, the value */3 can be used in the month field to run the task every third month.
Any lines that begin with a hash mark (#) are comments and are not processed.
Friday, 23 October 2009
linux
Linux Command: crontab
usage: crontab [-u user] file
crontab [-u user] [ -e | -l | -r ]
(default operation is replace, per 1003.2)
-e (edit user's crontab)
-l (list user's crontab)
-r (delete user's crontab)
-i (prompt before deleting user's crontab)
Lines that can be in the crontab file.
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).
Examples
crontab -e = edits the crontab file to be used.
0 12 14 2 * mailx john%Happy Birthday!%Time for lunch.
#crontab -e
*/1 * * * * php /var/www/html/poller.php > /dev/null 2>&1
0 1 * * * nice -n 15 /var/www/backup.sh
Thursday, 12 November 2009
linux
I want to cat a file and view the crontab entries and not use
crontab -e
or
crontab -l
/var/spool/cron/
there should be a file for every user that is allowed to and has a crontab
|