Crontab Examples: Running Cron Jobs in Linux

In Linux, we can configure the execution of a regular task, also known as a cron job , using the service cron crond.

Service cron crond, reads crontab(cron-a table) and executes scheduled tasks.

In this article I will show the formatcrontaband explain how to schedule the launch of commands or scripts in Linux.

You will also find here such common examples of schedules in crontab, like running a command or script every minute , every 5 minutes , every hourevery day and many others.

The first step is to make sure that the service cron crond is started.

Find out if the service is running cronin Ubuntu :

– or –

Find out if the service is running crondin CentOS :

– or –

Also, you need to make sure that the service croncrond) is started when the system starts.

Tip: Do you know how to add a service to startup in Ubuntu or CentOS? It’s easy! Read more →

As soon as the service croncrond) is started and is in startup – you can schedule a cron job.

Configuring Crontab

Run the following command to open the crontabcurrent user:

Open crontabAlice:

View the contents of the crontabcurrent user and Alice user:

Useful Information: By default, the user jobs for the cron scheduler are stored in the directory /var/spool/cron/.

Task Scheduling Format in Crontab

Each scheduled task is described by a single line and determines the start time for the job and the cron job itself (command or script) to be executed.

To set the time, you can use the specific value of the minute , hour , day of the month , month and day of the week .

Instead of specific values, you can use a character *.

Scheme for better understanding of the format crontab:

A timestamp can be an integer value , multiple values , a range , a fraction or a fractional range .

Examples of timestamps for column hour :

Value A type Description
9 Integer value Perform at 9am
6,7,10 Multiple values Perform at 6, 7 and 10am
6-9 Range Perform every hour between 6-9 AM (inclusive)
*/2 Fraction Perform every 2 nd hour, i.е. 0 (midnight), 2am, 4am, 6am, and so on.
3-12/3 Fractional range Perform every 3rd hour between 3am and 12pm, i.e. 3am, 6am, 9am, 12pm

Good advice: Do you want to become a DevOps engineer? Then you must know Git! This article will help to really quickly master the basics of Git! Read more →

There are a number of predefined values that can be used to replace the execution time of a job:

Value Description Equivalent
@reboot Run when the operating system boots
@yearly Perform every year at midnight on January 1st 0 0 1 1 *
@annually Perform every year at midnight on January 1st 0 0 1 1 *
@monthly Perform monthly at midnight on the 1st day 0 0 1 * *
@weekly Perform at midnight every Monday 0 0 * * 0
@daily Perform every day at midnight 0 0 * * *
@midnight Perform every day at midnight 0 0 * * *
@hourly Perform at the beginning of each hour 0 * * * *

Crontab Examples

At the end of this post, I would like to provide some useful examples of schedules for running cron jobs.

I’m looking through these examples almost every time I need to add to crontaba command or script.

This table helps me a lot and I hope it helps you.

Here are the most common examples of cron job schedules that can be found in almost any crontabLinux environment:

Schedule The task
* * * * * echo “Run cron jobs every minute”
*/5 * * * * echo “Run cron job every 5 minutes”
*/30 * * * * echo “Run cron job every 30 minutes”
0 * * * * echo “Run cron jobs every hour”
0 */3 * * * echo “Run cron jobs every 3 hours”
0 13 * * * echo “Run the cron job every day at 1:00 pm”
30 2 * * * echo “Run cron job every day at 2:30”
0 0 * * * echo “Performing the task every day at midnight”
0 0 * * 0 echo “Run cron jobs every Sunday”
0 0 * * 1 echo “Run cron jobs every Monday”
0 0 1 * * echo “Run cron jobs on the first day of each month”
0 0 1 1 * echo “Run cron jobs every year of January 1”

Authored By Imran Yousaf

I am Imran Yousaf, a computer geek, founder of the site Smashinglab.com. I am a die hard fond of open-source software and Linux operating system. In addition to Linux, I am interested in everything related to information technology and modern science.

Leave a Reply

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