Search Engine



RedhatEnterpriseLinux Blog

Automation with crontab

Automation with crontab
In any operating system, it is possible to create jobs that you want to reoccur. This
process, known as job scheduling, is usually done based on user-defined jobs. For
Red Hat, this process is handled by the cron service, which can be used to schedule tasks (also called jobs). By default, Red Hat comes with a set of predefined jobs that occur on the system (hourly, daily, weekly, monthly, and with arbitrary periodicity). As an administrator, however, you can define your own jobs and allow your users to create them as well.

Step 1.           Although the cron package is installed by default, check to make sure it is installed on your system:
# rpm -qa | grep cron

Step 2.           If, for some reason, the package isn’t installed, install it now:
# yum install -y cronie cronie-anacron crontabs
Step 3.           Verify that the cron service is currently running:
# service crond status
crond (pid 2239) is running...
Step 4.           Also verify that the service is set to start when the system boots:
# chkconfig --list crond
crond 0:off 6:off 1:off 2:on 3:on 4:on 5:on
To start working with cron, you first need to look at the two config files that control access to the cron service. These two files are:
The /etc/cron.allow file:
If it exists, only these users are allowed (cron.deny is ignored).
If it doesnt exist, all users except cron.deny are permitted.
The /etc/cron.deny file:
If it exists and is empty, all users are allowed (Red Hat Default).
For both files:
If neither file exists, root only.

Creating cron Jobs
The default setting for Red Hat allows any user to create a cron job. As the root
user, you also have the ability to edit and remove any cron job you want. Let’s jump into creating a cron job for the system. You can use the crontab command to create, edit, and delete jobs.
Syntax: crontab [-u user] [option]
-e        Edits the user’s crontab
-l          Lists the user’s crontab
-r         Deletes the user’s crontab
-i          Prompts before deleting the user’s crontab
Before you start using the crontab command, however, you should look over the
format it uses so you understand how to create and edit cron jobs. Each user has her own crontab file in /var/spool/cron (the file for each user is created only after the user creates her first cron job), based on the username of each user. Any “allow” actions taken by the cron service are logged to /var/log/cron.
View the /etc/crontab file to understand its syntax:

# grep ^# /etc/crontab
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# ||.---------- day of month (1 - 31)
# ||| .------- month (1 - 12) OR jan,feb,mar,apr ...
# ||| | .---- day of week (0 - 6) (Sunday=
* * * * .     command to be executed

This file clearly spells out the values that can exist in each field. You must make sure that you provide a value for each field; otherwise, the crontab will not be created.
You can also define step values by using */<number to step by>. For example, you
could put */5 in the minute field to mean every fifth minute.
The best way to understand these fields is to create a crontab file and make some
sample jobs. Using a text editor, create the following file in the /tmp directory.

Sample script for cron:
# nano /tmp/sample_script
# Send a msg to all users on the console
wall “Hello World”
Save the file and set the following permissions:
# chmod 775 /tmp/sample_script
Now create a cron job to launch the sample script. Because you are using the root
user account, you can create a crontab for the normal user account user01.

Step 1.           Set up user01’s crontab:
# crontab -u user01 -e

Step 2.           Add the following line:
* * * * * /tmp/sample_script

Step 3.           Save the file and quit the editor.

Because you are using * in every field to test, in about 60 seconds you will see the script execute, and it should display ”Hello World” on your screen.

Obviously, you don’t want messages every 60 seconds on your system, but you get the idea of how cron and crontab should work. Let’s list the current jobs that user01 has set up, and you should see the job just created (do this just for verification purposes).
Step 4.           List the current cron jobs of user01:
# crontab -u user01 -l
* * * * * /tmp/sample_script
You can now edit the crontab again to remove the single line, effectively deleting that individual job, or you can just delete the user’s crontab entirely.

Step 5.           To remove a user’s crontab jobs, use the following command:
# crontab -u user01 -r
Step 6.           You can verify the activity on different crontabs by...wait for it...looking at the log files!

# tail /var/log/cron
Sep 10 09:08:01 new-host crond[4213]: (user01) CMD
Sep 10 09:08:38 new-host crontab[4220]: (root) LIST (user01)
Sep 10 09:09:01 new-host crond[4224]: (user01) CMD
Sep 10 09:10:01 new-host crond[4230]: (user01) CMD
Sep 10 09:11:01 new-host crond[4236]: (user01) CMD
Sep 10 09:12:01 new-host crond[4242]: (user01) CMD
Sep 10 09:13:01 new-host crond[4248]: (user01) CMD
Sep 10 09:13:06 new-host crontab[4251]: (root) LIST (user01)
Sep 10 09:14:01 new-host crond[4253]: (user01) CMD
Sep 10 09:14:15 new-host crontab[4258]: (root) DELETE (user01)

You can see that the cron service is executing the /tmp/sample_script file, and you can see the action after you deleted it. The process of creating crontabs and scheduling jobs is the same for all users on the system, including the root user.

Here is what the /etc/crontab file looks like for RHEL5 systems:
# 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


Post a Comment

Powered by Blogger.


Copyright © Redhat Enterprise linux. Original Concept and Design by My Blogger Themes
My name is Abdul Razaq but people call me Raziq. Here is my home page: I live in Quetta, Pakistan and work as an IT-Engineer.