Linux timed Tasks

Source: Internet
Author: User
Tags crontab syntax

Timing Task Crond Introduction

Crond is a service or software used on a Linux system to perform regular commands/scripts or to specify program tasks, and in general, after we install the CENTOS5/6 Linux operating system, the Crond Task Scheduler service is started by default.

The Crond service will periodically (by default, check every minute) to check if there is any task to be performed on the system, and if so, it will automatically perform the scheduled task according to its pre-set scheduled task rules , which is the same as the alarm clock that we used to get up early in the Crond timer service.

Startup services that must be retained:

Crond: Timed Task Service
Network: Web Services
Rsyslog: Log service
SSHD: Remote Link Service
Sysstat: System monitoring Services (involving a range of monitoring tools)

Why use timed tasks?

    • Let's give an example: for example, our database or code program needs to do a full backup every night 0 o'clock, so that every night to do the periodic work, if you want to operate, you have to climb up every night, and then log on to the system to perform tasks, and then sleep. In addition, the process of performing a task can last for several hours, so that we don't have to sleep one night, which is obviously not possible. So what can be done to solve this recurring task requirement?
    • This is the Linux system's scheduled task Crond, which is equivalent to our usual life of the alarm clock function, every night in advance to set the timing, wake you up on time in the morning.
# log of the system  # User login log

Tasks performed by a user: tasks that a user or system administrator will perform on a regular basis, such as time synchronization every 5 minutes with the time server on the Internet (which is what is installed in the Basic Optimization section of the system), back up the site data and database data at 0 o'clock every night , In general, these tasks need to be set by each operational dimension itself.

Server Time Synchronization
" */5 * * * * /usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1 " >>/var/spool/cron/root

This can be done every 5 minutes through the command set above. Synchronize Internet time to the local Linux system each time.

Crontab-l View the current user's scheduled task configuration . * Number below will be detailed ~ ~

time.nist.gov is the NTP server address time.windows.com, Microsoft home time server. For more information, please refer to ==> domestic common NTP server address and IP

Crontab (command): As mentioned earlier, this command can perform tasks periodically, for example: Server time synchronization every five minutes. to execute crontab This command, you also need to start a service Crond . This crontab command is the most commonly used command in the production work, please be sure to master them.

1, starting from the start Crond service

2. Status of service at this moment (view, open, close)

3. How to view processes

Ps-ef | grep Crond

Summary tips:

    • What we call the Crond service is the running program, and Crontab is the command to manage the user's timed tasks (rules)
    • Crond Service is an important service commonly used in enterprise production, at and Anacron are seldom used, can be neglected
    • Crond services are used by almost every server
    • Thousands of servers can develop distributed scheduled Task project scenarios (batch management Ansible/saltstack)

Crond Usage Instructions for timed tasks
[[Email protected] ~]#crontab--helpCrontab:invalid Option--'-'crontab:usage error:unrecognized Optionusage:crontab [-u user] file crontab [-u user] [-e |-L |-R] (default operation isReplace, per 1003.2)    -E (Edit user's crontab)-L (List user's crontab)-R (Delete user's crontab)-I (Prompt before deleting user's crontab)-s (selinux context) [[email protected]~]# 
    • Crontab-l viewing the current user's scheduled task configuration
    • Crontab-e entering the current user's timed task vim edit mode
    • Crontab-u User name-L view scheduled task settings for a specified user

Focus

By crontab we can execute specified system instructions or script scripts at a fixed interval of time. The unit of time interval can be any combination of minutes, hours, days, months, weeks and more (note: Do not mix day and week). Crond Service through the crontab command can easily achieve periodic log analysis or data backup enterprise operation and maintenance scene work

User rights and timed task files

the file takes precedence over Cron.deny (default does not exist, usually not)
file description
/etc/cron.deny (Deny) the user listed in this file is not allowed to use crontab
/etc/cron.allow (allow)
/var/spool/cron/ All users crontab configuration text This directory exists by default, and the file name is named with the user name

 [[email protected] ~]#   Cat/etc/cron.deny  zcl[[email protected]  ~]#   SU-ZCL  [[email protected]~]$ crontab- lyou (Chensiqi) is  not   allowed to the use of this program (CRONTAB) See crontab ( 1) for   more information[[email protected]  ~]$ crontab- eyou (Chensiqi) is  not   allowed to the use of this program (CRONTAB) See crontab ( 1) for  more information  

crontab {-L |-e} is actually manipulating files such as /var/spool/cron/Current user

By default, when a user establishes a scheduled task rule, the rule records that the corresponding configuration file exists in/var/spool/cron , and its crontab profile corresponds to the name of the logged-on user, such as: The root user's timer task profile is/var/spool/cron/root

[[Email protected] ~] # ls/var/spool/cron/Root   # is not available by default, only when a timed task is created [[email protected] ~]# cat/var/spool/cron/root*/5 * * * */sbin/ntpdate time.windows.com >/dev/null 2>&1

Crontab Timing task of the writing format is very simple, the user's timing task rules are generally divided into 6 segments (each separated by a space, the system's scheduled task is/etc/crontab, divided into 7 segments, a space to separate), the first five is the time of the set paragraph, the sixth paragraph is the command or Script task segment to be executed

User timed Task instance:

* * * * cmd #每小时的01分钟执行
* * * cmd #每天4点的02分钟执行
* * + cmd #每周日的14点22分执行
* * cmd #每月1日的4点42分执行
Tips:
1. CMD is the command or script to execute, for example:/bin/sh/server/scripts/chensiqi.sh
2. There must be a space between each column. Can I have more than one space? My own practice ==> by my own test has a number of spaces can also be executed!!

Time Memory formula: The week of the moon

The meaning of special symbols in crontab syntax format is as follows table
Special Symbols meaning
* * Number, means any time, is actually the meaning of "every"
- The minus sign represents a separator, which represents a time range, an interval segment, such as 17-19 points, for example: 00 minutes of the 17,18,19 point per day to perform the task. XX 17-19 * * * cmd
A comma that indicates the meaning of the separating window, for example, 5 o ' 10:00 daily, 5,10 * * * cmd
/n n represents the number, "every n units of time", for example: every 10 minutes the task can be written as /*10 * * * * cmd, wherein,/10, the range is 0-59, so can also be written 0-59/10

Reference Blog: http://www.cnblogs.com/chensiqiqi/p/6367890.html

Linux timed Tasks

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.