Linux and Bash course menu
Linux cron Tutorial: Schedule Jobs with crontab
Automate tasks with cron: edit your crontab, read the five time fields with examples, log job output, avoid environment problems, and compare cron with systemd timers.
On this page
cron runs commands on a schedule: nightly backups, cleaning old files, sending reports. Each user has a list of jobs called a crontab.
Edit your crontab#
crontab -e # edit
crontab -l # list
crontab -r # remove everything; be carefulThe five time fields#
Each line has five time fields, followed by the command:
┌───────── minute (0-59)
│ ┌─────── hour (0-23)
│ │ ┌───── day of month (1-31)
│ │ │ ┌─── month (1-12)
│ │ │ │ ┌─ day of week (0-6, Sunday is 0)
│ │ │ │ │
* * * * * commandShortcuts such as @daily, @hourly and @reboot also work. The server's time zone is used, and crontab.guru is a useful site for checking schedules.
A real job#
Here we write a small script, and add it to the crontab from a script. Normally you would use crontab -e:
mkdir -p ~/jobs
cat > ~/jobs/heartbeat.sh <<'EOF'
#!/bin/bash
echo "Ran as $(whoami)" >> "$HOME/jobs/heartbeat.log"
EOF
chmod +x ~/jobs/heartbeat.sh
echo "* * * * * $HOME/jobs/heartbeat.sh" | crontab -
crontab -l* * * * * /home/asha/jobs/heartbeat.shAfter waiting a little over a minute, the job has run:
sleep 70
cat ~/jobs/heartbeat.log | sort -uRan as ashaRemove the job with crontab -r:
crontab -r
crontab -lno crontab for ashaSave the output#
cron normally tries to email a job's output, and on most servers that goes nowhere. Write it to a log file, including errors:
0 2 * * * /home/asha/jobs/backup.sh >> /home/asha/jobs/backup.log 2>&1cron has a minimal environment#
Most cron problems come from this. Jobs do not load your .bashrc, and PATH is very short. A script that works in your terminal may fail in cron:
env -i PATH=/usr/bin:/bin HOME="$HOME" bash -c 'echo "PATH=$PATH"; echo "EDITOR=[$EDITOR]"'PATH=/usr/bin:/bin
EDITOR=[]To avoid this:
- Use full paths:
/usr/bin/python3, notpython3. cdto the right folder inside the script.- Set what you need at the top of the crontab, such as
PATH=/usr/local/bin:/usr/bin:/bin. %is special in crontab lines, so write\%inside commands such asdate +\%F.
System cron#
As well as user crontabs, root can use /etc/crontab and /etc/cron.d/. Scripts in folders such as /etc/cron.daily/ run once a day:
ls /etc/cron.daily | head -n 3apache2
apport
apt-compatsystemd timers#
On modern systems, a timer is an alternative to cron. Timers are more work to set up, but they log to the journal, can catch up on runs missed while the machine was off, and can be controlled with systemctl. List them with systemctl list-timers.
Common mistakes#
Practice#
Practice exercise
Write the crontab lines for these: every 15 minutes, weekdays at 18:30, and the first day of each month at 03:00.
Show solution
*/15 * * * * /home/asha/jobs/check.sh
30 18 * * 1-5 /home/asha/jobs/report.sh
0 3 1 * * /home/asha/jobs/monthly.shKey takeaway#
Next: Your First Bash Script.
Finished this lesson?