Automating tasks is a key advantage of using Linux, and cron is the tool most commonly used for scheduling repetitive tasks. This page will guide you through understanding and using cron to schedule tasks efficiently.
cron is a daemon that runs scheduled tasks at specific intervals. These tasks, known as "cron jobs," can be used to automate system maintenance, backups, or any other repetitive task.
- Cron Daemon (
crond): The background service that schedules and executes tasks. - Cron Jobs: The tasks that are scheduled using cron.
- Crontab: The configuration file where cron jobs are defined.
crontab is the file where you define your scheduled tasks. Each user has their own crontab, and there is also a system-wide crontab for tasks that require root privileges.
Each line in a crontab file follows this syntax:
* * * * * command_to_execute
- - - - -
| | | | |
| | | | +---- Day of the week (0 - 7) (Sunday=0 or 7)
| | | +------ Month (1 - 12)
| | +-------- Day of the month (1 - 31)
| +---------- Hour (0 - 23)
+------------ Minute (0 - 59)
-
Run a script every day at 2 AM:
0 2 * * * /path/to/script.sh
-
Run a backup every Sunday at 3 AM:
0 3 * * 0 /path/to/backup.sh
-
Clear a cache every hour:
0 * * * * /path/to/clear_cache.sh
-
View your crontab:
crontab -l
This command lists the current user's crontab entries.
-
Edit your crontab:
crontab -e
This opens the crontab file for editing.
-
Remove your crontab:
crontab -r
This removes all cron jobs for the current user.
Instead of specifying the exact time for a cron job, you can use special strings:
@reboot: Run once, at startup.@yearly: 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: Run once a day (0 0 * * *).@hourly: Run once an hour (0 * * * *).
Example:
@daily /path/to/daily_task.shThis runs daily_task.sh every day at midnight.
While cron is ideal for systems that run continuously, anacron is better suited for systems that are not running 24/7. anacron ensures that scheduled tasks are executed even if the system was powered off when the task was originally scheduled.
anacron jobs are defined in /etc/anacrontab and follow this syntax:
period delay job-identifier command
- period: Specifies the frequency (
1for daily,7for weekly,30for monthly). - delay: Delay in minutes after
anacronstarts to execute the command. - job-identifier: A unique identifier for the job.
- command: The command to execute.
1 5 daily_backup /path/to/backup.shThis runs backup.sh daily, 5 minutes after anacron starts.
System-wide cron jobs are defined in /etc/crontab or in /etc/cron.d/. These files include an additional field that specifies the user under which the command should be executed.
0 4 * * * root /usr/local/bin/system_backup.shThis schedules a system backup every day at 4 AM, run by the root user.
Cron jobs typically log their output to /var/log/syslog or a similar system log file, depending on your distribution.
-
View cron logs:
grep CRON /var/log/syslog
This filters the log file for cron-related entries.
If a cron job is not running as expected:
- Ensure the command works: Run the command manually in the terminal to confirm it works.
- Check for environment issues: Cron jobs run with a minimal environment. Set the full path to executables and any necessary environment variables.
- Redirect output: Capture output and errors in a log file to diagnose issues.
0 2 * * * /path/to/script.sh > /path/to/logfile.log 2>&1Using cron and anacron effectively can greatly enhance your ability to automate tasks in Linux. Whether for simple maintenance scripts or complex backup routines, understanding these tools is essential for efficient system administration.
Next: System Monitoring and Logging
Previous: Process Management