A feature commonly used in Scheduled tasks, detailed below:
Start and stop of services
The cron service is a built-in service for Linux, but it does not boot automatically. You can start and stop a service with the following command:
Linux Code
/sbin/service Crond Start
/sbin/service Crond Stop
/sbin/service Crond Restart
/sbin/service Crond Reload
The above 1-4 lines are start, stop, restart service, and reload the configuration respectively.
To set up a cron to start automatically at boot time, add/sbin/service crond start to the/etc/rc.d/rc.local script.
View, edit, and delete
Cron stores the command line in a crontab (cron table) file, which is usually in the/etc directory. Each system user can have its own crontab (under/var/spool/cron/).
File uses the contents of the file as a cron service configuration for a user, and content is copied to the Cron service's configuration file
-U sets a cron service configuration for a user, which is typically required by the root user when executing this command
-l List A user cron service configuration
-R Delete a user's cron service configuration
-e Edit a user's cron service configuration, when the specified crontab does not exist, new, will use VI to open the configuration file for editing after saving exit, file syntax see
CRONTAB–V displays the time of the last edit (available only on some operating systems)
If you are currently root, to view/edit/delete/crontab a user, simply add-u USERNAME (such as crontab-e-U USERNAME) after the corresponding command.
The default editor for crontab files is VI, you can enter export visual= ' editor ' to change the default editor.
The Cron service will not only read all the files in the/var/spool/cron directory once per minute, but also read the/etc/crontab files once. Configuring this file also allows Cron to perform tasks. Using the crontab command is a configuration of user-level tasks, while editing/etc/crontab files is a configuration of system-level tasks.
Syntax description
Each line represents an instruction that represents a scheduled task; Each row consists of six spaces separated by a space, the first 5 representing the execution time of the scheduled task, and the remainder being the command to be executed by the scheduled task, in the following format:
Hour and Moon Week command
Minute Hour Day Month dayofweek command
Minute the task for the first few minutes of each hour, 0-59
Hour the task for the first few hours of the day, 0-23
Day of the month to perform the task, 1-31
Month a few months of the year to perform the task, 1-12
DayOfWeek the task on the first day of the week, 0-6,0 said Sunday
Command Specifies the program to be executed
The first five items above can use the following four special symbols:
* represents all values, such as the first digit used * for each minute
/means every 5 minutes per, as the first use of */5
-Represents a range of values, as the second digit uses 2-4 to indicate 2 to 4 points
That represents a discrete number of values, such as the 2nd digit using 6,8 to represent 6 points and 8 points
Specify "Step size": 8-14/2 indicates 8,10,12,14
Specify the list: for example, "1,2,3,4″," 0-4,8-12″
In each of the six fields in each row, except for the field that the command must specify each time, the other fields are optional and can be determined as needed. For fields that are not specified, use "*" to fill their position.
1 Remove the first five paragraphs, starting from the sixth paragraph to the line break or "%", for the command to be executed.
2 The default sixth paragraph will be/bin/sh execution unless a/bin/sh shell variable is specified in the crontab file.
3) In the sixth paragraph, "%", unless preceded by "", otherwise, "%" will end the command line, the meaning of line break, and subsequent content will be made as standard input sent to the previous command.
Name: crontab
Use Rights: All users
How to use:
crontab file [-u user]-replaces the current crontab with the specified files.
Crontab-[-u user]-replaces the current crontab with standard input.
crontab-1[user]-lists the user's current crontab.
crontab-e[user]-Edit user's current crontab.
crontab-d[user]-deletes the user's current crontab.
Crontab-c dir-Specifies the directory for crontab.
crontab file format: M H d M D cmd.
Examples are as follows:
Every Minute (* * * * * *)
Every 5 minutes (*/5 * * * *)
Twice an hour (0,30 * * * *)
Once an hour (0 * * * * *)
Twice a day (0 0,12 * * *)
Once a day (0 0 * * *)
Once a week (0 0 * * 0)
1st and 15th (0 0 1,15 * *)
Once a month (0 0 1 * *)
Once a year (0 0 1 1 *)
Linux Code
5 * * * * ls >/var/log.txt specifies that the LS command is executed at the first 5 minutes per hour and saves the results to the/var/log.txt file
5 * * * ls specifies 5:30 daily execution of LS command
7 8 * ls specifies 7:30 points per month 8th to execute LS command
5 8 6 * ls specifies every June 8 5:30 execution of the LS command
6 * * 0 ls Specifies 6:30 execution of the LS command per Sunday [Note: 0 for Sunday, 1 for Week 1, and so on, can also be expressed in English, Sun said Sunday, Mon said Monday. ]
3 10,20 * * ls 10th and 20th 3:30 Execute ls command [note: "," used to connect multiple discontinuous periods]
8-11 * * * ls 25 minutes per day at 8-11 o ' hour to execute the LS command [note: "-" to connect consecutive periods]
*/15 * * * * ls is executed once every 15 minutes by the LS command [that is, the No. 0 15 30 45 60 minutes per hour executes the LS command]
6 */10 * ls is executed once every 10 days, the LS command [that is 1, 11, 21, 31st of the month is 6:30 executes the LS command. ]
Cron configuration file
Using the crontab command to add a completed scheduled task will generate a user name file in the/var/spool/cron directory, which is your scheduled task, and the Cron service will read the files in/var/spool/cron every minute.
System planning Tasks
There is also a way to add scheduled tasks and edit/etc/crontab (the Cron service also reads/etc/crontab files every minute).
The crontab configuration is for a user, and editing/etc/crontab is a task for the system. The file format for this file is:
Linux Code
Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin
Mailto=root//If there is an error, or if there is data output, the data is sent to this account as mail
home=///user Run path, this is the root directory
# Run-parts
#每小时执行 scripts within the/etc/cron.hourly
* * * * Root run-parts/etc/cron.hourly
#每天执行 scripts within the/etc/cron.daily
4 * * * Root run-parts/etc/cron.daily
#每星期执行 scripts within the/etc/cron.weekly
4 * * 0 root run-parts/etc/cron.weekly
#每月去执行 scripts within the/etc/cron.monthly
4 1 * * Root run-parts/etc/cron.monthly
Note the "run-parts" parameter, if you remove this parameter, then write a script name to run, not the folder name. Example: 2 * RM-RF/MNT/FB
The following is an example of two cron statements (in the/etc/crontab file). The former is used for nightly backup of the/etc directory, which runs the analog program to process server statistics.
Linux Code
3 * * * Root tar czf/usr/local/backups/daily/etc.tar.gz/etc >>/dev/null 2>&1
5 * * * Root/usr/local/src/analog-5.32-lh/analog >>/dev/null 2>&1
The following is a description of the fields and fields in the CRON statement:
Field description
1 minutes (0-59)
2 hours (0-23)
3rd period (1-31)
April (1-12; or initials, Feb, etc.)
5 weeks (0-6,0 for Sunday; or word abbreviation sun, Mon, etc.)
6 username (as the user when executing the command)
7 commands to execute (PATH)
Now look at the first line:
Linux Code
3 * * * Root tar czf/usr/local/backups/daily/etc.tar.gz/etc >>/dev/null 2>&1
This statement will run the Tar czf/usr/local/backups/daily/etc.tar.gz/etc command at 3:12 A.M. (03:12) every day. >>/dev/null 2>&1 indicates that all standard output is sent to the/dev/null (the Linux Recycle Bin) and the standard error output (2) is sent to the same place (i.e./dev/null) as the standard output (1). Running this line of command will not produce any output.
This statement can be a little more complicated:
Linux Code
6 1 * Root tar czf/usr/local/backups/daily/etc.tar.gz/etc >>/dev/null 2>&1
It will run the tar czf/usr/local/backups/daily/etc.tar.gz/etc command at 15:30 Monday of June 13.
The following statements can achieve the same effect:
Linux Code
June Mon * Root tar czf/usr/local/backups/daily/etc.tar.gz/etc >>/dev/null 2>&1
If you want to run a program at the 15th minute of every hour as a user joey, you can use:
Linux Code
* * * * * Joey/usr/bin/somecommand >>/dev/null 2>&1
The asterisk (*) is a wildcard, which means that Cron will ignore this field.
If you want to run a program every two hours, you can use */2 in the hour field. It will be at 2 O ', 4, 6 ... 22 o'clock, 24 o ' work. The specific statements are as follows:
Linux Code
0 */2 * * * Joey/usr/bin/somecommand >>/dev/null 2>&1
A cron statement can also use commas (,) to specify more than one time. For example, if you want to run a program at 15 minutes and 30 minutes per hour, you can use 15,30 in the minute field:
Linux Code
15,30 * * * * Joey/usr/bin/somecommand >>/dev/null 2>&1
If you want to run a program every day for the first week of the month (that is, number 1th through 7th), you can use 1-7 in the Date field:
Linux Code
15,30 */2 1-7 * * Joey/usr/bin/somecommand >>/dev/null 2>&1
This statement will be two hours per month 第1-7 15 minutes and 30 points (02:15,02:30 ...). 22:15,22:30, etc.) to run the/usr/bin/somecommand command.
If you want to execute a collection of scripts at 16:18 a day, you can put all the scripts you want to execute into a directory (such as/home/username/cron), and you can use:
Linux Code
* * * Root Run-parts/home/username/cron >>/dev/null 2>&1
If you want to save the output of a program, you can replace the >>/dev/null 2>&1 with >>/home/user/somecommand.log 2>&1.
Some examples of crontab files:
* * * */usr/local/etc/rc.d/lighttpd restart
The above example represents 21:30 restart Apache per night.
4 1,10,22 * */USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example represents the 4:45 reboot of Apache for 1, 10, and 22nd per month.
1 * * 6,0/USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example shows the 1:10 reboot of Apache every Saturday and Sunday.
0,30 18-23 * * * */usr/local/etc/rc.d/lighttpd restart
The example above shows that Apache is restarted every 30 minutes from 18:00 to 23:00 every day.
0 * * 6/USR/LOCAL/ETC/RC.D/LIGHTTPD restart
The above example indicates that 11:00 PM restarts Apache every Saturday.
* */1 * * * */usr/local/etc/rc.d/lighttpd restart
Restart Apache every hour
* 23-7/1 * * * */usr/local/etc/rc.d/lighttpd restart
From 11 o'clock to 7 in the morning, restart Apache every hour.
0 4 * mon-wed/usr/local/etc/rc.d/lighttpd restart
4th per month with 11 points per Monday to Wednesday restart Apache
0 4 1/USR/LOCAL/ETC/RC.D/LIGHTTPD * Restart
Restart Apache at 4 o ' January 1.
Summarize
View the current user's cron configuration, using Crontab-l
Edit the current user's cron configuration using the CRONTAB-E
Deletes the current user's cron configuration, using the Crontab-r
View/edit/delete a user's cron configuration as root, plus-u USERNAME after the command
Configure system-level tasks to edit/etc/crontab files