Automating routine administrative tasks is a core practice in DevOps and system administration. In Unix-like operating systems, the cron daemon (crond) serves as a time-based job scheduler. It enables users to run shell commands, scripts, or system tasks automatically at specific times, dates, or intervals.
Individual user schedules are managed using crontabs (cron tables). These are configuration files containing lists of commands and their execution schedules. The system daemon constantly runs in the background and checks the system’s crontab files once per minute to determine if any jobs are scheduled for execution.
This guide outlines the complete syntax of the cron expression format, details the steps to install and enable the cron daemon on Nautilus application servers, sets up a cron job for the root user that writes to a temporary file every 5 minutes, and verifies its execution.
A cron job is defined by a single line containing five time and date fields, followed by the command to execute:
.---------------- 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,wed,thu,fri,sat
| | | | |
* * * * * command to be executed
To schedule complex intervals, you can use specialized operator characters:
| Operator | Name | Description | Example |
|---|---|---|---|
* |
Wildcard | Represents all possible values in that field. | * in the hour field means “every hour”. |
, |
Value List | Specifies a list of discrete values for execution. | 1,3,5 in the hour field means “at 1 AM, 3 AM, and 5 AM”. |
- |
Range | Defines a range of consecutive values. | 1-5 in the day-of-week field means “Monday to Friday”. |
/ |
Step / Interval | Specifies step values or repeating intervals. | */5 in the minute field means “every 5 minutes”. |
* * * * **/5 * * * *0 * * * *0 0 * * *30 4 * * 00 2 1 * *0 9-17 * * 1-5stapp01, stapp02, stapp03)tony, steve, banner)root*/5 * * * *)echo hello > /tmp/cron_textApply the following setup instructions to each of your application servers:
SSH into the target application server from the Jump Host:
# Example for App Server 1
ssh tony@stapp01
By default, some minimal server environments might not have the cron package pre-installed. Install cronie (the standard cron daemon for RHEL/CentOS):
sudo yum install -y cronie
Ensure the cron daemon is configured to start automatically on system boot and start the service in the current session:
# Start and enable the service
sudo systemctl enable --now crond
Verify that the daemon is active and running:
sudo systemctl status crond
Open the crontab editor for the root user. Using the crontab tool ensures that your configuration is syntax-validated before saving:
sudo crontab -e
Add the following line to the crontab configuration:
*/5 * * * * echo hello > /tmp/cron_text
Save and close the editor. If you are using the default vi editor:
Esc.:wq and press Enter.crontab: installing new crontab.Verify that the cron job was successfully written and is active for the root user:
sudo crontab -l
Expected output:
*/5 * * * * echo hello > /tmp/cron_text
Check the cron utility logs under /var/log/cron to verify that the cron daemon is executing the scheduled job:
sudo tail -f /var/log/cron
Expected execution trace snippet:
Jun 27 12:00:01 stapp01 CROND[10245]: (root) CMD (echo hello > /tmp/cron_text)
Wait at least 5 minutes for the job to fire, and verify that the output file /tmp/cron_text has been created with the expected content:
cat /tmp/cron_text
Expected output:
hello
Log out of the Application Server to return to the Jump Host:
exit