Pages

Thursday, June 18, 2015

How to setup a Cron Schedule job in Linux

I would like to setup Cron Schedule job to execute every minute in Red hat Linux. The following tasks needs to be performed to setup the Cron Schedule Job.

1. Create or Edit the Cron Tab File


Execute the following command to edit the cron tab file..

crontab -e

After executing the above command it will edit crontab file.


2. Schedule the Cron Job.

     2.1 Cron Tab Format

      The following format is being used to setup the cron job in Linux.

       2.1.1 Minute (0-59)
       2.1.2 Hour (0-23)
       2.1.3 Day Of Month (1-31)
       2.1.4 Month (1-12)
       2.1.5 Day of Week (0-6)
       2.1.6 Command

In my example I am going to setup the cron schedule job to run every minute and append the display output to a file as follows:

  * * * * * /home/testuser/display.sh >>display.out

 in the above example, I have used * * * * * because My Job needs to be run every minute.

The display.sh content is given below:

echo "Diaplying Message"


3. Listing the Cron Tab


After setting the cron job, you can list the all available jobs in your user space as follows:

 crontab -l

  After executing the above command it will display the following output:

* * * * * /home/testuser/display.sh >>display.out

4. Cron Job Store Location

After creating the Cron Schedule Job, Job details are stored in the following location with username as a file and this file owned by root:root.

 /var/spool/cron/testuser

 Login as a root and edit the /var/spool/cron/testuser file and verify the cron job content. In my example step 2 content and step 4 content are identical.


4. Logging

The cron schedule job running info will be displayed in the following log file:

/var/log/cron 

5. Error Messages

If your cron job throwing any error or cron job is not running due to fatal errors or command not found. The error messages are displayed in the following location.

/var/spool/mail/testuser

The sample error message is given below:

From root@test.ad.example.edu  Thu Jun 18 09:01:02 2015
Return-Path: <root@test.ad.example.edu>
X-Original-To: testuser
Delivered-To: testuser@test.ad.example.edu
Received: by test.ad.example.edu (Postfix, from userid 501)
id 385CE20357; Thu, 18 Jun 2015 09:01:02 -0700 (PDT)
From: root@test.ad.example.edu (Cron Daemon)
To: testuser@test.ad.example.edu
Subject: Cron <testuser@sssdlab-test> testuser /home/testuser/display.sh >>display.out
Content-Type: text/plain; charset=UTF-8
Auto-Submitted: auto-generated
X-Cron-Env: <LANG=en_US.UTF-8>
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <HOME=/home/testuser>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=testuser>
X-Cron-Env: <USER=testuser>
Message-Id: <20150618160102.385CE20357@test.ad.example.edu>
Date: Thu, 18 Jun 2015 09:01:02 -0700 (PDT)

/bin/sh: testuser: command not found


I have setup the cron job to throw the error in the following format:

* * * * * testuser /home/testuser/display.sh >>display.out

In the above format command is testuser, for that reason, cron job throwing error message is "/bin/sh: testuser: command not found" 

The expected result is it will create a display.out file and the content is not appended to a file.

2 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. Crontab files are simple text files that have a particular format. Each line of a crontab file follows a particular format as a series of fields, separated by spaces and/or tabs. Each field can have a single value or a series of values. A single cron job should take up exactly one line, but this can be a long line (more than 80 characters).
    for more information click here:how to setup a cron job

    ReplyDelete