Linux notes: crontab scheduled tasks

Source: Internet
Author: User

The server needs to process some tasks on a regular basis. Generally, the crontab of Linux can be used for processing, which can be accurate to minutes. There are also scheduled tasks in windows, but previously the network administrator said that this vulnerability exists, and win2003 servers are not enabled.

Crond is a Linux Command Used to regularly execute programs. 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. Linux task scheduling mainly includes the following two types:
1. work performed by the system: work to be performed periodically by the system, such as backing up system data and clearing Cache
2. 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.

There are just a few points in crontab:
Execution time, execution user, and execution content

1 execution time

Hour, day, month, day, week
If you do not need to specify a place, you can use * to represent it. Otherwise, it is the specified
You can also use */10 to specify every 10 units.

For example, */10 * Indicates execution every 10 minutes.

Reference Description of the task scheduling setting file
The 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. Specify to execute the ls command every 5th minutes of an hour.
5 * ls

2. Specify to execute the ls command at every day
30 5 * ls

3. Specify to execute the ls command at on October 8.
30 7 8 ** ls

4. Specify to execute the ls command at on January 1, June 8 each year.
30 5 8 6 * ls

5. Specify to execute the ls command at every Sunday.
30 6 ** 0 ls
Note: 0 indicates Sunday, 1 indicates Monday, and so on. sun indicates Sunday, mon indicates Monday, and so on.

6. Execute the ls command at on the 10th and 20th every month.
30 3 10, 20 ** ls
Note: "," is used to connect multiple discontinuous periods.

7. Execute the ls command at every day.
25 8-11 *** ls
Note: "-" is used to connect consecutive periods.

8. Execute the ls command every 15 minutes.
*/15 * ls
That is, the ls command is executed every hour for 0th 15 30 45 60 minutes.

9. Execute the ls command at every 10 days.
30 6 */10 ** ls
That is, execute the ls command once every month at on May 1, 11, 21, and 31.

10. Execute all executable files in the/etc/cron. daily directory as root at every day.
50 7 * root run-parts/etc/cron. daily
Note: The run-parts parameter indicates all executable files in the directory after execution.

New 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.

View scheduled tasks
Crontab-l // list all current scheduling tasks
Crontab-l-u jp // list all scheduling tasks of user jp

Delete Task Scheduling
Crontab-r // delete all task scheduling tasks

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 reads not only all files in/var/spool/cron every minute, but also/etc/crontab once. Therefore, we configure this file
You can use the cron service to do something. 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 // execute the script in/etc/cron. hourly every hour
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 be run, instead of the folder name.

The above content main reference article: http://blog.donews.com/shanxueyi/archive/2006/09/26/1047406.aspx

The crontab file has six fields separated by spaces or the tab key. The first five fields are integers or *. The format is as follows (the value range is in parentheses ):

Minute (0-59) hour (0-23) Day (1-31) month (1-12) Week (0-6) command to be executed

The week value 0 indicates Sunday, and the other values are recursive. In the first five domains, each domain can use * to indicate all valid values, or use a list to list valid values, separated, you can also use "-" to indicate a range. The following command demonstrates how to run/home/wuxp/backup. sh every week from Sunday to Thursday.

30 23 ** 0-4/home/wuxp/backup. sh

The following example shows how to clear core files in the user directory from Monday to Friday.

15 3 ** 1-5 find example $ HOME-name core 2>/dev/null | xargs rm-f

Restart the scheduled service of the OS to make the newly added task take effect.

Sun Solaris
#/Etc/rc2.d/S75cron stop
#/Etc/rc2.d/S75cron start
Linux
#/Etc/rc. d/init. d/crond restart

In this way, the system periodically executes the scheduled task.

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.