Linux Crontab: A Complete 5 minute tutorial

12 Sep 2018 0 32  0


Linux Crontab
Linux Crontab

In this tutorial, we will teach you how to use Linux crontab.

In a few easy steps, we will show you how to automate your system tasks using crontab in Linux.

Before going into details check out and recommended VPS hosting and WordPress hosting services

The Cron Software

The Cron software utility is a time-based job scheduler in Unix-like operating systems. Cron is driven by a crontab (crontable) file, a configuration file which specifies shell commands to run periodically on a particular schedule. The crontab files are going to be stored where the lists of jobs and other instructions to the cron daemon are kept.

Linux Crontab: Individual Crontab Files

Users are able to have their own individual crontab files and there is usually a system-wide crontab file (usually in ‘/etc’ or a subdirectory of ‘/etc’) that only system administrators are  able to modify.

You could use cron to automatically run scripts inside a specified period of time, create a backup of your databases or other important files, look at the services that are running on your server, and many other things. Let’s begin.

Connect to your server and update your system.

Before you start setting up crontab in Linux, connect to your VPS via SSH and update all of your system software to the latest version.

For Ubuntu systems, you just need to use the command below.

apt-get update && apt-get upgrade

For CentOS, you need to execute the following.

yum update

Check if cron is installed.

If you want to use the cron utility, you have to make sure that the cron package is installed on your server. For the Ubuntu server, execute the command below.

dpkg -l cron

On CentOs you can check with the following.

rpm -q cronie

Installing the Cron package.

If the cron package was not installed on your server, you can then install it using the package manager:

On Ubuntu.

apt-get install cron

On CentOS.

yum install cronie

Step 4: Confirm whether or not the cron service is running.

To see whether or not the cron service is running on your system, use the command below:

On Ubuntu.

systemctl status cron

On CentOS.

systemctl status crond

Configuring Cron jobs.

If you want to set up cron jobs, edit the ‘/etc/crontab’ file. Please remember that this file can only be modified by the root user.

You may modify the ‘crontab’ file with your text editor of choice, for example:

nano /etc/crontab

The content of this file will normally look like this.




# 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 has an explanation as to how you can define your own jobs. The syntax is the following:

minute hour day month day_of_week username command

Use of Asterisk

You may use an asterisk * in the ‘crontab’ so you can specify all valid values, so if you ever require a command to be run every day at midnight, you could append the cron job as shown below.

0 0 * * * root /sample_command >/dev/null 2>&1

Particular users could also create cron jobs. User-specific cron jobs are located in ‘/var/spool/cron/username’.

Once you make cron jobs for specific users, you will not have to specify the username in the cron job. The syntax for user-specific cronjobs will look like this:

minute hour day month day_of_week command

Linux Crontab examples.

Let’s see some more useful crontab examples.

For example, if you want to schedule a backup script to run every day at 6am, you can  set up the cron job as shown below.

0 6 * * * /path/to/script/

Or, for example, if you would like to schedule the backup every Sunday at 6pm, you can just set the cron job below.

0 18 * * sun /path/to/script/

You could also use some of the following timestamps:

@hourly path/to/script/

@daily path/to/script/

@weekly path/to/script/

@monthly path/to/script/

@reboot path/to/script/

This should schedule the cron job to be run at the beginning of every hour/day/week/month or upon server reboot.

Scripting and Cron job

If the script generates any type of output, including errors, you could set up the cron job to log this output into a different file. For example, the following cron is going to be run twice a day at 6am and 6pm 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/ > /path/to/logs/backup.log 2>%1

If you do not want any output to be generated, you can redirect both the standard error and the standard output to ‘/dev/null’ which is going to discard all information input into it.

0 6,18 * * mon,fri path/to/script/ > /dev/null 2>%1

Step 7: Restarting the cron service.

Once you make the changes to the crontab, you will have to restart the cron service using the command below:

On Ubuntu.

systemctl restart cron

On CentOS.

systemctl restart crond

Linux crontab manual.

If you want more information about cron, you could also check the man pages with:

man cron


man crontab

Here are a few hand-picked guides for you to read next:

  1. How to find package version in Linux
  2. Install Config Server Firewall (CSF) on a Linux VPS


Do let us know if you want to add any specific Linux, windows or CMS, or Security tutorial into this free tutorial directory.

Add a comment

Leave a Reply