Linux timed tasks detailed

Source: Internet
Author: User
Tags php script

In fact, scheduled tasks we often use, such as early morning backup database, run some PHP script and so on need to use the timed task.

Let me analyze the two commands for the Scheduled tasks:


At

Description: Perform a timed task that runs only once. and relies on the ATD service to run

Options:

-Q queue: Put the task in the queue, do not set the default is a queues

-L: Displays the tasks for all queues. Equivalent to the ATQ command

-D NUM: Deletes the task for the specified number. Equivalent to the ATRM command

-F file: Enter replace standard keyboard input from one file

-V: Shows when the task was executed


Time Options:

At allows you to use a fairly complex set of time-specific methods. He was able to accept the time specified in the hh:mm (hour: minute) of the day. If the time has passed, then it will be executed the next day. Of course, you can use Midnight (late night), noon (noon), Teatime (tea time, usually 4 o'clock in the afternoon) and other vague words to specify the time. The user is also able to use the 12-hour timekeeping system, which is either a AM (morning) or PM (afternoon) After the time to indicate whether it is morning or afternoon. You can also specify a specific date for the execution of the command, specifying the format for Month Day (month) or mm/dd/yy (Month/day/year) or Dd.mm.yy (day, month, year). The specified date must be followed by the specified time. The absolute timing method is described above, and the relative timing method can be used, which is good for arranging orders that will be executed soon. The specified format is: Now + Count Time-units, today is the current time, Time-units is the time unit, here can be minutes (minutes), hours (hours), Days (day), Weeks (week). Count is the amount of time, whether it is a few days, or a few hours, and so on. A more timed method is to specify the time to complete the command directly using today, tomorrow (tomorrow).

Time: Date format, where you can define when to do at this task, in the following format:


Instance:

At 16:00 assuming that the current time is 16:30, the task will be performed at 16:00 on the second day

At 16:00 2015-9-20

At now + 5 (minutes|hours|days|weeks) time relative to the current time

At 04pm + 3 days


Crontab


First, Crond Introduction

Description: In fact, we use the most of the item on the server, and at the server on the actual use of very little. Crontab indicates timed task loop execution


User Rights file :

File:

/etc/cron.deny

Description

The users listed in this file are not allowed to use the crontab command

File:

/etc/cron.allow

Description

Users listed in this file are allowed to use the crontab command

File:

/var/spool/cron/

Description

directory where all user crontab files are stored, named by user name

What the crontab file means:

In the crontab file created by the user, each line represents a task, each field of each row represents a setting, its format is divided into six fields, the first five is the time setting segment, and the sixth paragraph is the command segment to execute, in the following format:

Minute hour day Month Week command

which

Minute: Represents minutes, which can be any integer from 0 to 59.

Hour: Represents the hour, which can be any integer from 0 to 23.

Day: Represents a date, which can be any integer from 1 to 31.

Month: Represents the month, which can be any integer from 1 to 12.

Week: Represents the day of the week, which can be any integer from 0 to 7, where 0 or 7 represents Sunday.

Command: The commands to execute can be either system commands or script files that you write yourself.


In each of these fields, you can also use the following special characters:

Asterisk (*): represents all possible values, such as the month field if it is an asterisk, the command action is executed monthly after the constraints of other fields are met.

Comma (,): You can specify a list range with a comma-separated value, for example, "1,2,5,7,8,9"

Middle Bar (-): An integer range can be represented by a middle bar between integers, such as "2-6" for "2,3,4,5,6"

Forward slash (/): You can specify the interval frequency of the time with a forward slash, such as "0-23/2", which is performed every two hours. A forward slash can be used with asterisks, such as */10, if used in the minute field, which means that it executes every 10 minutes.


Second, Crond Service

Installing Crontab :

Yum Install Crontabs

Service Operations Description:

/sbin/service Crond Start//Startup service

/sbin/service Crond stop//Shut down service

/sbin/service crond Restart//Restart service

/sbin/service Crond Reload//Reload Configuration

View Crontab Service Status:

Service Crond Status

To start the Crontab service manually:

Service Crond Start

To see if the Crontab service is set to boot, execute the command:

Ntsysv

Join the boot auto start :

Chkconfig–level Crond on


Third, crontab Command Explanation

1. command format:

crontab [-u user] File

crontab [-u user] [-e |-l |-r]

2. Command function:

by Crontab command, we can execute specified system instructions or shell script scripts at a fixed interval of time. The units of the time interval can be any combination of minutes, hours, days, months, weeks, and more. This command is very useful for periodic log analysis or data backup.

3. Command parameters:

-u User: Used to set a user's crontab service, for example, "-u ixdba" means to set IXDBA user's crontab service, this parameter is usually run by the root user.

File:file is the name of the command file, which indicates that file is the Crontab task list and loaded into crontab. If this file is not specified on the command line, the crontab command will accept the commands typed on the standard input (keyboard) and load them into crontab.

-E: Edits the contents of a user's crontab file. If you do not specify a user, the crontab file for the current user is edited.

-L: Displays the contents of a user's crontab file, or displays the contents of the current user's crontab file if no user is specified.

-r: Deletes a user's crontab file from the/var/spool/cron directory and, if no user is specified, deletes the current user's crontab file by default.

-I: Give a confirmation prompt when deleting a user's crontab file.



Actual combat


1, every Monday to Saturday 3:20 A.M., run the tar command to save the/etc/directory, storage location is/backups/etc-yyyy-mm-dd.tar.xz;

Answer: 3 * * 1-6/bin/tar jcfp/backups/etc-' Date +\%y-\%m-\%d '. Tar.xz/etc


2, every Sunday 2:30 A.M., run the CP command to backup the/etc/fstab file, the storage location is/BACKUP/FSTAB-YYYY-MM-DD-HH-MM-SS

Answer:2 * * 0/bin/cp/etc/fstab/backup/fstab-' Date +\%y-\%m-\%d-\%h-\%m-\%s '


3, every night 12 o'clock, get all the lines in the/proc/meminfo file starting with S or M, appended to the/statistics/meminfo.txt file, and the daily message before, to add a similar =============== separator line;

Answer: XX * * */bin/grep-e ' ^ (s| M) '/proc/meminfo | /bin/sed ' 1i =============== ' >>/statistics/meminfo.txt





This article is from the "Wind Rhyme" blog, please be sure to keep this source http://chinalx1.blog.51cto.com/2334265/1693977

Linux timed tasks detailed

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.