Linux service scripting

Source: Internet
Author: User

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465 6667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611 7118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157 158159160161162163164 #!/bin/bash# # # BEGIN INIT INFO# provides: App developer# Required-start: $all# required-stop: $all# Default-start:2 3 4 5# default-stop:0 1 6# Short-description: Short Application Description# Description: Detailed Application Description# # # END INIT INFO# Program Files directoryroot="/work/code/cusflo-server-moon"# Program Filesapp="./dog/client_update_dog.py"# program configuration fileconfig="./server_config.ini"# Service Logdefault_log_file='./daodaoliang.log '# Style CustomizationRed="\\e[31m"green="\\e[32m"yellow="\\e[33m"black="\\e[0m"pos="\\e[60g"ok_msg () {Echo- e "${1}${pos}${black}[${green} OK ${black}]" }failed_msg () {Echo- e "${1}${pos}${black}[${red}failed${black}]" }# Load Process info of app# exist return 0# no exist return 1Load_process_info () {# get PIDapp_pid=$ (Ps-ef|grep${app}|gawk ' $!~/grep/{print $} ' |tr- s ' \ n ' ) # Check PID againPs-p${app_pid} >/dev/null 2>/dev/nullRet=$?;if [[0 -ne $ret]]; Then error_msg=,"process $app _pid does not exists"; return 1; fi return 0;}start () {# if exists, exit.Load_process_infoif [[0 -eq $?]]; Then failed_msg "${app} started (PID ${app_pid}), should not start it again."; return 0; fi # NOT EXISTS, start serverOk_msg"Starting ${app} ..." # get log fileLog_file= 'CD ${root} && cat ${config} |grep ' ^log_file ' | awk ' {print $} ' | awk-f '; ' ' {print '} ' if [[-Z $log _file]]; Then log_file=${default_log_file}; fi # Get ABS pathLog_dir= ' DirName$log _file 'Log_file= ' (CD ${root} && cd $log _dir && pwd) '/' basename $log _file ' # TODO: fixme:set limit by, for instance, "ULIMIT-HSN 10000" if [[-Z $log _file]]; Then(cd ${root};nohup python ${app} >/dev/null 2>&1 &) Else(cd ${root};nohup python ${app} >/dev.null >> $log _file 2>&1 &) fi# Check again after start serverFor ((i = 0; i < 5; i++)); Do# Sleep A little while, the for app could start then crash.Sleep 0.1load_process_inforet=$?;if [[0 -ne $ret]]; ThenFailed_msg"${app} start failed"; Failed_msg"See $log _file"; return $ret; fi Done# Check whether started.Load_process_inforet=$?;if [[0 -eq $?]]; Then ok_msg "${app} started (PID ${app_pid})"; return 0; fi Failed_msg"${app} not started" return $ret }Stop () {# Not start, exitLoad_process_infoif [[0 -ne $?]]; Then failed_msg "${app} not start."; return 0; fi Ok_msg"Stopping ${app} (PID ${app_pid}) ..." # Process exists, try to kill to stop normallyFor ((i=0;i<30;i++)); DoLoad_process_infoif [[0 -eq $?]]; ThenKill- s SIGTERM ${app_pid} 2>/dev/null Ret=$?;if [[0 -ne $ret]]; Then failed_msg "Send signal SIGTERM failed ret=$ret"; return $ret; fi Sleep 0.1ElseOk_msg"${app} stopped by SIGTERM" # Delete The PID file when stop success.Rm- f ${pid_file} Break ;fi Done# process exists, use kill-9 to force to exitLoad_process_infoif [[0 -eq $?]]; ThenKill- s SIGKILL ${app_pid} 2>/dev/null Ret=$?;if [[0 -ne $ret]]; Then failed_msg "Send signal SIGKILL failed ret=$ret"; return $ret; fi Ok_msg"${app} stopped by SIGKILL" Else# Delete The PID file when stop success.Rm- f ${pid_file} fiSleep 0.1return 0}# Get the status of the SRS process# @return 0 if SRS is running; otherwise, 1 for stopped.status () {Load_process_inforet=$?;if [[0 -eq $ret]]; then echo '${app} (PID ${app_pid}) is running. "; return 0; fi echo "${app} is stopped, $error _msg" return 1}menu () {case "$" inStart) Start; stop) stop;; restart) Stopstart;; status) status; *)echo "Usage: $ {start|stop|status|restart}" return 1;;Esac} menu $Code=$?exit ${code}
http://daodaoliang.com/linux%E5%AD%A6%E4%B9%A0/2015/02/04/linux%E5%AD%A6%E4%B9%A0-2015-02-04-%E6%9C%8D%E5%8A% a1%e8%84%9a%e6%9c%ac%e7%bc%96%e5%86%99/

Linux service scripting

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.