Skip to content
Laviyx Learn

Try

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.

Intermediate5 min readUpdated
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#

Terminal
crontab -e     # edit
crontab -l     # list
crontab -r     # remove everything; be careful

The five time fields#

Each line has five time fields, followed by the command:

Text
┌───────── minute (0-59)
│ ┌─────── hour (0-23)
│ │ ┌───── day of month (1-31)
│ │ │ ┌─── month (1-12)
│ │ │ │ ┌─ day of week (0-6, Sunday is 0)
│ │ │ │ │
* * * * *  command
ScheduleMeaning
* * * * *Every minute
*/5 * * * *Every 5 minutes
0 * * * *Every hour, on the hour
30 2 * * *Every day at 02:30
0 9 * * 1-509:00, Monday to Friday
0 0 1 * *Midnight on the 1st of each month
0 6,18 * * *06:00 and 18:00 every day

Shortcuts 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:

Terminal
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
Output
* * * * * /home/asha/jobs/heartbeat.sh

After waiting a little over a minute, the job has run:

Terminal
sleep 70
cat ~/jobs/heartbeat.log | sort -u
Output
Ran as asha

Remove the job with crontab -r:

Terminal
crontab -r
crontab -l
Output
no crontab for asha

Save 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:

Text
0 2 * * * /home/asha/jobs/backup.sh >> /home/asha/jobs/backup.log 2>&1

cron 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:

Terminal
env -i PATH=/usr/bin:/bin HOME="$HOME" bash -c 'echo "PATH=$PATH"; echo "EDITOR=[$EDITOR]"'
Output
PATH=/usr/bin:/bin
EDITOR=[]

To avoid this:

  • Use full paths: /usr/bin/python3, not python3.
  • cd to 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 as date +\%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:

Terminal
ls /etc/cron.daily | head -n 3
Output
apache2
apport
apt-compat

systemd 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
Text
*/15 * * * * /home/asha/jobs/check.sh
30 18 * * 1-5 /home/asha/jobs/report.sh
0 3 1 * * /home/asha/jobs/monthly.sh

Key takeaway#

Next: Your First Bash Script.

Finished this lesson?