Linux Crontab Command Format and detailed example

Source: Internet
Author: User

Basic Format:
* *** Command
Hour, day, month, and week commands

The 1st column indicates minute 1 ~ 59. Each minute is represented by * or */1.
The first column indicates the hour 1 ~ 23 (0 indicates 0 points)
The 3rd column indicates the date 1 ~ 31
The 4th column indicates the month 1 ~ 12
The Identification Number of column 5th is from day of the week to day ~ 6 (0 indicates Sunday)
6th columns of commands to run

Some examples of crontab files:

30 21 ***/usr/local/etc/rc. d/Lighttpd restart
The preceding example indicates restarting Apache at every night.

45 4, 10, 22 **/usr/local/etc/rc. d/Lighttpd restart
The preceding example indicates that Apache is restarted at on the 1st, 10th, and 22th every month.

10 1 ** 6, 0/usr/local/etc/rc. d/Lighttpd restart
The preceding example indicates that Apache is restarted at every Saturday and Sunday.

0, 30 18-23 ***/usr/local/etc/rc. d/Lighttpd restart
The preceding example indicates that Apache is restarted every 30 minutes between and every day.

0 23 ** 6/usr/local/etc/rc. d/Lighttpd restart
The preceding example indicates that Apache is restarted at every Saturday.

**/1 ***/usr/local/etc/rc. d/Lighttpd restart
Restart Apache every hour

* 23-7/1 ***/usr/local/etc/rc. d/Lighttpd restart
Restart Apache every hour between PM and PM.

0 11 4 * Mon-wed/usr/local/etc/rc. d/Lighttpd restart
Restart Apache on November 4 and every Monday to Wednesday.

0 4 1 Jan */usr/local/etc/rc. d/Lighttpd restart
Restart Apache at on January 1, January 1

 

Name: crontab

Permission: All Users

Usage:

Crontab file [-u user]-replace the current crontab with the specified file.

Crontab-[-u user]-replace the current crontab with the standard input.

Crontab-1 [user]-list the current crontab of the user.

Crontab-E [user]-edit the current crontab of the user.

Crontab-d [user]-delete the current crontab of the user.

Crontab-C Dir-specifies the crontab directory.

Crontab file format: m h d m d cmd.

 

M: minute (0-59 ).

H: hour (0-23 ).

D: Day (1-31 ).

M: Month (1-12 ).

D: days in a week (0 ~ 6, 0 is Sunday ).

The program to run in cmd is sent to SH for execution. This shell only contains three environment variables: User, home, and shell.

Note:

Crontab is used to allow users to execute programs at a fixed time or interval. In other words, it is similar to the user's time table. -U user is used to specify

The premise is that you must have the permission (for example, root) to specify the time table of another user. If-u user is not used, it indicates

Set your own time table.

 

 

Parameters:

Crontab-E: run the text editor to set the time table. The preset text editor is vi. If you want to use another text editor, set the visual environment variable first.

To specify the Text Editor (for example, setenv visual Joe)

Crontab-R: Delete the current time table

Crontab-L: to list the current time table

Crontab file [-u user]-replace the current crontab with the specified file.

The time table format is as follows:

F1 F2 F3 F4 F5 Program

F1 indicates the minute, F2 indicates the hour, F3 indicates the day of the month, F4 indicates the month, and F5 indicates the day of the week. Program indicates to execute

.

When F1 is *, the program is executed every minute. When F2 is *, the program is executed every hour, and so on.

When F1 is a-B, it indicates that execution is performed from the minute a to the minute B. When F2 is a-B, it indicates that execution is performed from the hour a to the hour B, and so on

When F1 is */N, it indicates execution is performed every n minutes. If F2 is */N, it indicates execution is performed every n hours, and so on.

When F1 is a, B, c ,... a, B, C ,... execute in minutes. F2 is a, B, c ,... a, B, c... execution in hours, and so on

You can also store all settings in the file first, and use crontab file to set the time table.

Example:

# Run/bin/ls at every morning:

0 7 ***/bin/ls

During October 11, December, execute/usr/bin/backup every three hours from to every day:

0 6-12/3*12 */usr/bin/backup

From Monday to Friday, send a letter to alex@domain.name at pm:

0 17 ** 1-5 mail-s "hi" alex@domain.name </tmp/maildata

Execute echo "Haha" at midnight, 00:20, and 02:20 every month"

20 0-23/2 *** echo "Haha"

Note:

When the program is executed at the specified time, the system will send you a letter showing the program execution content. If you do not want to receive such a letter, please leave each row Blank

Add>/dev/null 2> & 1.

 

Example 2:

#06:10 every morning

10 6 **** date

# Every two hours

0 */2 * Date

# Every two hours from PM to am, am

0 23-7/2, 8 **** date

# Am on the 4th day of each month and from Monday to Wednesday of each week

0 11 4 * Mon-wed date

# A.m. of July

0 4 1 Jan * Date

Example

$ Crontab-l list the current crontab of a user.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.