1, Introduction to crontab
1. crontab is used to allow users to execute programs at fixed times or intervals
If you need to implement the task scheduling function on linux platform, you can write cron script to implement it.
Perform tasks at a certain frequency
linux will start the crond process by default. The crond process does not need to be started or closed by the user.
The crond process is responsible for reading and executing the scheduling tasks. Users only need to write the corresponding scheduling script into the cron scheduling configuration file.
cron's scheduling files are as follows:
If the task used is not executed in hourly monthly weekly mode, you can write the corresponding crontab to crontab or cron D directory.
Example: Execute the script every other minute /opt/bin/test.sh Can be in cron.d New script echo-date.sh The content is */1 * * * * root /opt/bin/test.sh
2. Cron is a timed execution tool under Linux, which can run jobs without human intervention. Because cron is a built-in service of Linux, if it doesn't work automatically, you can start and close the service in the following ways:
/sbin/service crond start //Start service /sbin/service crond stop //Shut down service /sbin/service crond restart //Restart service /sbin/service crond reload //service crond reload
You can also start this service automatically when the system starts:
In / etc / RC d/rc. Local add the following at the end of the script:
/sbin/service crond start
Now that Cron is already in the process, we can use this service
Check whether the service is running. Use ps -ax | grep cron
2, crontab command
To check whether the crontab service under the user is created successfully, use the crontab -l command
Command which php to view the path of php installed under linux
Cron service provides crontab command to set the of cron service. Here are some parameters and descriptions of this command:
crontab -u //Set the cron service of a user. Generally, root users need this parameter when executing this command crontab -l //List the details of a user's cron service crontab -r //Delete cron service without users crontab -e //Edit a user's cron service
For example, root can view its own cron settings: crontab -u root -l
For another example, root wants to delete the cron setting of fred: crontab -u fred -r
When editing the cron service, the edited content has some formats and conventions. Enter: crontab -u root -e
Enter vi editing mode, and the edited content must conform to the following format: * / 1 * * ls > > / TMP / LS txt
crond resident command of task scheduling is a command used by linux to execute programs regularly. When the installation of the operating system is completed, the system will be started by default
Task scheduling command. The crond command will periodically check whether there is any work to be executed every minute. If there is any work to be executed, it will automatically execute the work.
1. crontab command options:
-u Specify a user -l List a user's task plan -r Delete a user's task -e Edit a user's task
2. cron file syntax:
Hourly day month week order
0-59 0-23 1-31 1-12 0-6 command (value range, 0 means Sunday, generally one line corresponds to one task)
Remember the meaning of several special symbols: "*"Represents the number within the value range, "/"Every "representative",If every 2 minutes:*/2 "-"Represents from a number to a number, ","Separate several discrete numbers
3. Writing method of task scheduling setting file
The crontab -e command can be used to edit the cron file of the corresponding user under / var/spool/cron. You can also directly change the / etc/crontab file. The specific format is as follows:
Minute Hour Day Month Dayofweek command minute Hour day month Days per week command The meaning of each field is as follows: Minute Perform the task in the first few minutes of each hour Hour Perform the task in the first few hours of each day Day Perform the task on the day of each month Month What month of the year is the task performed DayOfWeek Perform the task on the day of the week Command Specify the program to execute
In these fields, except that "Command" is a field that must be specified every time, other fields are optional fields, which can be determined as needed. For unspecified fields, fill their positions with "*".
Examples are as follows: 5 * * * * ls Specify to execute every 5 minutes of the hour ls command 30 5 * * * ls Specify 5 days per day:30 implement ls command 30 7 8 * * ls It is designated to execute at 7:30 on the 8th of each month ls command 30 5 8 6 * ls It is designated to be implemented at 5:30 on June 8 every year ls command 30 6 * * 0 ls Specify 6 days of every Sunday:30 implement ls command[Note: 0 means Sunday, 1 means Monday, and so on. It can also be expressed in English, sun Means Sunday, mon Monday, etc.] 30 3 10,20 * * ls At 3:30 on the 10th and 20th of each month ls command[Note: "," is used to connect multiple discontinuous periods] 25 8-11 * * * ls 8 per day-11 The 25th minute of point ls command[Note:“-"Used to connect consecutive periods] */15 * * * * ls Every 15 minutes ls command [That is, it is executed in the 0 15 30 45 60 minutes of each hour ls command ] 30 6 */10 * * ls Every month, every 10 days 6:30 Execute once ls command[That is, it is executed once at 6:30 on the 1st, 11th, 21st and 31st of each month ls Command. ] At 7:50 every day root Identity execution/etc/cron.daily All executables in the directory 50 7 * * * root run-parts /etc/cron.daily [ Note: run-parts Parameter indicates that all executable files in the following directory are executed. ]
4. New scheduling
There are two ways to add a scheduling task: 1),Enter at the command line: crontab -e Then add the corresponding task, wq Save and exit. 2),Direct editing/etc/crontab File, i.e vi /etc/crontab，Add the corresponding task.
5. View scheduled tasks
crontab -l //List all current scheduled tasks crontab -l -u jp //List all scheduling tasks of the user jp
6. Delete task schedule
crontab -r //Delete all task scheduling jobs
7. Turn of task scheduling execution results
Every day: 5:30 ls Command and output the results to/jp/test In the file 30 5 * * * ls >/jp/test 2>&1 Note: 2>&1 Indicates the execution result and error message.
8. Edit the / etc/crontab file to configure cron
The cron service not only reads all the files in / var/spool/cron once a minute, but also needs to read / etc/crontab once. Therefore, we can also use the cron service to do some things by configuring this file.
Configuring with crontab is for a user, while editing / etc/crontab is for the system. The file format of this file is:
SHELL=/bin/bash PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root //If there is an error or data output, the data will be sent to this account as an email HOME=/ //The path where the user runs. This is the root directory # run-parts 01 * * * * root run-parts /etc/cron.hourly //Execute / etc / cron. Every hour Script in hourly 02 4 * * * root run-parts /etc/cron.daily //Execute / etc / cron. Every day Script in daily 22 4 * * 0 root run-parts /etc/cron.weekly //Execute / etc / cron. Every week Script in weekly 42 4 1 * * root run-parts /etc/cron.monthly //Execute / etc / cron. Every month Scripts in monthly
You should pay attention to the "run parts" parameter. If you remove this parameter, you can write the name of a script to run instead of the folder name
For example: 1) Enter at the command line: crontab -e Then add the corresponding task, wq Save and exit. 2)Direct editing/etc/crontab File, i.e vi /etc/crontab，Add corresponding task 11 2 21 10 * rm -rf /mnt/fb
9. Considerations when planning to execute PHP scripts
Because php scripts may contain (require,include), relative paths and absolute paths, it is best to cd first and then execute.
As follows: */10 * * * * cd /var/www/cron && /usr/bin/php /var/www/cron.php
PS: make a record when using it for reference only!