Schedule tasks to run regularly in Ubuntu 18.04 with crontab

Chi Thuc Nguyen
3 min readNov 16, 2018

--

The easiest way

Simply put shell scripts in one of these folders:

  • /etc/cron.daily
  • /etc/cron.hourly
  • /etc/cron.monthly
  • /etc/cron.weekly

Starting Cron Service

Almost all Linux distributions comes with pre-installed cron by default. You can check that with this command: dpkg -l cron

In case if it’s not installed, you can install it using following command:

sudo apt install cron
sudo service cron start
sudo service cron status

Get help

man crontab
man 5 crontab

Starting to Use Cron

To use cron for tasks meant to run only for your user profile, add entries to your own user’s crontab file. To edit the crontab file enter:

crontab -e

To schedule tasks that require administrative privileges (i.e. they are generally run using sudo), you should edit the root crontab:

sudo crontab -e

Crontab lines

Each line has five time-and-date fields, followed by a command, followed by a newline character. The fields are separated by spaces.

The syntax of crontab entry should be as follow:

* * * * * command to be executed
- - - - -
| | | | |
| | | | ----- Day of week (0 - 7) (Sunday=0 or 7)
| | | ------- Month (1 - 12)
| | --------- Day of month (1 - 31)
| ----------- Hour (0 - 23)
------------- Minute (0 - 59)

Comma-separated values can be used to run more than one instance of a particular command within a time period. Dash-separated values can be used to run a command continuously. For example:

01,31 04,05 1-15 1,6 * /usr/bin/somedirectory/somecommand

The above example will run /usr/bin/somedirectory/somecommand at 01 and 31 past the hours of 4:00am and 5:00am on the 1st through the 15th of every January and June.

Other examples:

# use /bin/bash to run commands, instead of the default /bin/sh
SHELL=/bin/bash
# mail any output to `paul', no matter whose crontab this is
MAILTO=paul
#
# run five minutes after midnight, every day
5 0 * * * $HOME/bin/daily.job >> $HOME/tmp/out 2>&1
# run at 2:15pm on the first of every month -- output mailed to paul
15 14 1 * * $HOME/bin/monthly
# run at 10 pm on weekdays, annoy Joe
0 22 * * 1-5 mail -s "It's 10pm" joe%Joe,%%Where are your kids?%
23 0-23/2 * * * echo "run 23 minutes after midn, 2am, 4am ..., everyday"
5 4 * * sun echo "run at 5 after 4 every sunday"
# Run on every second Saturday of the month
0 4 8-14 * * test $(date +\%u) -eq 6 && echo "2nd Saturday"

Five time-and-date fields can also be replaced with following special strings:

string         meaning
------ -------
@reboot Run once, at startup.
@yearly Run once a year, "0 0 1 1 *".
@annually (same as @yearly)
@monthly Run once a month, "0 0 1 * *".
@weekly Run once a week, "0 0 * * 0".
@daily Run once a day, "0 0 * * *".
@midnight (same as @daily)
@hourly Run once an hour, "0 * * * *".

TL; DR;

Validate and explain a crontab time

Go to https://crontab.guru/ and paste your five time-and-date fields there. Example:

Where are crontab files actually located?

User-specific crontab files are located /var/spool/cron/crontabs/ folder, with one file for each user.

cat /var/spool/cron/crontabs/{username}

List all cron jobs for all users

for user in $(cut -f1 -d: /etc/passwd); do echo $user; sudo crontab -u $user -l; done

Crontab Commands Usage

List cron jobs

To list the scheduled cron jobs for the currently logged in user:

crontab -l

If you want to display the cron jobs of another user:

crontab –l –u username

Edit cron jobs

See crontab -e above.

Remove all cron jobs

crontab -r

Important: remove all the cronjobs without asking for confirmation. Use -ir if you want to remove cronjobs interactively.

Logging

Generate a log file

0 * * * * /path/to/your/script >> /var/log/cron.log

Block output

0 * * * * /path/to/your/script > /dev/null 2>&1

To see log of cron jobs execution in syslog

grep 'CRON.*(yourusername)' /var/log/syslog

--

--