We’ll show you, how to use Linux crontab. In a few simple steps we will explain, how to automate your system tasks using crontab in Linux. 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 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. Let’s start
1. Connect to your server and update your system
Before we begin with setting up crontab in Linux, let’s connect to your VPS via SSH and update all your system software to the latest version available.
On Ubuntu system, you can run the following command:
apt-get update && apt-get upgrade
and on CentOS you can run:
2. Check if cron is installed
In order to use the cron utility, you need to make sure that the cron package is installed on your server. On Ubuntu server, you can run the following command:
dpkg -l cron
On CentOS you can check with:
rpm -q cronie
3. Install the cron package
If the cron package is not installed on your server then you can install it with the package manager:
apt-get install cron
yum install cronie
4.Verify if the cron service is running
To check whether the cron service is running on your system, you can use the following command:
systemctl status cron
systemctl status crond
5. Configure cron jobs
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 contain an explanation about how to define your own 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. Linux 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 6 AM. We can then set up the following cron job:
0 6 * * * /path/to/script/backup-script.sh
Or for example if we want to schedule the backup every Sunday at 6 PM we can set the following cron job instead:
0 18 * * sun /path/to/script/backup-script.sh
We can also use some of the following timestamps:
@hourly path/to/script/backup-script.sh @daily path/to/script/backup-script.sh @weekly path/to/script/backup-script.sh @monthly path/to/script/backup-script.sh @reboot path/to/script/backup-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 twice a day at 6 AM and 18 PM every Monday and Friday and any output (standard and error) will be logged into the backup.log file :
0 6,18 * * mon,fri path/to/script/backup-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 6,18 * * mon,fri path/to/script/backup-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 command below:
systemctl restart cron
systemctl restart crond
8. Linux crontab manual
For more information about cron, you can also check the man pages with:
Of course you don’t have to use Linux crontab, if you use one of our managed Linux hosting, in which case you can simply ask our expert Linux admins to help you with crontab on Linux to Automate system tasks. They are available 24×7 and will take care of your request immediately.
PS. If you liked this post on Linux crontab, please share it with your friends on the social networks using the buttons on the left or simply leave a reply below. Thanks