
In the world of Linux and Unix system administration, automating repetitive tasks is essential for efficiency and reliability. One of the most powerful tools available for this purpose is Cron. Understanding how to use Cron allows administrators and users to schedule commands or scripts to run automatically at specified intervals or times.
At its heart, Cron is a time-based job scheduler. It allows you to set up jobs, often called “cron jobs,” which run in the background without manual intervention. The schedules and commands for these jobs are stored in a special file known as the crontab (short for “cron table”). Each user on a system can have their own crontab file, typically located in /var/spool/cron/crontabs/
(though the exact location can vary slightly between systems). There are also system-wide crontab files, usually found at /etc/crontab
or within the /etc/cron.d/
directory, often used for system-level maintenance tasks.
Managing your personal crontab is usually done using the crontab
command. To edit your crontab, you simply run crontab -e
. This opens your crontab file in your default text editor. To list your current scheduled jobs, you use crontab -l
. If you need to remove all scheduled jobs from your crontab, the command is crontab -r
, but be cautious as this requires confirmation and is irreversible.
The syntax for entries within a crontab file is crucial. Each line typically represents a single scheduled job and follows a specific format:
minute hour day_of_month month_of_year day_of_week command_to_execute
Let’s break down the time fields:
- minute: (0 – 59)
- hour: (0 – 23)
- day_of_month: (1 – 31)
- month_of_year: (1 – 12 or names like Jan, Feb)
- day_of_week: (0 – 6, Sunday=0 or 7, or names like Sun, Mon)
You can use several special characters in these fields:
*
: A wildcard, meaning “every possible value” for that field.,
: Separator for listing multiple values (e.g.,1,15
for the 1st and 15th).-
: Specifies a range of values (e.g.,9-17
for hours 9 AM through 5 PM)./
: Defines step values (e.g.,*/10
in the minute field means “every 10 minutes”).
For common scheduling needs, Cron also offers special strings as shortcuts:
@reboot
: Run once after system boots.@yearly
or@annually
: Run once a year (0 0 1 1 *
).@monthly
: Run once a month (0 0 1 * *
).@weekly
: Run once a week (0 0 * * 0
).@daily
or@midnight
: Run once a day (0 0 * * *
).@hourly
: Run once an hour (0 * * * *
).
The final part of the line is the command you want to execute. This can be a simple shell command, a script, or any executable program. It’s often best practice to use the full path to your executable and script to avoid issues with the Cron environment’s potentially limited PATH
variable. Redirecting output is also important; sending standard output and error to /dev/null
(>/dev/null 2>&1
) is common for silent execution, or you can redirect output to a log file for later review.
Cron is an indispensable tool for routine maintenance, backups, generating reports, and countless other automated tasks crucial for keeping Linux and Unix systems running smoothly. Mastering its simple yet powerful syntax is a key skill for anyone working with these operating systems.
Source: https://kifarunix.com/how-to-schedule-cron-jobs-tasks-in-linux-unix/