A cron job is a scheduled task that runs automatically at specified times or intervals on Unix-like operating systems. Cron jobs are used to automate repetitive tasks, like running scripts, backing up databases, or generating reports.
Key Components of a Cron Job:
-
Cron Daemon (
cron
):- The cron daemon is a background process that reads scheduled tasks from the crontab file and executes them at the specified times. This process continuously runs on the system and checks every minute for tasks to execute.
-
Crontab:
- A crontab (cron table) is a simple text file that lists cron jobs, specifying when and what to execute.
- Each user, including the root, can have their own crontab file. The system also has crontab files in
/etc/
that define system-wide cron jobs.
-
Crontab Syntax:
- The crontab file follows a specific syntax to schedule tasks based on time intervals. Each cron job is defined by five fields that specify minute, hour, day of the month, month, and day of the week, followed by the command to be executed.
* * * * * command_to_run
│ │ │ │ │
│ │ │ │ │
│ │ │ │ └─── Day of the week (0 - 6) (Sunday = 0 or 7)
│ │ │ └───── Month (1 - 12)
│ │ └─────── Day of the month (1 - 31)
│ └───────── Hour (0 - 23)
└─────────── Minute (0 - 59)
Example Cron Jobs
- Run a job every minute:
* * * * * /path/to/script.sh
This job will run the specified script every minute, regardless of the day, hour, or month.
- Run a job at 2:30 AM every day:
30 2 * * * /path/to/script.sh
This job will execute at 2:30 AM every day of the month.
- Run a job on the first day of every month at 3:00 PM:
0 15 1 * * /path/to/script.sh
This job will run on the 1st of each month at 3:00 PM.
- Run a job every 5 minutes:
*/5 * * * * /path/to/script.sh
The */5
syntax tells cron to execute the script every 5 minutes.
Special Characters in Crontab
-
Asterisk (
*
): Means “every” in the given field.- Example:
* * * * *
runs every minute.
- Example:
-
Comma (
,
): Used to specify multiple values in the same field.- Example:
0 9,17 * * * /path/to/script.sh
runs at 9 AM and 5 PM.
- Example:
-
Hyphen (
-
): Used to define a range of values.- Example:
0 9-17 * * * /path/to/script.sh
runs every hour between 9 AM and 5 PM.
- Example:
-
Slash (
/
): Used for step values.- Example:
*/10 * * * * /path/to/script.sh
runs every 10 minutes.
- Example:
-
Question mark (
?
): Used instead of an asterisk when specifying “no specific value” in day fields (day of the month or day of the week). This is commonly used incron
systems that support both fields like Quartz Scheduler.
Special Scheduling Keywords
In addition to the numeric syntax, cron allows for some shorthand notations:
- @reboot: Runs the command once when the system starts up.
-
@yearly or @annually: Runs the command once a year (equivalent to
0 0 1 1 *
). -
@monthly: Runs once a month (equivalent to
0 0 1 * *
). -
@weekly: Runs once a week (equivalent to
0 0 * * 0
). -
@daily: Runs once a day (equivalent to
0 0 * * *
). -
@hourly: Runs once an hour (equivalent to
0 * * * *
).
Output Handling and Logging
Cron jobs run in the background, and their output is not displayed in the terminal. You can capture the output (both standard output and errors) by redirecting it to a log file.
* * * * * /path/to/script.sh >> /path/to/logfile.log 2>&1
-
>> /path/to/logfile.log
: Appends standard output to a log file. -
2>&1
: Redirects standard error (2
) to standard output (1
), so that errors are also logged.
Environment Variables
Cron jobs run with a limited environment. The environment that cron uses can be different from your interactive shell, which means that:
- PATH may not include all directories where commands are located.
- Environment variables like HOME, USER, and SHELL may not be the same as in your user session.
To ensure your cron job works as expected, you should:
- Use full paths for all commands.
- Explicitly set environment variables if needed.
For example:
PATH=/usr/local/bin:/usr/bin:/bin
* * * * * /usr/local/bin/python3 /path/to/script.py
Managing Cron Jobs
-
Edit crontab:
- To create or edit your cron jobs, use:
crontab -e
-
List current cron jobs:
- To view your current cron jobs, use:
crontab -l
-
Remove a crontab:
- To delete your crontab (and remove all cron jobs), use:
crontab -r
-
View cron logs:
- Cron logs are usually stored in
/var/log/syslog
or/var/log/cron
depending on the system configuration. To view them, you can use:
tail -f /var/log/syslog
- Cron logs are usually stored in
Cron Permissions
There are two files used to control who can and cannot use cron
:
-
/etc/cron.allow
: Specifies users who are allowed to usecron
. -
/etc/cron.deny
: Specifies users who are denied access tocron
.
If both files are missing, only the root user can schedule cron jobs.
This gives a comprehensive view of how cron jobs work and how to manage them. Let me know if you need further explanations or examples!
Source link
lol