Operation and maintenance experience sharing as a topic, currently a total of 7 articles
Operation and maintenance experience sharing (i)--Linux Shell's Chatterserver service control script
Operation and maintenance experience Sharing (ii)--Linux Shell's Chatterserver service control script two times optimization
Operation and maintenance experience Sharing (iii)--solving the problem of crontab not executing shell scripts correctly under Ubuntu (i)
Operation and maintenance experience sharing (IV.)--the analysis of the programming of service control scripts for Java process Management
Operation and maintenance experience Sharing (v)-Improved Java Process Management Service control script
Operation and maintenance experience sharing (vi)--crontab The problem of shell scripting not being executed correctly (ii)
Operation and maintenance experience Sharing (vii)--Linux Shell's Chatterserver Service Control script third optimization
==================================== Split Line ======================================
Previously in the "Operation and maintenance experience Sharing (iii)-to solve the problem of crontab not correctly executed in Ubuntu" article mentions Ubuntu under crontab because the default shell may cause Crontab to not execute correctly. This article summarizes and enumerates some of the crontab writing rules that require particular attention.
Note: The following rules apply to CentOS and Ubuntu, which are crontab issues that require special attention.
environment variable issue. System-brought environment variables will not be used, or can be used very little, so some of the script written in the obvious use of system environment variables in the Crontab scheduled task execution failure, typically Java program, because the Java program to Java_home and Jre_ Home These two environment variables have a lot of dependence. In addition to Java programs, the more typical is the path variable, if the path variable is not declared, then the command in the shell will not be used, so in the execution of Crontab scheduled task, it is best to declare the path variable and related variables, In addition to using export can also use declare more usable key=value way to declare, examples please refer to the example of the end of the article.
-
Relative path and absolute path problem. Crontab does not support relative paths, such as simple ".", ".", "./", ". /"And so on, if there is a shell script such as". ",".. ", CD, $ (PWD), etc. such use requires special attention, the best solution is to invoke another shell script directly in crontab, the shell script first specifies the shell to execute (such as/ Bin/bash), set the current environment variable, and then write the shell script that you want to actually execute, so how to execute in the shell script can be executed in Crontab's scheduled task, no need to worry about the command line can perform successfully, and in the crontab do not succeed in the strange phenomenon
-
(supplementing the knowledge of the previous article) crontab the default shell issue.
Example:
Crontab Scheduled Tasks:
The correct wording:
*/1 * * * */data/chatterserver-test/cron_chatter.sh
The wrong wording:
*/1 * * * * cd/data/chatterserver-test &&/bin/bash/data/chatterserver-test/chatter.sh Restart >/tmp/cron_ch atter-test/$ (date +\%y\%m\%d\%h\%m\%s) 2>&1
Crontab Call Script (/data/chatterserver-test/cron_chatter.sh):
#!/bin/bashdeclare -x classpath= ".:/ usr/lib/jvm/jdk1.8.0_40/lib/dt.jar:/usr/lib/jvm/jdk1.8.0_40/lib/tools.jar:/usr/lib/jvm/jdk1.8.0_40/lib:/usr/ Lib/jvm/jdk1.8.0_40/jre/lib "declare -x display=" localhost:11.0 "Declare -x home="/home/vivo "Declare -x java_home="/usr/lib/jvm/jdk1.8.0_40 "Declare -x jre_home="/usr/lib/jvm/jdk1.8.0 _40/jre "declare -x lang=" ZH_CN. UTF-8 "declare -x language=" Zh_CN:zh:en_US:en "declare -x lc_all=" ZH_CN. UTF-8 "declare -x lessclose="/usr/bin/lesspipe %s %s "declare -x lessopen=" | /usr/bin/lesspipe %s "Declare -x logname=" Vivo "declare -x ls_colors=" rs=0:di=01;34:ln= 01;36:mh=00:pi=40;33:so=01;35:do=01;35:bd=40;33;01:cd=40;33;01:or=40;31;01:su=37;41:sg=30;43:ca=30;41:tw=30;42 : ow=34;42:st=37;44:ex=01;32:*.tar=01;31:*.tgz=01;31:*.arj=01;31:*.taz=01;31:*.lzh=01;31:*.lzma=01;31:*.tlz=01; 31:*.txz=01;31:*.zip=01;31:*.z=01;31:*. z=01;31:*.dz=01;31:*.gz=01;31:*.lz=01;31:*.xz=01;31:*.bz2=01;31:*.bz=01;31:*.tbz=01;31:*.tbz2=01;31:*.tz=01;31:*.deb=01;31:*. rpm=01;31:*.jar=01;31:*.war=01;31:*.ear=01;31:*.sar=01;31:*.rar=01;31:*.ace=01;31:*.zoo=01;31:*.cpio=01;31:*. 7z=01;31:*.rz=01;31:*.jpg=01;35:*.jpeg=01;35:*.gif=01;35:*.bmp=01;35:*.pbm=01;35:*.pgm=01;35:*.ppm=01;35:*.tga =01;35:*.xbm=01;35:*.xpm=01;35:*.tif=01;35:*.tiff=01;35:*.png=01;35:*.svg=01;35:*.svgz=01;35:*.mng=01;35:*.pcx =01;35:*.mov=01;35:*.mpg=01;35:*.mpeg=01;35:*.m2v=01;35:*.mkv=01;35:*.webm=01;35:*.ogm=01;35:*.mp4=01;35:*.m4v =01;35:*.mp4v=01;35:*.vob=01;35:*.qt=01;35:*.nuv=01;35:*.wmv=01;35:*.asf=01;35:*.rm=01;35:*.rmvb=01;35:*.flc= 01;35:*.avi=01;35:*.fli=01;35:*.flv=01;35:*.gl=01;35:*.dl=01;35:*.xcf=01;35:*.xwd=01;35:*.yuv=01;35:*.cgm=01; 35:*.emf=01;35:*.axv=01;35:*.anx=01;35:*.ogv=01;35:*.ogx=01;35:*.aac=00;36:*.au=00;36:*.flac=00;36:*.mid=00;36 :*. midi=00;36:*.mka=00;36:*.mp3=00;36:*.mpc=00;36:*.ogg=00;36:*.ra=00;36:*.wav=00;36:*.axa=00;36:*.oga=00;36:* . spx=00;36:*.xSPF=00;36: "declare -x mail="/var/mail/vivo "Declare -x maven_home="/usr/local/maven/ apache-maven-3.2.5 "declare -x oldpwd="/home/vivo "declare -x path="/usr/lib/jvm/jdk1.8.0_40 /bin:/usr/local/maven/apache-maven-3.2.5/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr /games:/usr/local/games "declare -x pwd="/data/chatterserver-test "declare -x shell="/bin/ Bash "declare -x shlvl=" 1 "# declare -x ssh_client=" 58.56.178.146 56404 12322 "# declare -x ssh_connection=" 58.56.178.146 56404 58.67.204.13 12322 "# Declare -x ssh_tty= "/DEV/PTS/5" declare -x term= "xterm" declare -x user= "Vivo" Declare -x xdg_runtime_dir= "/run/user/1000" declare -x xdg_session_id= "380" cd /data/ chatter-test/bin && /bin/bash /data/chatter-test/bin/chatter.sh restart >/ tmp/cron_chatter-test/$ (date +\%y\%m\%d\%h\%m\%s) 2>&1
--end--
==================================== Split Line ======================================
Operation and maintenance experience sharing as a topic, currently a total of 7 articles
Operation and maintenance experience sharing (i)--Linux Shell's Chatterserver service control script
Operation and maintenance experience Sharing (ii)--Linux Shell's Chatterserver service control script two times optimization
Operation and maintenance experience Sharing (iii)--solving the problem of crontab not executing shell scripts correctly under Ubuntu (i)
Operation and maintenance experience sharing (IV.)--the analysis of the programming of service control scripts for Java process Management
Operation and maintenance experience Sharing (v)-Improved Java Process Management Service control script
Operation and maintenance experience sharing (vi)--crontab The problem of shell scripting not being executed correctly (ii)
Operation and maintenance experience Sharing (vii)--Linux Shell's Chatterserver Service Control script third optimization
This article is from "Communication, My Favorites" blog, please make sure to keep this source http://dgd2010.blog.51cto.com/1539422/1677211
Operation and maintenance experience sharing (vi)--crontab The problem of not executing shell scripts correctly (ii)