linux-Scheduled Tasks

Source: Internet
Author: User

Scheduled Task cron

To schedule a task:

Let the system perform certain tasks (programs) at a specified point in time, and the task can be executed periodically or only once.

Scheduled tasks in Linux systems: the AT and Cron services are 2 services built into the operating system, and are installed by default.

At service : Specifies a time to perform a task at once, depending on the system background ATD process

Cron Service : Specifies the time to perform a task periodically, depending on the system background Crond process

Role: The planning task is mainly to do some periodic tasks, the main purpose of the current is to regularly back up data

Schedule one-time tasks with at. One-time dispatch execution at
Schedule recurring jobs with Cron. Cyclic scheduling execution Cron
Schedule Recurring System jobs.
The output from all scheduled task executions is sent to the specified user by mail, unless redirected

One-time dispatch execution at

At Task schedule:

1. Determine if the At this service is installed:
# rpm-qat at-3.1.10-43.el6_2.1.x86_64
2. Related documents:
# rpm-ql at |grep etc/etc/at.deny      user Control list, deny user list/etc/at.allow (default Yes, allow list); By default, all users are allowed to use the at service /etc/ rc.d/init.d/ATD   at service startup script /etc/pam.d/ATD        Authentication mechanism /usr/bin/at            -- command /usr/bin/ ATQ           -- View the Task List /usr/bin/atrm          -- Delete task /var/spool/at/xxx      --Save directory for scheduled task text file
3. Common commands:
At : Develop a one-time scheduled task -L list at task equals ATQ-D Delete at task equals ATRM-C View task contents
4. Command format:
At 3: 30pm at15:30 at10:10Todayat Now+2min--2 minutes after execution at 4pm+ 3days--3 days after 4 o'clock in the afternoon run this jobat 10am Jul31--July 31 10 o'clock in the morning run this jobat 1am tomorrow--tomorrow 1 o'clock in the morning run this jobat4:00 2020-12-12--in 2020-12-12th 4 o'clock in the morning execution at-T 09201430--September 20 2:30 run this jobat-T 202009201430--2020, September 20, 2:30 run this job#Mans atAt now + 5minutes task runs at now in 5 minutes+ 1Hour task runs at now after 1 hours+ 3days task runs after 3 day at now+ 2the weeks task runs after two weeks Crtl+ D--End instance: more complex tasks can be saved to the script:

At Service access control:

/etc/at.allow function: Only allow the user to use ATD This service, if the file exists, do not consider At.deny

/etc/at.deny effect: Only deny inside the user use ATD This service, if At.allow does not exist, it only takes effect

Example
example 1:[[email protected]~]#At now +1min--one minute to executeAt>Useradd uuuu at> <EOT>Job1 at Sat 21 22:34:00 2015[[Email protected]~]#ATQ3 Thu Mar 30 09:55:00 2017a root[[email protected]~]#ID uuuuexample 2:[[email protected]~]#VIM at.jobs---Create a file, the name can be customizeduseradd u99useradd U00touch/' Date +%F '. Txt[[email protected]~]#at now +1min < at.jobs a minute to execute the command that entered the file and execute
Recurring Scheduled Tasks crontab

Cron services are available when recurring tasks are required, and the service checks every minute and performs a qualifying task

1. Check if the service is started
# Service Crond Status # /etc/init.d/crond Status
2. View a list of related files
#which crontab#Rpm-qf/usr/bin/crontab#RPM-QL Crontabs#RPM-QL Cronie/var/spool/Cron User-level Scheduled Tasks store directory (user-named)/etc/Cron.deny/etc/pam.d/Crond/etc/rc.d/init.d/Crond Startup Scripts/usr/bin/crontab User-level commands/usr/sbin/crond binary Commands/etc/cron.d/inside is usually stored to set aside daily/weekly/timed tasks outside of the month, such as tasks performed per hour and any other scheduled tasks. /etc/cron.d/0hourly Systems need to perform the first minute of every hour/etc/cron.deny user deny list (users in this file cannot use Cron service)/etc/crontab the file serves the equivalent of/etc/cron.d/One of the following files allows you to define a system scheduled task. /var/spool/Cron This directory is used to store each user's own set of scheduled tasks, ordinary users do not have access to direct, you must pass the crontab command (setuid)/etc/cron.monthly/the script that the system needs to execute every month/etc/cron.weekly/the scripts that the system needs to execute weekly/etc/cron.daily/the script that the system needs to execute every day/etc/cron.hourly/the script to be executed per hour for the storage system
3. crontab Format Description
#Cat/etc/crontab There are some scheduler programs running on the system. Each user can establish their own dispatch crontab. SHELL=/bin/Bash The default shell to tell the system which shellpath to use=/sbin:/bin:/usr/sbin:/usr/bin defines the path to the command mailtoThe execution result of the =root command is sent as a message to root (whether the standard is correct or the wrong result) if mailto=" ", the representative will not send an email to anyone. HOME=/define the home directory when executing a command or script#For details see Mans 4 Crontabs#Example of job definition:#.----------------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 executedThe first 5 fields represent: minutes: 0-59Hours: 0-23Date:1-31Month:1-12Week: 0-7(0 means Sunday or 7 means Sunday) You can also use some special symbols:,: Represents the split, interval. As a second* Expressed as 8,10, representing 8 points and 10 points-: Represents a segment, such as a second* Expressed as 1-5, it means 1 to 5./N: Indicates that each n unit executes once, as the second * is represented as */1, which means that the command is executed every 1 hours.
Example
XX * * * ls//Daily 2:00 Full1 * * ls//1th every month, 2:00 the whole .2 * ls//every February 14 2:00 the entireXX * * 7 ls//every Sunday 2:00 full* 6 5 ls//every June, Friday 2:00 the whole* 7 LS//14th per month, 2:00 or every Sunday 2:00, these two times are executed.2 7 LS//Every February 14, 2:00 or every Sunday 2:00, these two times are executed.XX * * * ls//Daily 2:00 Full* * * * * ls//every minute of 2:00 every day* * * * * ls//Execute ls per minute* * 2 * ls//1440 minutes per minute, February 14*/5 * * * * ls//every 5 minutes1,5,8 * * ls//monthly 1, 5, number 8th, 2:00.1-8 * * ls//1 to 8th per month 2:00
Thinking 1

What if the system loses power or goes down and misses the time of the task execution?

Anacron tasks:

is an extension of the Cron service that works with the Cron service, and when the system is powered down or down and misses the time of the task execution, it will perform the non-performing task again after the system starts.

Configuration file:/etc/anacrontab

# Period in day delay in      minutes   job-identifier    command command execution frequency (days)   delay time (minutes)     task description          commands to execute 1                      5               cron.daily Nice        run-parts/etc/cron.daily7              Cron.weekly       Nice        run-parts/etc/cron.weekly@monthly               -              cron.monthly      Nice        run-parts/etc/cron.monthly

Note: For each scheduled task, the Anacrontab first determines whether the period (execution frequency) period is executed, and if no execution waits for delay (latency), perform the task again.

In general, the file does not have to be modified, know the principle.

Summarize:

1. If you have a lot of tasks that need to be repeated every day | weeks |, and you want the system to automatically fill out the missing tasks, you can put your script in the system response directory under/etc/cron. {d{daily,weekly,monthly}, and give executable permission.

2. The task that requires the system to be called periodically will be called a system-level scheduled task.

Example

Specify a recurring scheduled task by at least 2 methods:

1. stu01 users every Sunday and Wednesday 5:04 A.M. view/etc/hosts file contents and save to/tmp/stu01 file * * 0,3 stu01 cat/etc/hosts >/tmp/ 1/etc/hosts >/tmp/stu01* * 0,3 stu01/root/1. sh2, root user toward/tmp/  The current system time is appended to the stu01 file every 2 minutes */2 * * * * Root date >>/tmp/stu01

Set user-level scheduled Tasks

Related commands:

  Normal users edit their own scheduled Tasks crontab -e <--- edit current The user's own scheduled task (using the default editor specified by the Environment variables editor) crontab -L <--- lists all of the current user's own scheduled tasks Cronta b -R <--- Delete all of the current user's own scheduled tasks as an administrator, root can set /adjust all user's scheduled tasks, commands similar to the above, only Is the addition of a-u <username> parameters crontab -e-u Redhat <--- Edit the timer task for the specified user (using the default editor specified by the Environment variables editor) crontab -l-u Redhat <--- list all scheduled tasks for the specified user C Rontab -r-u Redhat <--- Delete all scheduled tasks for the specified user description:  1, crontab default call is the VI editor, edit the plan to either The use of services is the same as VI. If you want to define the editor yourself, complete by modifying the environment variables. such as export Editor=vim  2, user-level task definitions, there is no user name column, if you like to define system-level commands, enter the user name, The Crontab editor will not prompt for formatting errors, but the task will not be executed correctly. 
or or run-parts/dir/ Description:1, if you execute a script file, then you need to have executable permissions, if you can not use bash| sh, such as specifying an interpreter to perform 2, if run-parts, then all script files in the directory specified below must have executable permissions 3, user-level scheduled tasks, do not need to specify username4, Normal user can not directly vim modify their own scheduled task file (/var/spool/cron/username)
Cron Usage Restrictions
/etc/Cron.deny          --deny only which users can not use crontab, the default existence of empty files /etc/cron.allow         -- Which users are allowed to use crontab if the same user is in the deny and in the Allow, the default permission is preferred. 
Thinking 2

If I use Crontab-e write night 8:30 to shut down the machine, but I 18 o'clock manual shutdown, the next day to 9 points to boot, it will delay a period of time and shut down?

Answer: No

Example
Backup etc directory, requires:1. Daily 4:00 Backup/etc directory to/var/Backmkdir/var/Backtar-czf/var/back/$ (date +%f) _etc.tar.gz/etc00 04 * * * * *. Write the backup command in a script, such as/root/back.sh, plus execute permissions vim/root/Back.shtar-czf/var/back/$ (date +%f) _etc.tar.gz/Etcchmod+x/root/back.sh3. The file name backed up daily contains the date of the day, such as 2016-11-09_etc.tar.gz4when a scheduled task executes, the screen does not produce any outputXX * * */root/back.sh &>/dev/NULL5. Keep only the last 5 days of backup find/var/back-ctime +5-execRM-F {} \;find/var/back-ctime +5 |xargs RM-Fvim/root/Bak.shmkdir/var/Backtar-czf/var/back/$ (date +%f) _etc.tar.gz/Etcfind/var/back-ctime +5-execRM-F {} \;chmod+x/root/Bak.shcrontabe:XX * * * bash/root/bak.sh
#!/bin/bashdir=/var/ Back#determine if the backup directory exists and create it if it does not exist[!-D $dir] &&mkdir $dir#Backup/etc directory, using the Tar toolTAR-CZVF $dir/$ (date +%f) _etc.tar.gz/etc#the backed up files only need to be kept within 5 daysFind $dir-ctime +5-execRM-F {} \;crontabe:XX * * * bash/root/bak.sh

Linux-Scheduled Tasks

Related Article

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.