Resource icon

Scheduling cron job under Linux or UNIX

Adding Jobs To cron Under Linux or UNIX:

Using cron the Linux and UNIX users can run their scripts and commands at the specified time and date. Scripts can also be scheduled. Corn is the mostly preferred tool for running system admin jobs and it will always executed in the background. It usually checks the /etc/crontab file, /var/spool/cron/ directory and /etc/cron.*/ directory. Crontab command is to install, create, edit, uninstall or list cron jobs that belongs to Vixie Cron. Every use have a corn file and they can edit the files in /var/spool/cron/crontabs.

Creating or Editing the Corn Jobs:

Code:
$ crontab –e
The above command is used to create or edit corn jobs in UNIX or Linux. Cron don’t want to be restarted while modifying the corn file.

Cron Configuration File Types:

The UNIX / Linux system crontab is usually used by critical jobs and services of the system that is required by privileges. The user crontab allows the user to install their own corn jobs with the crontab command.

Crontab Syntax:

Code:
1 2 3 4 5 /path/to/command arg1 arg2
Or

Code:
1 2 3 4 5 /root/backup.sh


Here one represents the minutes, 2 represents the hours, 3 specifies the days, 4 represents the months and 5 specifies the day of the week. /path/to/command indicates the name of the command and scripts that needs scheduling.

Code:
1 2 3 4 5 USERNAME /path/to/command arg1 arg2
The above command is used for the system jobs.

Example:

Code:
# crontab –e

0 3 * * * /root/backup.sh
The cron job is installed in the first command and next command is used to run the /root/backup.sh script for every day at 3 A.M. the file is saved and closed after the execution.

Operators:

Operators are used for defining values in the field.

The asterisk operator (*) indicates all the values for values of the field. For instance, the * in month field indicates every month in the year.

The comma operator (,) indicates the list of values. 1, 5, 10, 15 is an example.

The dash operator (-) specifies the range of the values. For instance 10-15 indicates the values from 10 to 15 i.e 10, 11, 12, 13, 15.

The separator operator (/) indicates the values in steps.

Disabling email outputs:

The output of the script or command will be mailed to the local email account by default. To stop sending the email >/dev/null 2>&1 has to be appended to crontab.

Example:

Code:
MAILTO="info@hoststud.com"
0 3 * * * /root/backup.sh >/dev/null 2>&1
For defining MAILTO variable the above commands are used. Here output is mailed to info@hoststud.com

Cron jobs lists:

Code:
# crontab -l
# crontab -u username –l
Below command is used to remove the crontab jobs

# Delete the current cron jobs #

Code:
crontab –r
## Delete job for specific user. Must be run as root user ##


Code:
crontab -r -u username
/etc/crontab is the crontab file of the system. It is used only by the root user for the configurations of system jobs.

How to use my own cron jobs or script?

The below script use to check the specific port is down or not or if it is then you will get the email notification about it. This script put in /var/spool then you have to call the script through below expression, this expression will execute the cron in every minute.

Code:
*/10 * * * * sh /var/spool/apache.sh
Code:
#!/usr/bin/env bash
SERVER=hoststud.com
PORT=80
</dev/tcp/$SERVER/$PORT
if [ "$?" -ne 0 ]; then
  echo "" | mail -s "Server Down" abc@gmail.com
  exit 1
else
  exit 0
fi
Author
Bhawani Singh
First release
Last update
Rating
0.00 star(s) 0 ratings

More resources from Bhawani Singh

Top