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. The following examples illustrate the problem:
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.
Note the "run-parts" parameter. If this parameter is removed, you can write a script name to be run, instead of the folder name.
--------------------------------------
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 above example indicates restarting Lighttpd at every night.
45 4, 10, 22 **/usr/local/etc/rc. d/Lighttpd restart
The preceding example indicates that the Lighttpd is restarted at on the 1st, 10th, and 22th of each month.
10 1 ** 6, 0/usr/local/etc/rc. d/Lighttpd restart
The preceding example indicates restarting Lighttpd at every Saturday and Sunday.
0, 30 18-23 ***/usr/local/etc/rc. d/Lighttpd restart
The preceding example indicates restarting Lighttpd every 30 minutes between and every day.
0 23 ** 6/usr/local/etc/rc. d/Lighttpd restart
The preceding example indicates restarting Lighttpd at every Saturday.
**/1 ***/usr/local/etc/rc. d/Lighttpd restart
Restart Lighttpd every hour
* 23-7/1 ***/usr/local/etc/rc. d/Lighttpd restart
Restart Lighttpd every hour between PM and PM.
0 11 4 * Mon-wed/usr/local/etc/rc. d/Lighttpd restart
Restart Lighttpd from every Monday to Wednesday on the 4th of each month.
0 4 1 Jan */usr/local/etc/rc. d/Lighttpd restart
Restart Lighttpd at on January 1, January 1.
Another article
Introduction
Crontab-operate the daemon of each userProgramAnd the execution schedule.
Some parameter descriptions
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.
Crontab usage --- Linux scheduled task
Step 1:
Run the crontab-e command to edit a scheduled task or create a task with the same user name in the/var/spool/cron/crontabs directory.
That is:
# Execute the background program tmpmail once every minute
* ***/Home/milch/tmpmail
By the way, the command format in crontab is as follows:
The first * indicates the minute (0-59 ),
The second * indicates the hour (0-23 ),
The third * Indicates a day in the month (1-31 ),
The fourth * Indicates a month in a year (1-12 ),
The fifth digit * indicates the day of the week (0-6 with 0 = Sunday ).
Step 2:
Stop the cron process and restart it,
Command:/etc/init. d/cron stop
/Etc/init. d/cron start
-------------------------------------------------------------------------
Command syntax
Crontab [-u user] File
Crontab [-u user] {-L |-r |-e}
Instructions
Crontab allows you to execute custom programs, system commands, or shell secrip at a fixed interval. The time interval can be any combination of minutes, hours, days, weeks, months, and above. The user is allowed to perform the operation offline, and the execution result can be emailed to the user. Therefore, non-permanent joint work on periodic management analysis or data backup.
Basically, the crontab command format is divided into six parts, the first five are time intervals, and the last is the executed command. Each part is separated by spaces.
Minute-0-59
Time -- 0-23
Day-1-31
Month-1-12 or English name
Week -- 0-7 or use an English name
Command-command, shell script, program... (absolute path is recommended)
The preceding figure shows the basic format of crontab.
Option description
-U user
Run crontab as the specified user identity. This option is only for root users.
-L
Displays the user's current crontab file.
-R
Remove the current crontab file.
-E
Go to VI to edit the crontab file. (If you have set the visual or editor environment variable, how can you use the editor set by the environment variable to edit it ). After the user exits the editor, the edited crontab file is automatically placed into the crontab file for execution.
Related Files
/Etc/cron. allow
/Etc/cron. deny
Instance description
# Crontab-l
# Do not edit this file-edit the master and reinstall.
#(/Tmp/crontab.3672 installed on Thu jan1 15:55:18 2004)
# (Cron version -- $ ID: crontab. C, V 2.13 1994/01/17 03:20:37 vixie exp $)
0 0-23/6 ***/usr/bin/Webalizer
30 3 ***/root/fbin/Bak-Web
#
As mentioned previously, the crontab format is divided into six parts. The first five are time parameters. In the above example, you will find that except for numbers and English names, the symbol "*" is used, which indicates the meaning of each unit, for example, 30 3 * indicates the day of the week of every month at 30 o'clock.
Time can be a single number or a few numbers connected by commas. See the following example.
30, 3, 12 ****/root/fbin/Bak-Web
The second item is 3, 12, which indicates 3 and 12 hours. Let's take a look at the following example:
30 */6 ***/root/fbin/Bak-Web
I changed the second item to */6, which means that every 6 hours is equivalent to 6, 12, 18, and 24. There is also a CIDR Block
30 8-18/2 ***/root/fbin/Bak-Web
I changed the second item to 8-18/2, which means every 2 hours from 8 hours to 18 hours. hours, it is also equivalent.