Setting Up A Cron Job

Setting Up A Cron Job

The following is a list of system cron directories:

/etc/cron.hourly

/etc/cron.daily

/etc/cron.weekly

/etc/cron.monthly

 

The above folders are a location where you can save scripts to have them run at defined intervals.  They are run with root level permissions, and root access is required to add scripts in these folders.

Any scripts placed in the /etc/cron.hourly folder will run on the first minute of every hour (may vary depending on the settings in /etc/cron.d/0hourly)

The other folders will run scripts at their stated interval (i.e. daily, weekly, monthly).  The exact moment they run is dependent on the settings in /etc/anacrontab or the /etc/crontab file.  The file used for these settings depend on the OS version.

For the script to run, it must have the correct permissions.  When finished creating the script, run:

chmod 755 script.sh

(replace “script.sh” with the name of your script)

 

Cron jobs can also be added by users.  To add a new cron job, run the command:

crontab –e

This opens vi editor for you (assuming your $EDITOR variable is set).

The job should be created with the intended interval that it should run and the script/command to be executed.  Create the cron command using the following syntax:

  1. The number of minutes after the hour (0 to 59)
  2. The hour in military time (24 hour) format (0 to 23)
  3. The day of the month (1 to 31)
  4. The month (1 to 12)
  5. The day of the week(0 or 7, 0=Monday, 1=Tuesday, etc)
  6. The command to run

 

Note:  Use an “*” in place for any of the above numbers to indicate all options for that specific placeholder (i.e. an “*” in the hour field would mean to run every hour)

 

 

 

 

More graphically, it would look like this:

* * * * * Command to be executed

- - - - -

| | | | |

| | | | +----- Day of week (0-7)

| | | +------- Month (1 - 12)

| | +--------- Day of month (1 - 31)

| +----------- Hour (0 - 23)

+------------- Min (0 - 59)

An example command would be

0 12 * * 7 /home/username/script.sh

In this example, the script would execute at noon, every Sunday.

To save changes to the crontab, hit ESC key, and then type :wq! to exit.

The script/command that the cron job will run can be anywhere on the system that the user creating the cron has access to.  The crontab file for the user is usually stored in /var/spool/cron/<username>

To list existing cron jobs:

crontab –l

To remove an existing cron job:

crontab –e

Delete the line that contains your cron job, then save and exit.

One final note:

Cron jobs can be created by the root user, to run as another user.  An example would be creating a script to run through cron as the sql service account.  To do this, log in as root and create a flat file with the cron job syntax shown above, but with one addition.  Add the user name of the account that the script should run as, between the interval settings and the script/command name.  Example:

0 12 * * 7 sqlservice /home/username/script.sh

The flat file containing this cron job must be saved in the /etc/cron.d folder.