Linux Task Scheduler

Source: Internet
Author: User

The Linux task plan is divided into two types: one-time Task Scheduler and a recurring task plan implementation tool primarily at and crontab the use of the Task Scheduler tool is described in detail below.

1. At command one-time mission plan

The AT command is provided by the ATD service whose main package is ATD on CentOS6 you can use the Service ATD Start command to start the services on CentOS7 that need to use Systemctl start Atd.service.

Common methods for AT commands

At time: The simplest way to do this is to follow up with the Times to create a task plan in an interactive manner and end with Ctrl+d.

At-f file time: Read job task from specified file without interactive input

At-l: Equivalent to ATQ lists all current user's at queues on the system

At-d job: Equivalent to ATRM can delete a specified job

At-c job: View specific job Tasks

At created tasks are placed in the/var/spool/at directory and in the execution results of the at Job task, if there are standard output and error output, they are notified to the relevant user in the form of a message.

The time format is:

HH:MM: A few minutes of the day. Perform a task if the time is past and tomorrow the task can also be performed using the following defined time format

Noon: 12 O'Clock Noon

Midnight: 0 o ' morning

Teatime: Afternoon tea Time is 4 o'clock in the afternoon

hh:mm YYYY-MM-DD: A few minutes of a certain day of the year to perform a task

HH:MM[AM|PM] [Month] [Date]: Ditto is also specified at a certain point in time to perform a particular day such as 04pm March 17

Hh:mm tomorrow: A few minutes of tomorrow's execution

Now + # (Minutes,hours,days,weeks): Add # minutes or # hours or # days or # weeks after the current time to execute

By default, all users can use the AT command to create a one-time scheduled task if the administrator needs to control specific those users can use at to create a scheduled task that can be controlled by blacklist deny or whitelist allowed and by default the system comes with a blacklist file/etc/ At.deny fill in the list of users who are not allowed to use the AT command. You can also manually create a whitelist/etc/at.allow in which to fill in the list of allowed users if the blacklist and whitelist exist simultaneously The blacklist does not take effect only users in the whitelist can use the AT command to create tasks.


2. Batch command One-time task plan

Batch creates a one-time task plan and the use of at is almost identical, but batch does not need to specify when batch will let the system choose the idle time to perform the specified task.


3. crontab Command Recurring task plan

The crontab command requires the Crond daemon to serve its main package is Cronie. Submitting a scheduled task to Crond by using the crontab command differs from at, it needs to use a dedicated configuration file and this file has a fixed format.

The cron task is divided into two categories, that is, the system cron task is mainly used to implement the system's own maintenance and the periodic tasks created in general, the system tasks are not required to be manually added when installing the system will be automatically created if you need to add system tasks, you can manually edit the/etc/crontab file. A class is a user cron task customized according to user needs of the periodic task its creation method you can use the crontab command to create the crontab command will automatically detect a syntax error when creating a task and create a task profile will be automatically saved as a user name in the/var/spool/cron/ Directory.

The system cron task is in the format

#.----------------Minute (0-59)

# | .-------------Hour (0-23)

# |   | .----------Day of Month (1-31)

# |   |  | .-------month (1-12) OR jan,feb,mar,apr ...

# |   |  |  | .----Day of Week (0-6) (sunday=0 or 7) or Sun,mon,tue,wed,thu,fri,sat

# |   |  |  | |

# * * * * * * user-name command to be executed

A total of 7 fields the first 5 fields represent time the 6th field indicates that running the 7th field as a user represents a recurring task for each row of a specific command task.

The format of the user cron task is basically the same as the System Cron task format, but the user cron task does not need to specify which user to run directly with the specific command task, so the user cron task has a total of 6 fields.

Time notation for cron tasks

1), the value of a specific value within the range of valid values for a given point in time

2), Asterisk * indicates that all values in the range of valid values at a specific point in time represent "every ..."

For example: 12 o'clock noon each year on October 1:0 12 1 10 *

Every night 18 o'clock is indicated as: 0 18 * * *

The morning point for every Monday is: 0 8 * 1

3), Discrete value

such as: Daily morning point and night 18 o'clock is indicated as: 0 8,18 * * *

1th and 15th per month are expressed as: 0 0 1,15 * *

The weekly Monday and Friday are expressed as: 0 0 * * 1,5

4), Continuous value

such as: 8 to 18 points per day is: 0 8-18 * * *

1th to 15th per month is indicated as: 0 0 1-15 * *

The weekly from Monday to Friday is expressed as: 0 0 * 1-5

5), define the step size on the specified time range

For example: Every 3 minutes is indicated as: */3 * * * *

indicated every 3 days: 0 0 * *

6), system-defined time

@reboot: One time after each reboot

@yearly and annually: all expressed as annual January 1:0 0 1 1 *

@monthly: 1th number per month: 0 0 1 * *

@weekly: Sunday of the week: 0 0 * 0

@daily: Every morning: 0 0 * * *

@hourly: Hourly: 0 * * *


Common uses of the crontab command are:

The Create User recurring task command is: CRONTAB-E

List all users the recurring task command is: crontab-l

The Delete all Users recurring Task command is: crontab-r

Root user creates or manages cron tasks for a specified user: Crontab-u user


Cron tasks can also be blacklisted and whitelisted to control the user's ability to create tasks using the crontab command by default, the system is self-contained/etc/cron.deny or you can manually create a whitelist/etc/ Cron.allow blacklist expires when both whitelist and blacklist are present only users in the whitelist can create tasks using the crontab command.


Need to note:

1) It is recommended to define a cron task using the command to use an absolute path or to customize an environment variable that has a full path.

2) The standard output and errors of the running results of the cron task are communicated to the relevant user in the form of a message. If you do not want to notify the user in the form of a message, you can use redirection to output to/dev/nullcommand &>/dev/null.

3) for cron tasks% has a special purpose if you want to use% in the command you need to escape but you can also not escape if you place the% in a single quotation mark.


Practice:

1, weekly 2, 4, 7 back up/var/log/messages files to the/logs directory with filenames like "MESSAGES-YYYYMMDD"

#先创建/logs directory]# mkdir/logs# use crontab-e Create task]# crontab-e0 0 * * 2,4,7/bin/cp/var/log/messages/logs/messages-$ (date +\%Y\ %m\%d) &>/dev/null

2, every two hours to remove the current system in the/proc/meminfo file with s or m beginning information appended to the/tmp/meminfo.txt file

]# CRONTAB-E0 */2 * * */bin/grep "^[SM]"/proc/meminfo >>/tem/meminfo.txt


This article is from the "Linux Operational Learning path" blog, please be sure to keep this source http://fengliang.blog.51cto.com/3453935/1851608

Linux Task Scheduler

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.