On Linux systems, cron daemon is a robust scheduling tool that executes commands or scripts at predefined times or intervals. This approach ensures repeated tasks, such as backups or log rotations, run automatically without user intervention. Bash scripts can be seamlessly integrated into these schedules for flexible and efficient automation.
The crontab file controls how cron daemon interprets scheduling instructions. Each line in a crontab entry follows a specific five-field syntax to represent minutes, hours, days of the month, months, and days of the week. This flexible structure accommodates everything from one-time tasks to complex recurring schedules.
Storing commands in crontab ensures consistent execution, even after reboots or user logouts. Environment variables within scheduled scripts can be set to define paths or custom parameters. Relying on cron daemon helps streamline system administration and simplify automation workflows.
$ systemctl status cron ● cron.service - Regular background program processing daemon Loaded: loaded (/lib/systemd/system/cron.service; enabled; vendor preset: enabled) Active: active (running) ...
$ crontab -e no crontab for user - using an empty one crontab: installing new crontab
The default editor may vary by distribution and can be changed by setting the EDITOR environment variable.
0 3 * * * /home/example/backup.sh
In this example, the script runs every day at 03:00.
$ crontab -l 0 3 * * * /home/example/backup.sh
$ grep CRON /var/log/syslog Mar 7 03:00:01 hostname CRON[12345]: (example) CMD (/home/example/backup.sh)
$ crontab -r crontab: removing crontab for example
Deleting the entire crontab file cannot be undone and removes all scheduled jobs.