🌐 🇵🇱 Polski · 🇬🇧 EN
In every operating system, there are tasks that are performed cyclically and are repetitive. This process is called job scheduling and is usually based on actions defined by the user. In RedHat, the tool for automating and executing repetitive tasks is "cron". By default, RedHat has several predefined system jobs that can be configured by time (hourly, daily, weekly, monthly, etc.).
Step 1 - Although the "cron" package should be installed by default, it is worth checking if it actually is.
cronie-1.4.4-2.el6.x86_64
cronie-anacron-1.4.4-2.el6.x86_64
crontabs-1.10-32.1.el6.noarch
Step 2 - If the packages are not installed, let's do it now.
# yum install -y cronie cronie-anacron crontabs
Step 3 - Check if the "cron" service is running.
# service crond status
crond (pid 2239) is running...
Step 4 - We also check if "cron" starts with the system.
# chkconfig --list crond
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
When starting to work with the "cron" service, you should familiarize yourself with two configuration files:
- /etc/cron.allow
- /etc/cron.deny
The /etc/cron.allow file:
- If it exists, only these users have permission (cron.deny is ignored).
- If it does not exist, all users have permissions according to cron.deny.
The /etc/cron.deny file:
- If it exists and is empty, all users have permission.
- If it does not exist, only root can use cron.
CREATING JOBS IN CRON
By default in RedHat, all users are authorized to use the "cron" tool. To create a new job, we use the "crontab" command.
Syntax of the crontab command: # crontab [-u user] [options]
Crontab command options:
- -e - Edit user crontab
- -l - List user crontab
- -r - Remove user crontab
- -i - Prompt before removing user crontab
Before you start using the crontab command, familiarize yourself with the job format used. Each user has their own crontab file in /var/spool/cron (this file is created the moment the crontab command is used for the first time). All actions have their own logs saved in /var/log/cron.
To understand the crontab syntax, let's look at the /etc/crontab file:
# grep ^# /etc/crontab
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,etc.
# | | | | |
# * * * * * command to be executed
To show more precisely how this works, we will create a simple job in the /tmp directory.
Example script to be executed in cron:
# vi /tmp/sample_script
#!/bin/bash
#
# Send a msg to all users on the console
#
wall “Hello World”
We save the file and grant execution permissions:
# chmod 775 /tmp/sample_script
Now we will add a new job to the cron service. Since we are acting as the root user, we will assign the crontab job to a regular user, User01.
Step 1 - Setting up crontab for User01
# crontab -u user01 -e
Step 2 - Adding the line
* * * * * /tmp/sample_script
Step 3 - Saving the file and exiting the editor
Since we used the "*" symbol everywhere for testing, the script will execute every 60 seconds, displaying "Hello World" on the screen.
Step 4 - Checking which jobs User01 is running in cron
# crontab -u user01 -l
* * * * * /tmp/sample_script
Step 5 - Removing crontab jobs for User01
# crontab -u user01 -r
Step 6 - You can check crontab activity by viewing the log file content.
# tail /var/log/cron
Sep 10 09:08:01 new-host crond[4213]: (user01) CMD
(/tmp/sample_script)
Sep 10 09:08:38 new-host crontab[4220]: (root) LIST (user01)
Sep 10 09:09:01 new-host crond[4224]: (user01) CMD
(/tmp/sample_script)
Sep 10 09:10:01 new-host crond[4230]: (user01) CMD
(/tmp/sample_script)
Sep 10 09:11:01 new-host crond[4236]: (user01) CMD
(/tmp/sample_script)
Sep 10 09:12:01 new-host crond[4242]: (user01) CMD
(/tmp/sample_script)
Sep 10 09:13:01 new-host crond[4248]: (user01) CMD
(/tmp/sample_script)
Sep 10 09:13:06 new-host crontab[4251]: (root) LIST (user01)
Sep 10 09:14:01 new-host crond[4253]: (user01) CMD
(/tmp/sample_script)
Sep 10 09:14:15 new-host crontab[4258]: (root) DELETE (user01)
You can see that the /tmp/sample_script script was executed and that the root user disabled its execution.
In the RHEL5 release, the /etc/crontab file, which is used by the root user, differs slightly from the format used by regular system users. The /etc/crontab file has an additional space between the time settings and the actual command to be executed, and in this place, we must define which user this specific command will be executed as.
# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# 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
Here, the root user is defined for the needs of specific commands. The space for entering the user in crontab is an option in RHEL6.
Do you think anything will happen if cron is set to work all night on preparing a report, and you turn off the computer when you get home? It turns out that I will now present another important feature of cron. In the /etc/anacrontab file, tasks that are to be performed every time the system is started are defined. If the system is turned off while cron is working, after startup, the /etc/anacrontab services will be called again to ensure that all unfinished tasks have been completed.
Let's look at the /etc/anacrontab file:
Let's look at the /etc/anacrontab file:
# cat anacrontab
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
START_HOURS_RANGE=3-22
#period in days delay in minutes job-identifier command
1 5 cron.daily nice run-parts /etc/cron.daily
7 25 cron.weekly nice run-parts /etc/cron.weekly
@monthly 45 cron.monthly nice run-parts /etc/cron.monthlyThe comments included in the file make it easy to understand its task.
Comments