In this tutorial, we will show you, how to use the Ubuntu crontab. In a few simple steps, we will explain, how to automate your system tasks on your Ubuntu server, using the crontab in Ubuntu. The cron software utility is a time-based job scheduler in Unix-like operating systems. Cron is driven by a crontab (cron table) file, a configuration file that specifies shell commands to run periodically on a given schedule. The crontab files are stored where the lists of jobs and other instructions to the cron daemon are kept. Users can have their own individual crontab files and often there is a system-wide crontab file (usually in /etc or a subdirectory of /etc) that only system administrators can edit.
You can use cron on Ubuntu to automatically run scripts within a specified period of time, create a backup of your databases or other important files, monitor the services running on your server and many other things. Follow the steps bellow to setup the Ubuntu crontab.
1. Connect to your server and update your system
Before we begin with setting up crontab on your Ubuntu server, let’s connect to your VPS via SSH and update your system software to the latest available version.
We can do this by executing the following command:
apt-get update && apt-get upgrade
2. Check if the cron package is installed
To use the cron utility, we need to make sure that the cron package is installed on your server.
To check if cron is installed, we can run the following command:
dpkg -l cron
3. Install the cron package on Ubuntu
If the cron package is not installed on your server then you can install it with the package manager:
apt-get install cron
4.Verify if the cron service is running
To check whether the cron service is running on your system, we can use the following command:
systemctl status cron
5. Configure cron jobs on Ubuntu
In order to set up cron jobs, you need to modify the /etc/crontab file. Please note that this file can only be modified by the root user.
You can edit the crontab file with your favorite text editor, for example:
The content of this file usually looks like this:
SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root # For details see man 4 crontabs # Example of job definition: # .---------------- minute (0 - 59) # | .------------- hour (0 - 23) # | | .---------- day of month (1 - 31) # | | | .------- month (1 - 12) OR jan,feb,mar,apr ... # | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat # | | | | | # * * * * * user-name command to be executed 37 * * * * root run-parts /etc/cron.hourly 23 5 * * * root run-parts /etc/cron.daily 19 3 * * 0 root run-parts /etc/cron.weekly 23 0 6 * * root run-parts /etc/cron.monthly
As you can see the crontab file already contains an explanation about how to define your own cron jobs. The syntax is the following:
minute hour day month day_of_week username command
An asterisk (*) in the crontab can be used to specify all valid values, so if you need a command to be executed every day at midnight, you can add the following cron job:
0 0 * * * root /sample_command >/dev/null 2>&1
Specific users can also create cron jobs. User-specific cron jobs are located in /var/spool/cron/username.
When you create cron jobs for specific users you do not need to specify the username in the cron job. The syntax for user-specific cronjobs should look like this:
minute hour day month day_of_week command
6. Ubuntu crontab examples
Let’s take a look at some more useful crontab examples.
Let’s say we want to schedule a backup script to run every day at 4:30 AM. We can then set up the following cron job:
30 4 * * * /path/to/script/backup-script.sh
Or for example if we want to schedule the backup on the first day of each month at 8 PM, we can set the following cron job instead:
0 18 1 * * /path/to/script/backup-script.sh
We can also use some of the following timestamps:
@hourly path/to/script/script.sh @daily path/to/script/script.sh @weekly path/to/script/script.sh @monthly path/to/script/script.sh @reboot path/to/script/script.sh
This will schedule the cron job to be executed at the beginning of each hour/day/week/month or upon server reboot.
If the scripts generate any kind of output, including errors, we can set up the cron job to log this output into a separate file. For example, the following cron will be executed three times a day at 4 AM, 10 AM and 16 PM every Wednesday and Saturday and any output (standard and error) will be logged into the backup.log file :
0 4,10,16 * * wed,sat path/to/script/script.sh > /path/to/logs/backup.log 2>&1
If we do not want any output to be generated we can redirect both the standard error and the standard output to /dev/null which will discard all the information written to it :
0 4,10,16 * * wed,sat path/to/script/script.sh > /dev/null 2>&1
7. Restart the cron service
After you make the changes to the crontab you will need to restart the cron service using the following command:
systemctl restart cron
8. Linux crontab manual
For more information about cron, you can also check the man pages with:
If it is difficult for you to set up correct cron jobs at the beginning, you can use some cron job calculator to generate the cron job expression. There are several good cron job calculators available on the Internet.
PS. If you liked this post on Ubuntu crontab and how to automate system tasks, please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks