How to automate tasks with cron jobs on Linux
· Category: Linux
Short answer
Use crontab -e to edit your cron schedule. Each line defines a command to run at specified intervals using time and date fields. For writing the scripts that cron executes, see how to write bash scripts. For managing services that might run scheduled tasks, see how to use systemd.
Steps
- Open crontab:
crontab -e - Add a scheduled job, for example every day at 2 AM:
0 2 * * * /home/user/backup.sh - Save and exit the editor
- List jobs:
crontab -l - Check cron logs in
/var/log/syslogorjournalctl -u cron
Tips
- Use absolute paths in cron commands because PATH is limited
- Redirect output to a log file to capture errors:
>> /var/log/myjob.log 2>&1 - Set up how to configure SSH key-based login if cron jobs need to run commands on remote servers