Linux crontab format and crontab examples

Linux crontab command is a command used to periodically execute programs. It uses the job scheduler cron to execute tasks.

This article shares the Linux crontab command format, crontab examples, syntax, related commands, and how to use crontab commands from it.

crontab format


 +---------------- minute (0 - 59)
 |  +------------- hour (0 - 23)
 |  |  +---------- day of month (1 - 31)
 |  |  |  +------- month (1 - 12)
 |  |  |  |  +---- day of week (0 - 6) (Sunday=0)
 |  |  |  |  |
 *  *  *  *  *  command to be executed

Crontab command consists of: time + action, where time is divided into minutes, hours, days, months, and weeks.

The time operators are:

  • * all numbers in the range of values
  • , hash value, for example, “1,3,6”
  • – Range of values, for example “2-4” means “2, 3, 4”
  • / Specify the interval frequency of the time, for example, “0-23/2” means to execute every two hours; for example, */10, if used in the minute field, it means to execute every ten minutes.

crontab options

crontab — maintain crontab files for individual users

  • -u Specify the name of the user whose crontab is to be tweaked.
  • -l Display the current crontab on standard output.
  • -r Remove the current crontab.
  • -e Edit the current crontab using the editor specified by the VISUAL or EDITOR environment variables.

crontab examples

1. crontab every minute execute

In the following crontab example, the command “myCommand” is executed every minute.

* * * * * myCommand

2. crontab every five minutes execute

In the following crontab example, the command “myCommand” is executed every 5 minutes.

*/5 * * * * myCommand

3. Execute on 10th minute and 40th minute on Sundays from 5 am to 7 am

In the following crontab example, it means that the command “myCommand” will be executed on the 10th and 40th minutes of every Sunday from 5am to 7am

10,40 5-7 * * 0 myCommand

4. 21:30 every night

30 21 * * * myCommand

5. crontab every two hours execute

In the following crontab example, the command “myCommand” is executed every two hours.

* */2 * * * myCommand

OK.

Linux crontab format, syntax and common examples of crontab are introduced. Have you got it?

Add a Comment

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