Cron is a regular execution tool under Linux that can run jobs without human intervention. Because Cron is a built-in service for Linux, it does not automatically get up and you can start and close this service in the following ways:
/sbin/service crond Start//boot service
/sbin/service Crond Stop//Off service
/sbin/service crond Restart//Restart service
/sbin/service Crond Reload//Reload Configuration
You can also start this service automatically when the system is started:
At the end of the/etc/rc.d/rc.local script, add:
/sbin/service Crond Start
Now that the Cron service is in the process, we can use this service, and the Cron service provides the following interfaces for everyone to use:
1. Edit directly with crontab command
The Cron service provides the crontab command to set the Cron service, and here are some of the parameters and instructions for this command:
Crontab-u//Set a user's Cron service, which is required by the general root user when executing this command
CRONTAB-L//list details of a user's cron service
Crontab-r//Remove cron service for no users
CRONTAB-E//Edit a user's cron service
For example, root view your cron settings: Crontab-u root-l
Again for example, Root wants to delete Fred's cron settings: Crontab-u fred-r
When editing a cron service, there are some formatting and conventions for editing the content, input: Crontab-u root-e
into the vi editing mode, the contents of the edit must conform to the following format: */1 * * * * ls >>/tmp/ls.txt
Crond resident command for task scheduling
Crond is a command that Linux uses to execute programs on a regular basis. When the operating system is installed, the task Scheduling command is started by default. The Crond order will periodically check whether there is any work to be done and will automatically carry out the work if there is any work to be done.
1, Linux task scheduling is mainly divided into the following two categories:
* Work performed by the system: periodic work performed by the system, such as backing up system data, cleaning up the cache
* Work performed by a user on a regular basis, such as checking the mail server for new letters every 10 minutes, which can be set by each user.
2.crontab Command options:
-u Specifies a user,
-l lists a user's task schedule,
-R Deletes a user's task,
-e Editing a user's task
3.cron file Syntax:
Hour and Moon Week command
0-59 0-23 1-31 1-12 0-6 Command (value range, 0 for Sunday One for a row corresponding to a task)
4. Remember the meaning of several special symbols:
"*" represents the number in the range of values,
"/" stands for "every",
"-" represents from a number to a number,
"," separated by several discrete figures
I. The writing of task scheduling setup file
can be edited using the Crontab-e command, edited by the/var/spool/cron of the corresponding user's cron file, or directly modify the/etc/crontab file
The specific format is as follows:
Minute Hour Day Month dayofweek command
Minutes hours days month days per week order
Each field represents the following meanings:
Minute the first few minutes of each hour
Hour a few hours a day to perform this task
Day of the month to perform this task
Month a few months of the year to perform this task
DayOfWeek to perform the task on the first day of the week
Command Specifies the program to be executed
In these fields, except that the Command is a field that must be specified each time, the other fields are optional fields that can be determined as needed. For fields that are not specified, use "*" to fill their position.
Examples are as follows:
5 * * * * ls Specifies the first 5 minutes of every hour to execute the LS command
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. ]
Executes every executable file in the/etc/cron.daily directory as root 7:50 daily
7 * * * Root run-parts/etc/cron.daily [Note: run-parts parameter indicates that all executables in the following directory are executed. ]
Second, the new scheduling task
There are two ways to add new scheduling tasks:
1, at the command line input: CRONTAB-E and then add the corresponding tasks, Wq disk exit.
2, directly edit/etc/crontab file, that is, vi/etc/crontab, add the corresponding task.
Third, view the scheduling task
Crontab-l//List all current Scheduled tasks
Crontab-l-u JP//List all scheduling tasks for user JP
Iv. Delete Task scheduling work
Crontab-r//Delete all task scheduling work
V. Steering of the implementation results of task scheduling
Example 1: Execute the LS command 5:30 every day and output the result to the/jp/test file
5 * * * ls >/jp/test 2>&1
Note: 2>&1 represents execution results and error messages.
Edit/etc/crontab File Configuration cron
The cron service does not only have to read all the files in the/var/spool/cron every minute, but also read the/etc/crontab, so we can configure the file to do something with the cron service. The crontab configuration is for a user, and editing/etc/crontab is a task for the system. The file format for this file is:
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
* * * * * root run-parts/etc/cron.hourly//hourly execution of scripts within/etc/cron.hourly
4 * * * Root run-parts/etc/cron.daily//daily execution of/etc/cron.daily scripts
4 * * 0 root run-parts/etc/cron.weekly//per week execute/etc/cron.weekly script
4 1 * * Root run-parts/etc/cron.monthly//monthly to execute/etc/cron.monthly script
Attention to the "run-parts" This parameter, if you remove this parameter, then you can write a script to run the name, not the folder name
For example: 1, at the command line input: CRONTAB-E and then add the corresponding tasks, Wq disk exit.
2, directly edit/etc/crontab file, that is, vi/etc/crontab, add the corresponding task
2 * RM-RF/MNT/FB
Usage of the AT command for scheduled tasks in Linux (one-time scheduled tasks)
1. Command format:
at[parameters [TIME]
2. Command function:
Executes a specified task at a specified time, only once, and requires the ATD process to be turned on (Ps-ef | grep ATD View, open with/etc/init.d/atd start or restart, and start on that will need to run Chkconfig--level 2345 on).
3. Command parameters:
-M sends a message to the user after the specified task is completed, even if there is no standard output
Alias for-I ATQ
-D ATRM Alias
-V shows when the task will be executed
-C Print Task content to standard output
-V Display version information
-q< Queue > Use the specified queue
-f< file > read from the specified file to the task rather than from the standard input
-t< Time parameter > Submit a task to run as a time parameter
At allows you to use a fairly complex set of time-specific methods. Be able to accept the time specified in the hh:mm (Hours: minutes) of the day. If that time has passed, then put it on the second day of execution. Of course can also use Midnight (late at night), noon (noon), Teatime (tea time, usually 4 o'clock in the afternoon) and other vague words to specify the time. The user can also use a 12-hour clock, which means that it is morning or afternoon after the time plus AM (a.m.) or PM (PM). You can also specify a specific date for the execution of the command, specifying the format of Month Day (month) or mm/dd/yy (Month/day/year) or Dd.mm.yy (day. Month, year). The specified date must be followed by the specified time. It's all about absolute timing, but it's also possible to use relative timing, which is good for ordering orders that will be executed soon. The format specified is: Now + Count Time-units, then the current time, Time-units is the time unit, which can be minutes (minutes), hours (hours), Days (day), Weeks (week). Count is the amount of time, whether it is a few days, or a few hours, and so on. A more timed approach is to use today's, Tomorrow (Tomorrow's) to specify the time to complete the command.
Time: Date format, where you can define when to do the task at the time, the format is:
hh:mm
Ex> 04:00
At today's hh:mm time, if the time has passed, then tomorrow's hh:mm to carry out this task.
hh:mm YYYY-MM-DD
Ex> 04:00 2009-03-17
To enforce the task at a particular time of the day of the month of a certain year.
HH:MM[AM|PM] [Month] [Date]
Ex> 04pm March 17
It is the same as enforcing the task at a certain time of the month of a certain year.
HH:MM[AM|PM] + number [Minutes|hours|days|weeks]
Ex> now + 5 minutes
Ex> 04pm + 3 days
That is, a few extra hours at a point in time before the task is done.
4. Use instance:
Example 1: Three days after 5 o'clock in the afternoon execution/bin/ls
Command:
At 5pm+3
Output:
[Root@localhost ~]# at 5pm+3
At>/bin/ls
At> <EOT> Press Ctrl+d
Job 7 at 2013-01-08 17:00
[Root@localhost ~]#
Example 2: Tomorrow 17 o'clock, the output time to the specified file
Command:
At 17:20 tomorrow
Output:
[Root@localhost ~]# at 17:20 tomorrow
At> Date >/root/2013.log
At> <EOT> Press Ctrl+d
Job 8 at 2013-01-06 17:20
[Root@localhost ~]#
Example 3: Once the task is scheduled, we can use the ATQ command to see that the system is not performing a task before it is executed.
Command:
Atq
Output:
[Root@localhost ~]# ATQ
8 2013-01-06 17:20 a root
7 2013-01-08 17:00 a root
[Root@localhost ~]#
Example 4: Delete a task that has already been set
Command:
ATRM 7
Output:
[Root@localhost ~]# ATQ
8 2013-01-06 17:20 a root
7 2013-01-08 17:00 a root
[Root@localhost ~]# ATRM 7
[Root@localhost ~]# ATQ
8 2013-01-06 17:20 a root
[Root@localhost ~]#
Example 5: Display the task content that has been set
Command:
At-c 8
Output:
[Root@localhost ~]# at-c 8
#!/bin/sh
# Atrun Uid=0 gid=0
# Mail Root 0
Umask 22 omit n characters here
Date >/root/2013.log
[Root@localhost ~]#
5. ATD Startup and at run mode:
Start of 5.1 ATD
In order to use a one-time scheduled task, our Linux system must have the services in charge of this scheduling task, that is, the ATD service. But not all Linux distributions will turn him on by default, so at some point we need to manually activate the ATD service. The activation method is simple, and that's it:
Command:
/ETC/INIT.D/ATD start
/ETC/INIT.D/ATD restart
Output:
[Root@localhost/]#/etc/init.d/atd start
[Root@localhost/]#/ETC/INIT.D/ATD
Usage:/etc/init.d/atd {start|stop|restart|condrestart|status}
[Root@localhost/]#/etc/init.d/atd Stop
Stop atd:[OK]
[Root@localhost/]# Ps-ef|grep ATD
Root 25062 24951 0 14:53 pts/0 00:00:00 grep ATD
[Root@localhost/]#/etc/init.d/atd start
[OK]td:[OK]
[Root@localhost/]# Ps-ef|grep ATD
Root 25068 1 0 14:53? 00:00:00/usr/sbin/atd
Root 25071 24951 0 14:53 pts/0 00:00:00 grep ATD
[Root@localhost/]#/etc/init.d/atd Restart
Stop atd:[OK]
[OK]td:[OK]
[Root@localhost/]#
Description
/ETC/INIT.D/ATD start the ATD Service directly when the start is not started
Restart the ATD service after the/ETC/INIT.D/ATD restart service has started
Note: Configure to start this service at startup, so that you don't have to do it again every time you reboot.
Command:
Chkconfig ATD on
Output:
[Root@localhost/]# chkconfig ATD on
[Root@localhost/]#
How to run 5.2 at
Since it's a scheduled task, there should be a way in which tasks are executed, and these tasks are queued in the process table. So how does the way to create a scheduled task work? In fact, we use the AT command to produce the scheduled task to run, and the scheduled task is written to the/var/spool/at/directory as a text file, which can wait for the ATD and operation of the service. It's as simple as that.
However, not all people are allowed to do the at Schedule task. Why? Because of the system security reasons. Many hosts are known to attack the cracked, most often found that their system is more than a lot of hacker programs, these programs are very likely to use some planning tasks to run or collect your system running information, and regularly sent to hackers. So, don't let them use the AT command unless you have an approved account number. How do you achieve the controllable use of at?
We can use the two files/etc/at.allow and/etc/at.deny to limit the usage of at. After adding these two files, the at work is as follows:
First find/etc/at.allow This file, the user written in this file can use at, no users in this file can not use at (even if not written in the At.deny);
If the/etc/at.allow does not exist, look for the/etc/at.deny file, and if the user who wrote it in this at.deny cannot use at, without the user in the At.deny file, the AT command is available.
If two files do not exist, then only root can use the AT command.
Through this note, we know that/etc/at.allow is a more rigorous way to manage, while/etc/at.deny is looser (because the account is not in the file, it can run at). In general distributions, since all users on the system are assumed to be trustworthy, the system usually retains an empty/etc/at.deny file, meaning that everyone is allowed to use the AT command (you can check the file yourself). However, if you don't want some users to use at, write the user's account to/etc/at.deny! One account to write a line