How to setup crontab on Ubuntu 18.04

This post guide you how to setup crontab on Ubuntu 18.04 but you can apply on different versions of Ubuntu.

First of all, edit crontab file at:

vi /etc/crontab

You will see something like this

#/etc/crontab: system-wide crontab
# Unlike any other crontab you don't have to run the `crontab'
# command to install the new version when you edit this file
# and files in /etc/cron.d. These files also have username fields,
# that none of the other crontabs do.
SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
# m h dom mon dow user  command
17 *    * * *   root    cd / && run-parts --report /etc/cron.hourly
25 6    * * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.daily )
47 6    * * 7   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.weekly )
52 6    1 * *   root    test -x /usr/sbin/anacron || ( cd / && run-parts --report /etc/cron.monthly )

This is the default content of crontab file. Now you can add more schedules with syntax guide can be found at: Config9.com crontab guides

For example, to add a schedule runs at 1h 00 AM everyday under config9 username, you can add this line to that opened file:

0 1 * * * config9 /opt/app/app.sh

More templates, more guides will be at: Config9.com crontab guides.

After saving the file, we need to restart cron to schedules take effect:

# Check command
systemctl status cron

# restart command
systemctl restart cron

If you don’t have cron in your system, somehow, you will need to install cron with this command:

# Install cron if not available
sudo apt install -y cron

To check if cron is installed or not, using this command:

dpkg -l cron
Related:  Crontab run every day at 1 am and 3 pm

Leave a Reply

Your email address will not be published. Required fields are marked *