Linux scheduled task supplement and Linux task supplement

Source: Internet
Author: User

Linux scheduled task supplement and Linux task supplement

 

Scheduled task instances

Example 1: print your name to the "/server/log/file named after your name" every minute.

[Root @ chengliang log] # mkdir-p/server/log/[root @ chengliang log] # echo "chensiqi">/server/log/chengliang [root @ chengliang log] # crontab-l # time sync by zcl at least /5/8 */5 *****/usr/sbin/ntpdate time.nist.gov>/dev/null 2> & 1 [root @ chengliang log] # crontab-e ==> set a scheduled task, add the following two sentences # print my name to log by zcl at */1 *****/bin/echo "my name is zcl">/server/log/chengliang [root @ chengliang log] #

Error example:

# Print my name to log by chensiqi at 2017211 */1 *****/bin/echo "chensiqi">/server/log/chensiqi> dev/null 2> & 1 tip: this is a wrong scheduled task. Please think about it?

Summary:

1. Add comments to the scheduled task
2. do not end>/Dev/null 2> & 1
3. The/server/log directory must exist before the result can be generated. If this directory is not created.
4. The path in the scheduled task must be an absolute path.
5. The crond service must be enabled first.
6. view the scheduled task log tail/var/log/cron

 

Example 2: Run/server/scripts/chensiqi every Saturday, nine o'clock A.M. and fourteen o'clock P.M. sh ). requirements:/server/scripts/chensiqi. THE sh script is used to print the date of the current day. The format is.

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 a directory

mkdir /server/scripts -p

2. Command Line Testing

[root@chengliang log]# date +%F2017-05-14[root@chengliang log]# 

3. write scripts

[root@chengliang scripts]# echo 'date +%F' >/server/scripts/chensiqi.sh[root@chengliang scripts]# cat chengliang.shdate +%F[root@chengliang scripts]# 

4. Command Line Test script

[root@chengliang scripts]# /bin/sh /server/scripts/chengliang.sh2017-05-14[root@chengliang scripts]# 

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

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

6. Test

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

7. Change the time of the scheduled task as required.

##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 scheduled task writing
  • Essentials 1: add necessary comments to the scheduled task rules

Add necessary comments: It is a good habit and standard to add comments (preferably in English) when writing scheduled task rules as much as possible.
For example, WHO has done what (comment content) at what time, what time, what, and what has been done. If these are clearly marked, other O & M personnel can easily understand the task information, thus improving the Team's work efficiency.

  • Method 2: Add/bin/sh before executing the shell script task

When executing a scheduled task, if you want to execute the script, add the/bin/sh command before the script. Otherwise, you may forget to set the execution permission (x) for the script ), so I thought it was okay and the task could not be completed. This would be a "Tragedy ".

  • Essentials 3: Add/dev/null at the end of the scheduled Task Command or script. 2> & 1

It is best to add>/dev/null 2> & 1 to the end of a scheduled task (generally a script task) rule. If you need to print logs, you can append it to the specified log file (do not exist with/dev/null at this time). Try not to leave it blank. If the task is a command, use ">/dev/null 2> & 1" at the end of the test to perform more tests. Check methods are required. For example :*/1* *** Echo "=">/tmp/chensiqi. log>/dev/null 2> & 1 the task rule cannot be executed.

  • Method 4: execute more than two scheduled task commands. It is best to use a script file.

Execute more than two commands. It is best to use a script file. The following method is nonstandard and unprofessional.

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

Standard writing:

[root@chensiqi /]# cat /server/scripts/log.shsleep1echo chensiqi >> /server/log/chensiqi.log
  • Essentials 7: it is best to write the scheduled Task Command or program to the script for execution.

  • Essentials 8: standardize the path of scripts for scheduled task execution

    • Example:/server/scripts
  • Essentials 9: Configure scheduled tasks to standardize the operation process to prevent errors.

1. First, you must operate successfully on the command line, and then copy the successful command to the script to reduce the chance of errors in each small step.
2. Test the script. After the test is successful, copy the script's standard path to the scheduled task configuration.
3. First test in the test environment, and then formally deploy the environment specification.

 

In the command line, enter./chensiqi. sh (/server/scripts/chensiqi. sh) and sh chensiqi. sh. What is the difference?

[root@chengliang scripts]# lschengliang.sh[root@chengliang scripts]# sh chengliang.sh2017-05-14[root@chengliang scripts]# [root@chengliang scripts]# ./chengliang.sh-bash: ./chengliang.sh: Permission denied[root@chengliang scripts]# 

Command Description: sh chensiqi. sh indicates to use the/bin/sh command to parse and start the script chensiqi. sh. And./chensiqi. sh indicates to use the default interpreter of linux to parse and start this script. Therefore,./chensiqi. sh requires the execution permission of x in linux, while sh chensiqi. sh does not.

 

The consequence of not adding scheduled tasks>/dev/null 2> & 1
 
 
  • If you do not add command configuration such as>/dev/null 2> & 1 at the end of the scheduled task rule, there may be a large amount of output information for a long time, the system may not enable the mail service, which may cause the number of temporary mail directories/var/spool/clientmqueue files to soar, A large number of files occupy a large number of disk inode nodes (each file occupies an inode), so that the disk inode is full and normal data cannot be written (in the following example ).
  • Tip: The above>/dev/null 2> & 1 can also be written as 1>/dev/null 2>/dev/null, for example:$JAVA-jar $RESIN_HOME/lib/resin.jar $ARGS stop 1>/dev/null 2>/dev/nullThe default startup script of resin is used.
  • The above is the case of centos5.8. If sendmail (Centos6.4) is not installed in the system, is there no such problem?

 

Enterprise case:If you do not add>/dev/null 2> & 1 at the end of the scheduled task rule, the hard disk inode space is easily occupied and the system service is abnormal.

When a scheduled task is executed, an email is sent to the system. The sendmail mail service is often disabled, so the mails sent by the scheduled task will be temporarily piled up in/var/spool/clientmqueue/for a long time, /var/spool/clientmqueue/The number of files is very large. This problem must occur in Centos5.

What about Centos6? Read down.

[root@chensiqi1 ~]# cat /etc/redhat-release CentOS release 6.8 (Final)[root@chensiqi1 ~]# crontab -l #go to chensiqi trainning by chensiqi at 20170211* * * * * /bin/sh /root/chensiqi.sh[root@chensiqi1 ~]# ls /var/spool/postfix/maildrop/D3AD0C6  DB2BAC9  E14E6D0  E5222D1[root@chensiqi1 ~]# 

Command description:

If the scheduled task is not targeted to a blank file and the postfix service is not enabled, a small file is generated in the/var/spool/postfix/maildrop/folder for each scheduled task, as time goes on, more and more problems will occur. If the mail service is enabled, an email is sent directly to the root user.

Solution:

1. delete a large number of small files/var/spool/postfix/maildrop/all files (ls | xargs rm-f)
2. Temporarily enable the postfix (sendmail) Service
3. vi/etc/crontab: replace 'mailto = root' with 'mailto = "" and then use service crond restart. (If not, add MAILTO = "" to the first line of crontab-e.)

 

Make up for it:

Scheduled task is directed to null>/dev/null 2> & 1

Directory Name Explanation
/Var/spool/clientmqueue centos5.xThe sendmail temporary mail file directory contains many broken files due to many reasons. For example, the crontab scheduled task command does not include>/dev/null, And the sendmail service is unavailable. Occasionally, the directory contains too many files, causing the number of inode partitions in/var to be exhausted and the file cannot be written.
/Var/spool/postfix/maildrop/ centos6.xThe postfix temporary queue directory/var/spool/postfix/maildrop/will send an email to the root user when the scheduled task is executed by default. If the mail service is unavailable, the email will be pushed to the preceding directory. When no>/dev/null 2> & 1 is added at the end of the scheduled task, the scheduled task stores a large number of small files in the preceding directory.

 

System configuration file/etc/crontab of the scheduled task
[root@chensiqi1 ~]# cat /etc/crontabSHELL=/bin/bash  PATH=/sbin:/bin:/usr/sbin:/usr/bin MAILTO=root  HOME=/  # For details see man 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 interpreter
  • PATH =/sbin:/bin:/usr/sbin:/usr/bin # PATH Environment Variable
  • MAILTO = root # define the user to which the task is sent if the task has output, and send it to the root user by default.
  • HOME = // # scheduled task execution command starts from the root directory

The root user executes the crontab-e command and actually modifies the root file of/var/spool/cron/root.

[root@chengliang 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

  

Add task execution frequency debugging tasks (some tasks cannot be used in the production environment)

During debugging, the task execution frequency is adjusted a little faster, for example, every minute, once every five minutes, or five minutes later than the current time to see if the task can be executed, do you want to execute the task as expected? If it is normal, you need to change the execution time of the required task.

Emphasize: Some scheduled tasks are not allowed to be executed frequently. For example, if data is inserted into the database at regular intervals, such tasks should be tested on the test machine and then deployed to the official production environment, in this way, there will be fewer opportunities for formal issues.

Standardized Operation Procedures for company development and O & M personnel, personal development and configuration environment --> office test environment --> idc data center formal environment.

 

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

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.