Linux Timing Task Supplement

Source: Internet
Author: User

timed task two instances

Example 1: Print your own name every minute pinyin all to "/server/log/your own named file" in.

[Email protected] log]# mkdir-p/server/log/[[email protected] log]# echo "Chensiqi" >>/server/log/chengliang[[ Email protected] log]# crontab-l#time sync by ZCL at 2017/5/8*/5 * * * */usr/sbin/ntpdate time.nist.gov >/dev/null 2& Gt;&1[[email protected] log]# crontab-e     

Error Example:

#print my name to log in Chensiqi at 2017211*/1 * * * */bin/echo "Chensiqi" >>/server/log/chensiqi >dev/null 2&G T;&1 tip: This is a wrong timing task, ask the students to think wrong where?

Answer Knowledge Summary:

1, Scheduled tasks to add comments
2, if you have to direct to the file, the end does not have >/dev/null 2>&1
3./server/log directory must exist in order to produce results, such as not creating this directory.
4, the path of the scheduled task must be absolute path
5, Crond service must first open
6. Check the scheduled task log Tail/var/log/cron

Example 2: Every Saturday, day 9 o'clock in the morning and 14 o'clock in the afternoon (execution/server/scripts/chensiqi.sh). Requirements:/server/scripts/ The function of the chensiqi.sh script is to print the day's date: The format for 2017-02-11 can be arbitrary.

Answer:

# #execute chengliang.sh by ZCL at 2017/5/1400 9,14 * * 0,6/bin/sh/server/scripts/chengliang.sh >/dev/null 2>&1

Steps:

1, create the corresponding directory

Mkdir/server/scripts-p

2. Command line test

3. Writing scripts

4. command line test Script

5. Edit the timed task (let him quickly execute * * * * *)

Crontab-e==> Add the following scheduled Tasks */5 * * * */bin/sh/server/scripts/chensiqi.sh >>/server/log/chengliang.log

6. Testing

Tail-f/server/log/chengliang.log2017-5-14

7, according to the original requirements to change the time of the scheduled task

# #execute chengliang.sh by ZCL at 2017/5/1400 9,14 * * 0,6/bin/sh/server/scripts/chengliang.sh >/dev/null 2>&1

Essentials of writing timed tasks
    • Essentials 1: Add necessary annotations to timed task rules

Add necessary notes : write timed task rules as much as possible with comments (preferably in English comments), which is a good habit and specification.
For example: Who did what at what time (comment content) What person, what time, because what, did what. If these are clearly marked, other OPS people can easily understand the information of the task and improve the team's productivity.

    • Essentials 2: Add/bin/sh before executing the shell Script task

When performing a timed task, if you are executing a script, try to precede the script with the/bin/sh command, or else you may have forgotten to set the Execute permission (x) for the script, so that the result is OK and the task cannot be completed, so it is "tragic".

    • Essentials 3: Timed task command or end of script plus >/dev/null 2>&1

The end of a timed task (typically a script task) is preferably preceded by a >/dev/null 2>&1 and so on, and if you need to print the log, you can append it to the specified log file (not at the same time as/dev/null). Try not to leave blank. If the task is a command, the end of the use of ">/dev/null 2>&1" when more testing, to have a means of inspection. such as: */1 * * * * echo "= =" >>/tmp/chensiqi.log>/dev/null 2>&1 The task rule cannot be executed.

    • Essentials 4: timed task commands more than 2 command execution, preferably with a script file

More than 2 commands are executed, preferably with a script file. The following methods are not standardized, unprofessional.

* * * * * * sleep 1;echo Chensiqi >>/server/log/chensiqi.log

Standard notation:

[Email protected]/]# Cat/server/scripts/log.shsleep1echo Chensiqi >>/server/log/chensiqi.log
    • Essentials 7: Timed task commands or programs are best written to execute in scripts

    • Essentials 8: Scripts executed by timed tasks to standardize paths

      • Example:/server/scripts
    • Essentials 9: Configure timed Task specification operation procedures to prevent errors.

1, the first to be successful in the command line operation, and then copy the successful command into the script, in each small link to reduce the chance of error.
2, then test the script, after the test is successful, copy the canonical path of the script into the scheduled task configuration, do not hand knocking.
3, test in test environment first, then formal environment specification deployment

In the command line input./chensiqi.sh (/server/scripts/chensiqi.sh) and SH chensiqi.sh the difference?

Command description: SH chensiqi.sh means to use/bin/sh this command to parse and start chensiqi.sh this script. The./chensiqi.sh represents the use of Linux's default interpreter to parse and start the script. Therefore,./chensiqi.sh requires Execute permissions for Linux under X, while SH chensiqi.sh does not require

the consequences of not adding >/dev/null 2>&1 to timed tasks
  • If the end of the scheduled task rule does not add >/dev/null 2>&1 such as command configuration, there may be a lot of output information, a long time, may be due to the system does not open the mail service caused by the message temporary directory/var/spool/clientmqueue The hidden danger of a huge number of files is that a large number of files occupy a large number of disk inode nodes (one inode per file), so that the disk inode is full and cannot write to normal data (case below).
  • Hint: The above >/dev/null 2>&1 writing can also be written as 1>/dev/null 2>/dev/null, example: $JAVA-jar $RESIN_HOME/lib/resin.jar $ARGS stop 1>/dev/null 2>/dev/null this notation from the resin service default startup script
  • The above is the case of centos5.8, if the system does not install SendMail (Centos6.4), it is not the above problem?

Enterprise Case: If the end of the scheduled task rule does not add >/dev/null 2>&1, it is easy to cause the hard disk inode space is full, thus the system service is not normal.

When a scheduled task executes, it sends an email to the system. SendMail Mail Service, is often closed, so the scheduled task sent messages will be temporarily heap in/var/spool/clientmqueue/, long time, the number of/var/spool/clientmqueue/files is particularly numerous. There must be this problem when Centos5.

Where's CENTOS6? Please look down.

/var/spool/postfix/maildrop/ D3ad0c6  db2bac9  e14e6d0  

命令说明:

定时任务没定向到空,postfix服务没有开启的话,那么每执行一次定时任务,/var/spool/postfix/maildrop/文件夹下就会产生一个小文件,随着时间累计,就会越来越多,导致出现问题。 如果开启了邮件服务,就会直接给root发送邮件。

Workaround:

1, delete a large number of small files/var/spool/postfix/maildrop/all files (Ls|xargs rm-f)
2. Temporarily open Postfix (sendmail) service
3. Vi/etc/crontab: Replace ' mailto=root ' with ' mailto= ' and then service Crond restart. (If not, crontab-e the first line adds mailto= "")

Mend

Timed tasks directed to empty >/dev/null 2>&1

Directory name explain
/var/spool/clientmqueue centos5.xSendMail temporary mail file directory, there are many reasons for this directory is a lot of broken files, such as crontab timed task commands do not add >/dev/null, and SendMail service did not open. Occasionally in the work because of the directory file too much, the number of partition Inode in/var is exhausted, unable to write to the file
/var/spool/postfix/maildrop/ centos6.xPostfix Temporary Queue directory/var/spool/postfix/maildrop/sends a message to root when the default scheduled task is executed, and if the mail service is not open, the message is pushed to the above directory. When the end of a scheduled task does not add >/dev/null 2>&1, the scheduled task will save a large number of small files in the above directory

System configuration file/etc/crontab for timed tasks
[Email protected] ~]# cat/etc/crontabshell=/bin/bash  path=/sbin:/bin:/usr/sbin:/usr/bin mailto=root  HOME= /  # 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
    • Shell=/bin/bash #shell解释器
    • Path=/sbin:/bin:/usr/sbin:/usr/bin #PATH环境变量
    • Mailto=root #定义如果任务有输出, to which user, default to the root user
    • home=/#定时任务执行命令从根目录开始

The root user execution Crontab-e command actually modifies the root file of/var/spool/cron/root

[[email protected] scripts]# cat/var/spool/cron/root  #time sync by ZCL at 2017/5/8*/5 * * * */usr/sbin/ntpdate time. Nist.gov >/dev/null 2>&1

  

Increased task frequency debugging tasks (some tasks cannot be used in a production environment)

When debugging, the task execution frequency speed up a bit, such as: every minute, every 5 minutes to execute, or 5 minutes later than the current time, see whether the execution, is not according to your imagination to carry out, if normal no problem, in the need to change the execution time of the task.

It is emphasized that some of the scheduled tasks are not allowed to be performed frequently, such as inserting data into a database at timed intervals, which are tested on a test machine and then deployed to a formal line so that there is less chance of a formal job problem.

Standardize the company development and operation Process, personal development and configuration environment--Office test Environment-->IDC Room test environment-->IDC Room of the formal environment.

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

Linux Timing Task Supplement

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.