Sunday, June 24, 2018

Running cron Job

What is cron?

Cron is the name of program that enables unix users to execute commands
or scripts (groups of commands) automatically at a specified time/date.
It is normally used for sys admin commands, like makewhatis, which builds a 
search database for the man -k command, or for running a backup script, 
but can be used for anything. A common use for it today is connecting to 
the internet and downloading your email.


Is a daemon, which means that it only needs to be started once, and will lay
dormant until it is required.A Web server is a daemon, it stays dormant until
it gets asked for a web page.The cron daemon, or crond, stays dormant until a
time specified in one of the config files, or crontabs. On most Linux distributions crond is automatically installed and entered into
the start up scripts. To find out if it's running do the following:

$ ps aux | grep crond
root       311  0.0  0.7  1284  112 ?        S    Dec24   0:00 crond
cog       8606  4.0  2.6  1148  388 tty2     S    12:47   0:00 grep crond
The top line shows that crond is running, the bottom line is the search we just run. If it's not running then either you killed it since the last time you rebooted, or it wasn't started. To start it, just add the line crond to one of your start up scripts. The process automatically goes into the back ground, so you don't have to force it with &. Cron will be started next time you reboot. To run it without rebooting, just type crond as root:
root@pingu # crond
With lots of daemons, (e.g. httpd and syslogd) they need to be restarted after the config files have been changed so that the program has a chance to reload them. Vixie Cron will automatically reload the files after they have been edited with the crontab command. Some cron versions reload the files every minute, and some require restarting, but Vixie Cron just loads the files if they have changed.

Using cron There are a few different ways to use cron (surprise, surprise).

In the /etc directory you will probably find some sub directories called 'cron.hourly', 'cron.daily', 'cron.weekly' and 'cron.monthly'. If you place a script into one of those directories it will be run either hourly, daily, weekly or monthly, depending on the name of the directory. If you want more flexibility than this, you can edit a crontab (the name for cron's config files). The main config file is normally /etc/crontab. On a default RedHat install, the crontab will look something like this:
root@pingu # cat /etc/crontab

# 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 part is almost self explanatory; it sets the variables for cron. SHELL is the 'shell' cron runs under. If unspecified, it will default to the entry in the /etc/passwd file. PATH contains the directories which will be in the search path for cron e.g if you've got a program 'foo' in the directory /usr/cog/bin, it might be worth adding /usr/cog/bin to the path, as it will stop you having to use the full path to 'foo' every time you want to call it. MAILTO is who gets mailed the output of each command. If a command cron is running has output (e.g. status reports, or errors), cron will email the output to whoever is specified in this variable. If no one if specified, then the output will be mailed to the owner of the process that produced the output. HOME is the home directory that is used for cron. If unspecified, it will default to the entry in the /etc/passwd file. Now for the more complicated second part of a crontab file. An entry in cron is made up of a series of fields, much like the /etc/passwd file is, but in the crontab they are separated by a space. There are normally seven fields in one entry. The fields are:

minute hour dom month dow user cmd

minute This controls what minute of the hour the command will run on, and is between '0' and '59' hour This controls what hour the command will run on, and is specified in the 24 hour clock, values must be between 0 and 23 (0 is midnight) dom This is the Day of Month, that you want the command run on, e.g. to run a command on the 19th of each month, the dom would be 19. month This is the month a specified command will run on, it may be specified numerically (0-12), or as the name of the month (e.g. May) dow This is the Day of Week that you want a command to be run on, it can also be numeric (0-7) or as the name of the day (e.g. sun). user This is the user who runs the command. cmd This is the command that you want run. This field may contain multiple words or spaces. If you don't wish to specify a value for a field, just place a * in the field.

No comments:

Post a Comment