Linux scheduled task settings

Source: Internet
Author: User

Address: http://blog.csdn.net/helisong427/article/details/7386231

Configuration steps:
1. the PHP interpreter must be compiled and installed in Linux. After installation, copy the $ {PHP}/bin/PHP file under the installation directory
/Usr/bin/, and use chmod + X./PHP to add executable permissions to it. OK,
Then enter PHP-Q *. php In the Linux Command Line.
Execute a PHP program to verify whether the interpreter works properly
2. Publish the PHP file: publish the compiled PHP program to the Apache publishing directory, for example,/var/www/html.
Add permission: chmod + x/var/www/html/*. php
3. Create a scheduled task:
(1) Start the Linux scheduled service: Service crond start
(2) view the current scheduled task: crontab-l
(3) Add a new scheduled task: crontab-e
Edit in the opened file:
*/2 *****/usr/bin/PHP-F/var/www/html/*. php
Indicates that the/var/www/html/*. php file is executed every 2 minutes.
(Note:-F is very important and cannot be changed to-Q)

Create a Cron service for the current user

1. Type crontab-e to edit the crontab service file.
For example, the file content is as follows:
*/2 *****/bin/sh/home/admin/jiaoben/buy/deletefile. Sh
Save the file and exit
*/2 *****/bin/sh/home/admin/jiaoben/buy/deletefile. Sh
*/2 ***** you can use this field to set when to execute the script.
/Bin/sh/home/admin/jiaoben/buy/deletefile. the sh field can be used to set the script to be executed. Note that bin/sh refers to the path where the script is stored when the command for running the script is followed.

2. Check whether the crontab service under the user is successfully created. Run the crontab-l command.

3. Start the crontab Service
Generally, the/sbin/service crond start is used to start the service. If the root user's cron service is used, the sudo service crond start can be used. Here, you should note that the commands of the services started by Linux systems of different versions are different, for example, in my virtual machine, you only need to use sudo service cron restart. If you type service cron start directly at the root, the service can be started.

4. Check whether the service is running with PS-ax | grep Cron
5. crontab command
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 the cron service of no user
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
Crond resident command for Task Scheduling
Crond is a Linux Command Used to regularly execute programs. After the operating system is installed
Task Scheduling command. Each worker of the crond command regularly checks whether there is any job to be executed.
The job is automatically executed.

6. crontab command options:
-U specifies a user
-L list the task plans of a user
-R: delete a user's task
-E. edit a user's task.
7. Cron File Syntax:
Hour, day, month, and week commands
0-59 0-23 1-31 1-12 0-6 command)
Remember the meanings of several special symbols:
"*" Indicates the number in the value range,
"/" Indicates "every ",
"-" Indicates a number to a number,
"," Separate several discrete numbers
8. Writing of the task scheduling setting file
You can use the crontab-e command to edit the cron file of the corresponding user in/var/spool/cron. You can also directly modify the/etc/crontab file.
The specific format is as follows:
Minute hour day month dayofweek command
Minute hour day week command
The meaning of each field is as follows:
Minute executes the task in minutes of each hour.
Hour executes the task the hour of the day
Day of each month
Month: The month of each year.
Dayofweek executes this task the day of the week
Command specifies the program to be executed
In these fields, all other fields except "command" must be specified each time are optional.
Field, depending on the needs. For unspecified fields, use "*" to fill their positions.
Example:
* Ls indicates that the LS command is executed every 5th minutes of an hour.
30 5 *** ls specifies to execute the LS command at every day
30 7 8 ** ls: Specify to execute the LS command at on the 8 th of every month.
30 5 8 6 * ls specifies to execute the LS command at on January 1, June 8 each year.
30 6 ** 0 ls specify to execute the LS command at every Sunday [Note: 0 indicates Sunday, 1 indicates Monday,
Similarly, Sun represents Sunday, mon represents Monday, and so on.]
30, 20 ** ls: Execute the LS command at on the 10th and 20th of every month [Note: "," is used to connect multiple discontinuous periods]
25 8-11 *** ls execute the LS command 25th minutes at every day [Note: "-" is used to connect consecutive periods]
*/15 * ls: Execute the LS command every 15 minutes [that is, execute the LS command every hour for 0th 15 30 45 60 minutes]
30 6 */10 ** ls: Execute the LS command at every 10 days. ]
Run all executable files in the/etc/cron. daily directory as root at every day.
50 7 * root run-parts/etc/cron. daily [Note: Run-parts parameter indicates that all executable files in the following directory are executed. ]

9. added a scheduling task.
There are two ways to add a scheduling task:
1) Enter crontab-E in the command line and add the corresponding task. The WQ disk will exit.
2) directly edit the/etc/crontab file, that is, VI/etc/crontab, and add the corresponding task.
10. View scheduling tasks
Crontab-l // list all current scheduling tasks
Crontab-l-u JP // list all scheduling tasks of user JP
11. Delete Task Scheduling
Crontab-r // delete all task scheduling tasks
12. redirection of task scheduling execution results
Example 1: Execute the LS command at every day and output the result to the/jp/test file.
30 5 * ls>/jp/Test 2> & 1
Note: 2> & 1 indicates the execution result and error message.
Edit the/etc/crontab file configuration Cron
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 // Run hourly
Scripts in/etc/cron. Hourly
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 run, instead of a folder name.

For example:
1) Enter crontab-E in the command line and add the corresponding task. The WQ disk will exit.
2) directly edit the/etc/crontab file, that is, VI/etc/crontab, and add the corresponding task
11 2 21 10 * Rm-RF/mnt/FB

Address: http://blog.csdn.net/helisong427/article/details/7386231

Configuration steps:
1. the PHP interpreter must be compiled and installed in Linux. After installation, copy the $ {PHP}/bin/PHP file under the installation directory
/Usr/bin/, and use chmod + X./PHP to add executable permissions to it. OK,
Then enter PHP-Q *. php In the Linux Command Line.
Execute a PHP program to verify whether the interpreter works properly
2. Publish the PHP file: publish the compiled PHP program to the Apache publishing directory, for example,/var/www/html.
Add permission: chmod + x/var/www/html/*. php
3. Create a scheduled task:
(1) Start the Linux scheduled service: Service crond start
(2) view the current scheduled task: crontab-l
(3) Add a new scheduled task: crontab-e
Edit in the opened file:
*/2 *****/usr/bin/PHP-F/var/www/html/*. php
Indicates that the/var/www/html/*. php file is executed every 2 minutes.
(Note:-F is very important and cannot be changed to-Q)

Create a Cron service for the current user

1. Type crontab-e to edit the crontab service file.
For example, the file content is as follows:
*/2 *****/bin/sh/home/admin/jiaoben/buy/deletefile. Sh
Save the file and exit
*/2 *****/bin/sh/home/admin/jiaoben/buy/deletefile. Sh
*/2 ***** you can use this field to set when to execute the script.
/Bin/sh/home/admin/jiaoben/buy/deletefile. the sh field can be used to set the script to be executed. Note that bin/sh refers to the path where the script is stored when the command for running the script is followed.

2. Check whether the crontab service under the user is successfully created. Run the crontab-l command.

3. Start the crontab Service
Generally, the/sbin/service crond start is used to start the service. If the root user's cron service is used, the sudo service crond start can be used. Here, you should note that the commands of the services started by Linux systems of different versions are different, for example, in my virtual machine, you only need to use sudo service cron restart. If you type service cron start directly at the root, the service can be started.

4. Check whether the service is running with PS-ax | grep Cron
5. crontab command
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 the cron service of no user
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
Crond resident command for Task Scheduling
Crond is a Linux Command Used to regularly execute programs. After the operating system is installed
Task Scheduling command. Each worker of the crond command regularly checks whether there is any job to be executed.
The job is automatically executed.

6. crontab command options:
-U specifies a user
-L list the task plans of a user
-R: delete a user's task
-E. edit a user's task.
7. Cron File Syntax:
Hour, day, month, and week commands
0-59 0-23 1-31 1-12 0-6 command)
Remember the meanings of several special symbols:
"*" Indicates the number in the value range,
"/" Indicates "every ",
"-" Indicates a number to a number,
"," Separate several discrete numbers
8. Writing of the task scheduling setting file
You can use the crontab-e command to edit the cron file of the corresponding user in/var/spool/cron. You can also directly modify the/etc/crontab file.
The specific format is as follows:
Minute hour day month dayofweek command
Minute hour day week command
The meaning of each field is as follows:
Minute executes the task in minutes of each hour.
Hour executes the task the hour of the day
Day of each month
Month: The month of each year.
Dayofweek executes this task the day of the week
Command specifies the program to be executed
In these fields, all other fields except "command" must be specified each time are optional.
Field, depending on the needs. For unspecified fields, use "*" to fill their positions.
Example:
* Ls indicates that the LS command is executed every 5th minutes of an hour.
30 5 *** ls specifies to execute the LS command at every day
30 7 8 ** ls: Specify to execute the LS command at on the 8 th of every month.
30 5 8 6 * ls specifies to execute the LS command at on January 1, June 8 each year.
30 6 ** 0 ls specify to execute the LS command at every Sunday [Note: 0 indicates Sunday, 1 indicates Monday,
Similarly, Sun represents Sunday, mon represents Monday, and so on.]
30, 20 ** ls: Execute the LS command at on the 10th and 20th of every month [Note: "," is used to connect multiple discontinuous periods]
25 8-11 *** ls execute the LS command 25th minutes at every day [Note: "-" is used to connect consecutive periods]
*/15 * ls: Execute the LS command every 15 minutes [that is, execute the LS command every hour for 0th 15 30 45 60 minutes]
30 6 */10 ** ls: Execute the LS command at every 10 days. ]
Run all executable files in the/etc/cron. daily directory as root at every day.
50 7 * root run-parts/etc/cron. daily [Note: Run-parts parameter indicates that all executable files in the following directory are executed. ]

9. added a scheduling task.
There are two ways to add a scheduling task:
1) Enter crontab-E in the command line and add the corresponding task. The WQ disk will exit.
2) directly edit the/etc/crontab file, that is, VI/etc/crontab, and add the corresponding task.
10. View scheduling tasks
Crontab-l // list all current scheduling tasks
Crontab-l-u JP // list all scheduling tasks of user JP
11. Delete Task Scheduling
Crontab-r // delete all task scheduling tasks
12. redirection of task scheduling execution results
Example 1: Execute the LS command at every day and output the result to the/jp/test file.
30 5 * ls>/jp/Test 2> & 1
Note: 2> & 1 indicates the execution result and error message.
Edit the/etc/crontab file configuration Cron
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 // Run hourly
Scripts in/etc/cron. Hourly
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 run, instead of a folder name.

For example:
1) Enter crontab-E in the command line and add the corresponding task. The WQ disk will exit.
2) directly edit the/etc/crontab file, that is, VI/etc/crontab, and add the corresponding task
11 2 21 10 * Rm-RF/mnt/FB

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.