Linux Cron scheduled task system commands
Classification: Linux VPs tutorial Author: achuan Release date: October 13,201 1
Many users need to use scheduled tasks after purchasing VPs. however, I do not know much about scheduled tasks. today, our help center provides you with some Linux tutorials on task scheduling.
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:
1.
/Sbin/service crond start
// Start the service
1.
/Sbin/service crond stop
// Close the service
1.
/Sbin/service crond restart
// Restart the service
1.
/Sbin/service crond reload
// Re-load the configuration
You can also enable the service automatically when the system starts:
Add:
1.
/Sbin/service crond start
Now that the C Ron 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:
1.
Crontab-u
// Set a user's cron service. Generally, the root user needs this parameter when executing this command.
1.
Crontab-l
// List the details of a user's cron Service
1.
Crontab-R
// Delete the cron service of no user
1.
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 the VI editing mode, the edited content must conform to the following format:
1.
*/1 ***** ls>/tmp/LS. TX t
Crond resident command for Task Scheduling
Crond is used for regular execution in Linux.Program. After the operating system is installed, the task scheduling command is started by default. The crond command periodically checks whether there is any job to be executed every minute. If there is any job to be executed, the job is automatically executed.
1. Linux task scheduling is divided into the following two types:
* Work performed by the system: the work to be performed periodically by the system, such as backing up system data and clearing Cache
* Personal work: the work that a user regularly performs, such as checking whether there are new emails on the email server every 10 minutes. This work can be set by each user.
2. crontab command options:
-U specifies a user,
-L list the task plans of a user,
-R: deletes a user's task,
-E. edit a user's task.
3. Cron File Syntax:
Hour, day, month, and week commands
0-59 0-23 1-31 1-12 0-6 command)
4. 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
I. 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, except that "command" is a field that must be specified each time, other fields are optional fields, which can be determined as needed. For unspecified fields, use "*" to fill their positions.
Example:
1.
5 * ls
// Specify to execute the LS command once every 5th minutes per hour
1.
30 5 * ls
// Specify to execute the LS command at every day
1.
30 7 8 ** ls
// Specify to execute the LS command at on the 8 th of every month
1.
30 5 8 6 * ls
// Specify to execute the LS command at on January 1, June 8 each year.
1.
30 6 ** 0 ls
// Specify to execute the LS command at every Sunday. [Note: 0 indicates Sunday, 1 indicates Monday, and so on. It can also be expressed in English, Sun indicates Sunday, and Mon indicates Monday. ]
1.
30 3 10, 20 ** ls
// Execute the LS command at on the 10th and 20th of every month [Note: "," is used to connect multiple discontinuous periods]
1.
25 8-11 *** ls
// Execute the LS command 25th minutes at every day [Note: "-" is used to connect consecutive periods]
1.
*/15 * ls
// Execute the LS command every 15 minutes [that is, execute the LS command every hour for 0th, 15, 30, and 60 minutes]
1.
30 6 */10 ** ls
// Execute the LS command at every 10 days in each month [that is, execute the LS command at on the 1st, 11th, 21st, and 31st of each month. ]
Run all executable files in the/etc/cron. daily directory as root at every day.
1.
50 7 * root run-parts/etc/cron. daily
// [Note: the Run-parts parameter indicates that all executable files in the directory after execution are executed. ]
Ii. New scheduling tasks
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.
3. View scheduling tasks
Crontab-l // list all current scheduling tasks
Crontab-l-u JP // list all scheduling tasks of user JP
4. Delete Task Scheduling
Crontab-r // delete all task scheduling tasks
V. Transition 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
1.
01 *** root run-parts/etc/cron. Hourly
// Execute the script in/etc/cron. Hourly every hour
1.
02 4 *** root run-parts/etc/cron. daily
// Execute the script in/etc/cron. daily every day
1.
22 4 ** 0 root run-parts/etc/cron. Weekly
// Execute the script in/etc/cron. Weekly every week
1.
42 4 1 ** root run-parts/etc/cron. Monthly
// Execute 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.
Example: 1. Enter crontab-E in the command line and add the corresponding task. Then, the WQ disk exits.
2. directly edit the/etc/crontab file, that is, VI/etc/crontab, and add the corresponding task.
1.
11 2 21 10 * Rm-RF/mnt/FB