Linux Scheduled Tasks (reproduced)

Source: Internet
Author: User

Linux Scheduled Tasks (reproduced)

The planning task of Linux is an important part of the system management, and it is a way to realize the automatic work of the system, because of the planning task, we can fully realize the scripting and automation of system management.

With regard to scheduling tasks,Linux provides two services based on user needs, one for ATD Services and the other for Cron services

1.ATD Service

If your scheduled task is only executed once, then please make ATD service. By default, theLinux system turns on the ATD service. If you are not sure whether your Linux ATD service is turned on , use the following command to view:

#/ETC/INIT.D/ATD Status

ATD (PID 3016) is running ...

From the above you can see that the ATD service is started. If your ATD service is not started, you can use

#/etc/init.d/atdstart

To start your ATD service.

In the ATD service, we often use the at command, which has /etc/at.deny and/etc/at.allow two files associated with the AT command to filter which users can use the AT command. Linux presets, only /etc/at.deny this file, and this file content is empty, because the content does not have any user, so everyone can use at command. If you do not want a consumer to use at, then write the user's login username to the /etc/at.deny file.

The Linux system can also create its own /etc/at.allow file, so that the user written in this file can use at , no users in this file will not be able to use at (even if not written in At.deny ). If none of the two files exist, then only root can use the at command.

Here's how to use the at command:

At [-m] time;//Release scheduled task, then the edit window will appear, write the command to execute

-M: when the at Schedule task is executed, the screen output mail is sent to the consumer of the release instruction.

Please refer to the following column for the format of time.

# at 04:002011-09-20// 2011 September , 20th, four o'clock in the morning execute the command.

# at 11pm//tonight at 11 o ' Night, if it's over, wait till tomorrow night

# at 08am + 3weeks //Three weeks after eight o'clock in the morning execution

There are also two common commands:

ATQ;//view Scheduled Tasks

ATRM;//delete Scheduled Tasks

2.Crond Service

2.1 Crond Service Introduction

Linux task scheduling is mainly divided into the following two categories:

* System execution: The work to be performed by the system periodically, such as backing up system data, cleaning up the cache

* Personal work: A user's regular work, such as checking the mail server for new letters every 10 minutes, can be set by each user.

Cron can be used to help the system perform task scheduling at this time. Cron can schedule a daemon to execute a recurring task based on a combination of time, date, month, and week.

The Crond service is implemented through the CRONTAB (scheduled task control) under the shell and Linux/unix. Cron is the name of a scheduled task,Crond is a background process, and crontab is a custom-made scheduled task table. The name Cron comes from "Chronos", an Ancient Greek "time " meaning. The Crond process periodically checks to see if there is work to be performed, and the work is performed automatically if there is work to be done.

To use the Crond service, you must have the Vixie-cron RPM package installed and you must be running the Crond service. To determine if the package is installed, use the rpm-qvixie-cron command. To determine whether the service is running, use the /sbin/service crond status command.

The Crond service is a built-in service for Linux, but it does not boot automatically. You can start and stop the service with the following command:

/sbin/service Crond start//Start-up service

/sbin/service crondstop //Close service

/sbin/service Crondrestart //Restart service

/sbin/service crondreload //Reload configuration

You can also let the service start automatically when the system starts, just add it at the end of the /etc/rc.d/rc.local script :

/sbin/service Crondstart

Use of 2.2 Crond services

Crond Service provides crontab command to set Crond service, here are some parameters and descriptions of this command :

crontab-u//setting a user's cron service, This parameter is only allowed for root use

crontab-l//listing Details of a user's cron service

Crontab-r //Delete a user's cron service

Note: If you have more than one scheduled task Crontab-r will delete all of the user's plans, if you only want to remove one of the scheduled tasks, use the crontab-e command or edit the /var/spool/cron/root file directly.

crontab-e//editing a user's cron service

Now for example:

# crontab-u Root–l//root View your cron settings

# crontab-u Fred–r//root want to remove Fred's cron settings

# crontab-u root–e//Edit root crontab file

The crontab-e command edits the cron file that corresponds to the user under/var/spool/cron

We can also directly modify the /etc/crontab file,crontab file has its own format, specifically as follows:

Minute Hour Day Month Dayofweek command

Minutes hours days months days per week order

Each field represents the following meanings:

Minute the first few minutes of every hour to perform the task

Hour the first few hours of the day to perform this task

Day of the month to perform the task

Month months of the year to perform this task

DayOfWeek the day of the week to perform the task

command specifies the program to execute

Examples are as follows:

5 * * * * ls//Specifies that the ls command is executed once every hour for the first 5 minutes

5 * * * ls//specify 5:30 per day to execute the ls command

7 8 * * ls//Specify 7:30 of the monthly number 8th to execute the ls command

5 8 6 * ls//designated every June 8th 5:30 execute ls command

6 * * 0 ls//Specifies that the ls command is executed every Sunday 6:30

Note:0 for Sunday,1 for the week 1, and so on, can also be expressed in English,Sun said Sunday,Mon said Monday and so on

3 10,20 * * ls// 10th and 20th per month 3:30 execute ls command

Note: "," used to connect multiple discontinuous periods

258-11 ***ls// Execution of the ls command at 25 minutes 8-11 o ' day

Note: "-" is used to connect consecutive periods

*/15 * * * * ls// executes the LS command every 15 minutes (that is, 0 15 30 45 60 minutes per hour to execute the ls command )

6 */10 * * ls//each month, the LS command is executed every 10 days 6:30 (i.e. 1,11,21,31st) 6:30 is executed once ls command )

7 * * * rootrun-parts/etc/cron.daily//per day 7:50 Execute all executable scripts in the/etc/cron.daily directory as Root

Note: Therun-parts parameter indicates that all executables in the following directory are executed, and if you remove this parameter, you can write a script name to run instead of the folder name.

5 * * * ls</jp/test 2<&1//per day 5:30 execute the ls command and output the results to the /jp/test file

Note:2<&1 indicates execution results and error messages

Remember the meanings of several special symbols :

"*" represents a number in the range of values ,

"/" stands for "Every ",

"-" represents a number to a number ,

"," separate a few discrete numbers

In these fields, except that "Command" is the field that must be specified each time, the other fields are optional fields, which can be determined visually. For a field that is not specified, "*" is used to fill its position.

Linux Scheduled Tasks (reproduced)

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.