• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

SMASHING LAB

GUIDES | LINUX | GEEKY STUFF

  • Nginx
  • Linux
  • Guide
  • Ubuntu
  • WordPress
  • Drones, My Hobby

Crontab Examples: Running Cron Jobs in Linux

By Imran Yousaf

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 hour, every 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 :

1
systemctl status cron.service

– or –

1
service cron status

Find out if the service is running crondin CentOS :

1
systemctl status crond.service

– or –

1
service crond status

Also, you need to make sure that the service cron( crond) 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 cron( crond) is started and is in startup – you can schedule a cron job.

Configuring Crontab

Run the following command to open the crontabcurrent user:

1
$ crontab -e

Open crontabAlice:

1
$ crontab -u alice -e

View the contents of the crontabcurrent user and Alice user:

1
2
$ crontab -l
$ crontab -u alice -l

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:

1
2
3
4
5
6
7
.---------------- minute (0 - 59)
| | .-------------- hour (0 - 23)
| | | | .------------ day of the month (1 - 31)
| | | | | | .---------- month (1 - 12) OR jan, feb, mar ...
| | | | | | | | .-------- day of the week (0 - 6) (Sunday = 0 or 7) OR sun, mon, tue ...
| | | | | | | | | |
* * * * * command to execute

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”

Some more articles you might also be interested in …

  • 20 Awesome Nmap Command Examples for Linux
  • How to Convert JPG to PDF in Linux
  • Top 5 Linux Distributions for Programmers
  • How to Shutdown Linux System from a Terminal
  • How to Avoid Accidental File Deletion in Linux
  • Simple, Reliable and Convenient Monitoring of Linux Servers
  • How To Dual Boot Linux/Ubuntu and Windows 10…
  • How to Install LAMP Stack (Linux, Apache, MySQL,…
Tweet
Pin
Share
0 Shares

Filed Under: Guide

Primary Sidebar

  • Twitter
  • Facebook
  • Google+

Recent Posts

  • How to install Nextcloud on CentOS 8
  • How to install Zabbix on openSUSE Leap 15.1
  • How to Easily Configure Your Domain’s Email Account with Gmail?
  • Tails 4.4 OS Released with Tor Browser 9.0.6
  • How to Convert JPG to PDF in Linux

Subscribe to our Newsletter

Useful articles, tips and videos about creating and promoting websites to your mail



* required field


  • Contact Us
  • Privacy Policy
  • About Us

Copyright © 2021. Smashing Lab