Linux Crontab timing command

Source: Internet
Author: User
Tags cron script

I always think that timer is a better way to implement the timer. Later, I found out the disadvantage of timer only when I used Linux commands to create scheduled tasks, in timer, it is very likely that your task will be killed as a dead program ...... the last time a colleague accidentally used a timer task as a dead program to kill it, he reported an error to know that a timer was circulating. for many reasons, the following are excerpted content.

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.

Basic Format:

* *** Command

Hour, day, month, and week commands

M: minute (0-59 ). Each minute is represented by * or */1.

H: hour (0-23 ). (0 indicates 0)

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 set the time table of the specified user. 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, the time table is set.

Parameters:

Crontab-E: Execute 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 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 the program to be executed.

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, add>/dev/null 2> & 1 after each line is empty.

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.

/Usr/lib/cron. allow indicates who can use the crontab command. If it is an empty file, it indicates that no user can schedule a job. If this file does not exist and another file/usr/lib/cron. deny exists, the crontab command can be used only by users not included in this file. If it is an empty file, it indicates that any user can arrange jobs. If both files exist at the same time, cron. allow takes priority. If neither of them exists, only the Super User can schedule a job.

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

Example:

The time values are separated by commas.

In addition to numbers, there are also several special symbols: "*", "/", "-", * representing all numbers in the value range, "/" indicates the meaning of each. "*/5" indicates every five units. "-" indicates that a number is transferred to a number. "," separate several discrete numbers.

Every morning

0 6 *** echo "Good morning. ">/tmp/test.txt // note that no output is visible from the screen with pure echo, because cron has emailed any output to the root mailbox.

Every two hours

0 */2 *** echo "have a break now.">/tmp/test.txt

Every two hours from PM to am, am

0 23-7/2, 8 *** echo "have a good dream :)">/tmp/test.txt

Am from Monday 4 to Wednesday every week

0 11 4*1-3 command line

Am, January 1, January 1

0 4 1 1 * command line

After you edit the cron settings of a user, cron automatically generates a file with the same name under/var/spool/cron. The Cron information of this user is recorded in this file, this file cannot be edited directly. You can use crontab-e to edit it. The cron reads the file every minute after it is started, and checks whether to execute the commands in it. Therefore, you do not need to restart the cron service after the file is modified.

2. Edit the configuration cron in the/etc/crontab file.

The cron service not only reads all files in/var/spool/cron every minute, but also reads/etc/crontab once. Therefore, we can use the cron service to configure this file. Crontab configuration is intended for a user, and editing/etc/crontab is a system task. The file format of this file is:

Shell =/bin/bash

Path =/sbin:/bin:/usr/sbin:/usr/bin

Mailto = root // if an error occurs or data is output, the data is sent to this account as an email.

Home = // path of the user running. The root directory is used here.

# Run-Parts

01 *** root run-parts/etc/cron. Hourly // execute the script in/etc/cron. Hourly every hour

02 4 *** root run-parts/etc/cron. daily // run the script in/etc/cron. daily every day.

22 4 ** 0 root run-parts/etc/cron. Weekly // execute the script in/etc/cron. Weekly every week

42 4 1 ** root run-parts/etc/cron. Monthly // run the script in/etc/cron. Monthly every month.

Ii. cron timing

Cron is a Linux scheduled execution tool that can run jobs without human intervention. Since cron is a built-in service in Linux, but it does not automatically get up, you can use the following methods to start and close this service:

/Sbin/service crond start // start the service

/Sbin/service crond stop // close the service

/Sbin/service crond restart // restart the service

/Sbin/service crond reload // reload the configuration

You can also enable the service automatically when the system starts:

Add:

/Sbin/service crond start

Now that the cron Service is in the process, we can use it. The Cron Service provides the following interfaces for you to use:

1. directly use the crontab command to edit

The cron Service provides the crontab command to set the cron service. The following are some parameters and descriptions of this command:

Crontab-u // set a user's cron service. Generally, the root user needs this parameter when executing this command.

Crontab-l // list the details of a user's cron Service

Crontab-r // delete a user's cron Service

Crontab-E // edit a user's cron Service

For example, to view your cron settings as root: crontab-u root-l

For another example, Root wants to delete Fred's cron settings: crontab-u Fred-R

When editing the cron service, the edited content has some formats and conventions. Enter crontab-u root-e.

In VI editing mode, the edited content must conform to the following format: */1 ***** ls>/tmp/ls.txt

The first part of this format is the time setting, and the last part is the command to be executed. If there are too many commands to be executed, you can write these commands into a script, then you can directly call this script here. Remember to write the complete path of the command during the call. We have a certain agreement on the time setting. The first five * numbers represent five numbers. The value range and meaning of the numbers are as follows:

Minutes (0-59)

Hour (0-23)

Date (1-31)

Month (1-12)

Week (0-6) // 0 represents Sunday

In addition to numbers, there are also several special symbols: "*", "/", "-", * representing all numbers in the value range, "/" indicates the meaning of each, "*/5" indicates every five units, "-" indicates the number from a number to a number, "," separate several discrete numbers

Cron is easy to use: first, a quick one:
Step 1: Write the cron script file. For example, to name a crontest. cron text file, you only need to write one line:
, **** Echo "xgmtest ......"> xgmtest.txt
Indicates that the command is printed every 15 minutes.
Step 2: Add a scheduled task. Run "crontab crontest. cron ". Done
Step 3: Enter "crontab-L" to check whether a scheduled task exists.
Details:

Crontab usage
The crontab command is used to install, delete, or list tables used to drive cron background processes. That is to say, the user puts the command sequence to be executed into the crontab file for execution. Each user can have their own crontab file. The following describes how to create a crontab file.

Crontab files under/var/spool/cron cannot be directly created or modified. The crontab file is obtained through the crontab command. Assume that you have a username of foxy, and you need to create your own crontab file. First, you can use any text editor to create a new file, and then write the commands to be run and the time to be periodically executed to it.

Then save the disk and exit. Assume the file is/tmp/test. cron. Then, use the crontab command to install the file and make it the user's crontab file. Type:

Crontab test. cron

A crontab file is created. You can go to the/var/spool/cron directory and check that there is an additional foxy file. This file is the required crontab file. You can use the more command to view the file content and find that there are three lines of information in the file header:

# Do not edit this file-edit the master and reinstall.

# (Test. cron installed on Mon Feb 22 14:20:20 1999)

# (Cron version -- $ ID: crontab. C, V 2.13 1994/01/17 03:20:37 vivie exp $)

The approximate meaning is:

# Do not edit this file-if you need to change it, edit the source file and reinstall it.

# Test. Cron File Installation time: 14: 20: 20 02/22/1999

If you want to change the command content, you need to re-edit the original file and then use the crontab command to install it.

Users who can use the crontab command are limited. If/etc/cron. if the Allow file exists, only the listed users can use this command. If the file does not exist but cron. if the deny file exists, only users not listed in the file can use the crontab command. If neither file exists, it depends on the setting of some parameters, it is possible that only the super user is allowed to use this command, or that all users can use this command.

The syntax format of the crontab command is as follows:

Crontab [-u user] File

Crontab [-u user] {-l-R-e}

The first format is used to install a new crontab file and install the file referred to by the fade away variable ile. If the "-" symbol is used as the file name, it means the standard input is used as the installation source.

-U if this option is used, that is, the crontab file of the specified user will be modified. If this option is not specified, crontab is the crontab of the operator by default, that is, the crontab file of the user executing the crontab command will be modified. However, if you use the su command and then use the crontab command, there may be confusion. Therefore, if the su command is used, it is best to use the-u option to specify the user's crontab file.

-L display the current crontab on the standard output.

-R: Delete the current crontab file.

-E use the editor referred to by visual or editor environment variables to edit the current crontab file. After editing is completed, the edited files are automatically installed.

[Example 7]

# Crontab-L # list the current crontab of a user.

10 6 **** date

0/2 ** date

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

#

In the crontab file, enter the command and time to be executed. Each line in this file contains six fields, the first five of which specify the time when the command is executed, and the last field is the command to be executed. Each domain is separated by spaces or tabs. The format is as follows:

Minute hour day-of-month-of-year Day-of-week commands

The first item is the minute, the second item is the hour, the third item is the day of the month, the fourth item is the month of the year, and the fifth item is the day of the week, the sixth item is the command to be executed. These items cannot be blank and must be filled in. If you do not need to specify several items, you can use * instead. Because * is a unified character and can replace any character, it can be considered as any time, that is, this item is ignored. The valid range of each item is given in Table 4-1.

Table 4-1 valid time range

Time minute hour day-of-month-of-year Day-of-week
Valid value: 00-59 00-23 01-31 01-12 0-6 (0 is Sunday)

In this way, you can write unlimited lines to the crontab file to complete unlimited commands. All commands and symbols that can be written in the command line can be written in the Command domain, and other time domains can be listed, that is, many time values can be written in the domain, if any of these time values is met, execute the command, and use commas to separate each two time values.

In addition to numbers, there are also several special symbols: "*", "/", "-", * representing all numbers in the value range, "/" indicates the meaning of each, "/5" indicates every five units, "-" indicates the number from a number to a number, "," separate several discrete numbers.

Examples:

Every morning
0 6 *** echo "Good morning. ">/tmp/test.txt // note that no output is visible from the screen with pure echo, because cron has emailed any output to the root mailbox. Every two hours
0 */2 *** echo "have a break now.">/tmp/test.txt every two hours from PM to am, am
0 23-7/2, 8 *** echo "have a good dream :)">/tmp/test.txt, 4 of each month, and am from Monday to Wednesday of every week
0 11 4*1-3 command line, January 1, January 1
0 4 1 1 * command line shell =/bin/bash Path =/sbin:/bin:/usr/sbin:/usr/bin mailto = root // if an error occurs, or there is data output. The data is sent to the account home as an email = // path for the user to run, here is the root directory # Run-parts 01 ***** root run-parts/etc/cron. hourly // run/etc/cron hourly. hourly script 02 4 *** root run-Parts
/Etc/cron. daily // run/etc/cron every day. daily script 22 4 ** 0 root run-parts/etc/cron. weekly // run/etc/cron every week. weekly script 42 4 1 ** root run-parts/etc/cron. monthly // run/etc/cron every month. note the "run-parts" parameter for the script in monthly. If this parameter is removed, you can write a script name to be run later, instead of the folder name.
This indicates the calendar month of any day, in fact, the command is executed every day at four o'clock P.M., 15 min, 25 min, 35 min, 45 min, and 55 min.
, 17, 18 *** the system enters the maintenance status at every Monday, 3, and 5, and restarts the system. The following fields should be written to the crontab file:
00 15 **, 5 shutdown-R + 5 then save the file disk as Foxy. cron, and then type crontab Foxy. cron to install the file.
Execute the innd/bbslin command in the user directory at 10 and 40 hours:
*** Innd/bbslink executes the bin/account command in the user directory every hour:
1 *** bin/account execute the following two commands in the user directory at 03:20 every morning (each command is separated ):
20 3 *** (/bin/Rm-F expire. ls logins. Bad; bin/expire $ # @ 62; expire.1st)
Execute the/bin/Rm-F expire.1stcommand at 03:12 and 03:55 on July 4 and July 9, and set the result to the end of the mm.txtfile (the mm.txt file is located in your own directory ).
3 4-9 */bin/Rm-F expire.1stre%@62%%%%%@62%mm.txt

Crontab: 1 man cron 2 man crontab 3 man 5 crontab => 5 in the middle. Command "crontab-e": Export Path =/bin:/sbin:/usr/bin:/usr/sbin:/usr/x11r6/bin # This is a blind spot, I never thought about it, but I was disappointed again and again. [Colon separation] display =: 0.0
# Add a colon with a period. # Minute, day, month, week, command 1 23, 1 8 * shutdown-H + 3 # shutdown at on January 1, (delay of 3 minutes), 7 23 1 8*#1 minute, 4 points, 7 points [January 8] */3 23 1 8 * #, 6, 9... every 3 minutes (or every 3 minutes, a little different from every 3 minutes) [1-10]/3 23 1 8*#1 minute, 4 minutes, 7 minutes [from 1 minute, add three minutes one by one until the value is greater than 10 ==> this is the correct solution] @ reboot shutdown-k now # This line can scare you, shut down when the server is started (if "-k" is changed to "-h ).
"@ Reboot" indicates execution at startup. --------------------------------- End -------------------------------------------- another script: **************************************** ***************************************[ 0-59]/5 23 *** shutdown-H now # shut down every five minutes at every night, the desire is good, but it will not be executed, because no PATH variable is set. For the solution, see the downstream.
[0-59]/5 23 ***/sbin/shutdown-H now # The path is provided to ensure that it is shut down. 0 6 1 8 * xmms/music/zhangchu/lightweight # Call me a song at six o'clock tomorrow morning. However, I was disappointed that I still didn't hear the music until seven o'clock. [Display =: 0.0]. The last point of this line is added: currently, the 99% motherboard supports timed boot. If there is no alarm, you can ask the computer to wake you up. The option is [wake up by alarm] In the BIOS power supply. * End ****** *************************************
It can be freely modified and was initially released on the linuxsir website.
Let's look at a Super User's crontab file:
# Run the 'atrun' program every minutes # This runs anything that's due to run from 'at '. see man 'at' or 'atrun '., 20, 25, 30, 35, 40, 45, 50, 55 */usr/lib/Atrun 40 7 * updatedb 8, 10, 54,58 ****/bin/Sync
Detailed source reference: http://www.jb51.net/article/15008.htm

Related Article

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.