Routine work scheduling for Linux (crontab)

Source: Internet
Author: User
Tags name database

What is routine work scheduling
1 Types of Linux work scheduling: At,cron
Two ways to schedule a job:
Routine: Items to be done every certain period
Burst: Do not do it only once
At:at is an instruction that can handle the completion of a scheduled execution once, but to perform an at, there must be support ATD the service, in Redhat and CentOS, the service is scheduled to start.
Crontab:crontab the work set by this directive will be cycled through, with a cycle time of minutes, hours, weeks, months, or yearly. Crontab In addition to the use of command execution, can also edit/etc/crontab to support, as for crontab can be effective service is crond this service

2 common routine work on CentOS Linux systems
1 rotation of the login file (log rotate)
Linux will take the initiative to record all the information that happens in the system, that is, log files, which will result in more and more log file, it is suitable to transfer the log file data, the old data and new data separation, this is the task of log rotate.
2 Login File Analysis Logwatch's tasks:
If the system has a software problem, hardware errors, security issues, and so on, most of the error messages will be logged into the login file, so one of the important tasks of the Administrator is to analyze the login files, CentOS provides a program (Logwatch) to actively analyze the login information, So they're going to receive Logwatch's letters.
3 Establishing a locate database
Locate instruction is through the existing file name database to the file name on the system query, our file name database is placed in the/var/lib/mlocate/, this database automatic update is the system of routine work produced by the effect
4 Man page Query database establishment
5 RPM Software Login file creation
6 Removing a staging archive
7 analysis behavior related to network services
++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Two work schedules that are performed only once
1 ATD Start-up and at operation mode

How the At Works
We use the AT command to generate the work requested and to write the work into the/var/spool/at/directory as text, which can wait for the ATD of the service to be taken and executed.

We can use the/etc/at.allow and/etc/at.deny two files to limit the at
1 First look for/etc/at.allow this file, written in this file users can use at, no user in this file can not use at (and even if not written in At.deny)
2 If/etc/at.allow does not exist, look for/etc/at.deny this file, if it is written in this At.deny user can not use at, but not in this At.deny file users, it is possible to use at this command
3 If none of the two files exist, then only root can use the AT command.

2 actual operation single work scheduling
Grammar:
At-c Work number
Options and Parameters:
-M: When the at job is completed, even if there is no output message, the user can be notified by email that the work has been completed.
-l:at-l is equivalent to ATQ, which lists all of the user's at schedules on the current system
-d:at-d equivalent to ATRM, can cancel a work in at scheduling
-V: You can use the more obvious time format to stop the taskbar table in the at schedule
-C: You can list the actual instruction content of the work after it

Time: Temporal format, where you can define when [when to do at this work] time hh:mm
cx>04:00
At this time of day, if the time has expired, tomorrow hh:mm will do the work
hh:mm YYYY-MM-DD
cx>04:00 2017-12-22
To enforce the work at a particular time of day of the month
Hh:mm[amlpm][month][date]
cx>04pm July 30

Hh:mm[am|pm]+number [Minutcs|hours|days|weeks]
CX > Now + 5minutes cx>04pm + 3 days

Configuration instance:
1 Configuring a fixed time to perform a fixed operation (can only be performed once)

2 Configuring operations after this time

3 View task status
Create a task

View task ID

At-c task ID number to view specific actions performed

The specific action

4 Deleting a task
AT-D ID Number

Note:
If at Now+1min
echo "Hello"
is not displayed
Because the execution of at is not related to the terminal environment, all standard Output/standard error ouput are routed to the performer's mailbox
, so there won't be any display on the terminal.

At also has a background function to perform
Tasks that continue working offline
Due to the use of the at work scheduling, the system will be independent of the project in your bash environment, directly to the system's ATD program to take over, so when you release the work of the at the immediately offline, the rest of the work can be given to Linux management

Batch: Perform background tasks when the system is empty
In fact, the use of batch at the command release, just add some control parameters, batch magical Place is: he will be in the CPU workload of less than 0.8, only to carry out the task you issued.

Permissions black and white list



The admin user at this time does not have permission to execute the AT command

Add them to the whitelist


At this point the at task can be established

++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
Routine work scheduling for three-cycle execution

The routine scheduling of the loop execution is controlled by the cron (Crond) system service.

1 User's settings
/etc/cron.allow will be able to use Crontab's account to write to it, if the user is not within the file is not available crontab
/tec/cron.deny's account is written to it, and if the user is not logged into the file, you can use crontab.

Priority permission is higher than rejection, recommended general retention denied, permissible can not be written

When the user uses the crontab command to set up a work schedule, the work is recorded in the/var/spool/cron, and the account is used as a discriminant.

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

-U only has root to perform this task, and it also helps other users to set up/remove crontab work schedules

-e: Edit Crontab's work
-L: Check the crontab's work content
-R: Remove all the work of crontab, if it is urgent to remove an item, use-C to edit

Initiating a task using the command line


View of the task

The result of the task

Task format
7 * TOUCH/MNT/AAA
Actions to be performed on a minute-hour-month-day week

Auxiliary characters

Delete a task


To delete a single task, you need to edit and delete it using-E

To view a task


The default storage directory is/var/spool/cron/ , which indicates the user name

2 configuration file for the system:/etc/crontab,/etc/cron.d/
CRONTAB-E is designed for cron, if it is [system's routine task], only need to edit/etc/crontab this file can, Crontab is actually/usr/lib/crontab this executable file, but/etc/ Crontab is a plain text document that can be edited using Root's identity

Cron The minimum detection limit for this service is [minutes], so [cron reads the data contents of/etc/crontab and/var/spool/cron every minute], so it's only necessary to edit and store it.

Sometimes other Unix systems, because crontab is read in memory, so after you modify/etc/crontab, may not be executed immediately, this time please restart Crond this command

[Email protected] ~]# Cat/etc/crontab
What kind of shell interface is used by Shell=/bin/bash
Path=/sbin:/bin:/usr/sbin:/usr/bin Execute File Search path
Mailto=root If there is an extra stdout, Cmail send the data to whom
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 executed

Mailto=root;
This project is to say that when the/etc/crontab of the routine work in this file is wrong, or if the result of the work is Stdout/stderr, the error message or the screen display of the communication to whom, by default, the system directly sends a mail to root, Can be changed to their own e-mail

Path= ...;
Path to the search document

[Time-of-day and month-to-day identity instruction] seven field settings

Must have an identity designation in the/etc/crontab

Crond Service reads the location of the configuration file
/etc/crontab
/etc/cron.d/
/var/spool/cron/

Summarize:
File mode definition Crontab
vim/var/spool/cron/User name #以某个用户身份执行cron, the file must be consistent with the user name
Time-of-day month-week action
vim/etc/cron.d/file #设定多个用户身份执行cron
< > < > < day > < month > < week > < user > < action >
Cron tasks in the system
/etc/cron.daily/# #系统每天做执行的程序
/etc/cron.hourly/# #系统每小时执行的程序
/etc/cron.monthly/# #系统每月执行的程序
/etc/cron.weekly/# #系统每星期执行的程序

Routine work scheduling for Linux (crontab)

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.