首先我们以ubuntu下的redis服务器启动脚本说起。
#!/bin/sh ###BEGININITINFO #Provides:redis-server #required-Start:$syslog$remote_fs #required-Stop:$syslog$remote_fs #Should-Start:$local_fs #Should-Stop:$local_fs #Default-Start:2345 #Default-Stop:016 #Short-Description:redis-server-Persistentkey-valuedb #Description:redis-server-Persistentkey-valuedb ###ENDINITINFO PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin DAEMON=/usr/bin/redis-server DAEMON_ARGS=/etc/redis/redis.conf NAME=redis-server DESC=redis-server RUNDIR=/var/run/redis PIDFILE=$RUNDIR/redis-server.pid test-x$DAEMON||exit0 if[-r/etc/default/$NAME] then ./etc/default/$NAME fi ./lib/lsb/init-functions set-e case"$1"in start) echo-n"Starting$DESC:" mkdir-p$RUNDIR touch$PIDFILE chownredis:redis$RUNDIR$PIDFILE chmod755$RUNDIR if[-n"$ULIMIT"] then ulimit-n$ULIMIT fi ifstart-stop-daemon--start--quiet--umask007--pidfile$PIDFILE--chuidredis:redis--exec$DAEMON--$DAEMON_ARGS then echo"$NAME." else echo"Failed" fi ;; stop) echo-n"Stopping$DESC:" ifstart-stop-daemon--stop--retryforever/TERM/1--quiet--oknodo--pidfile$PIDFILE--exec$DAEMON then echo"$NAME." else echo"Failed" fi rm-f$PIDFILE sleep1 ;; restart|force-reload) ${0}stop ${0}start ;; status) echo-n"$DESCis" ifstart-stop-daemon--stop--quiet--signal0--name${NAME}--pidfile${PIDFILE} then echo"running" else echo"notrunning" exit1 fi ;; *) echo"Usage:/etc/init.d/$NAME{start|stop|restart|force-reload|status}">&2 exit1 ;; esac exit0
ubuntu下的MysqL服务启动脚本
#!/bin/bash # ###BEGININITINFO #Provides:MysqL #required-Start:$remote_fs$syslog #required-Stop:$remote_fs$syslog #Should-Start:$network$time #Should-Stop:$network$time #Default-Start:2345 #Default-Stop:016 #Short-Description:StartandstoptheMysqLdatabaseserverdaemon #Description:ControlsthemainMysqLdatabaseserverdaemon"MysqLd" #anditswrapperscript"MysqLd_safe". ###ENDINITINFO # set-e set-u ${DEBIAN_SCRIPT_DEBUG:+set-v-x} test-x/usr/bin/MysqLd_safe||exit0 ./lib/lsb/init-functions SELF=$(cd$(dirname$0);pwd-P)/$(basename$0) CONF=/etc/MysqL/my.cnf MYADMIN="/usr/bin/MysqLadmin--defaults-file=/etc/MysqL/debian.cnf" #prioritycanbeoverridenand"-s"addsoutputtostderr ERR_LOGGER="logger-pdaemon.err-t/etc/init.d/MysqL-i" #Safeguard(relativepaths,coredumps..) cd/ umask077 #MysqLadminlikestoread/root/.my.cnf.ThisisusuallynotwhatIwant #asmanyadminse.g.onlystoreapasswordwithoutausernamethereand #sobreakmyscripts. exportHOME=/etc/MysqL/ ##FetchaparticularoptionfromMysqL'sinvocation. # #Usage:voidMysqLd_get_paramoption MysqLd_get_param(){ /usr/sbin/MysqLd--print-defaults\ |tr"""\n"\ |grep--"--$1"\ |tail-n1\ |cut-d=-f2 } ##DosomesanitychecksbeforeeventryingtostartMysqLd. sanity_checks(){ #checkforconfigfile if[!-r/etc/MysqL/my.cnf];then log_warning_msg"$0:WARNING:/etc/MysqL/my.cnfcannotberead.SeeREADME.Debian.gz" echo"WARNING:/etc/MysqL/my.cnfcannotberead.SeeREADME.Debian.gz"|$ERR_LOGGER fi #checkfordiskspaceshortage datadir=`MysqLd_get_paramdatadir` ifLC_ALL=CBLOCKSIZE=df--portability$datadir/.|tail-n1|awk'{exit($4>4096)}';then log_failure_msg"$0:ERROR:Thepartitionwith$datadiristoofull!" echo"ERROR:Thepartitionwith$datadiristoofull!"|$ERR_LOGGER exit1 fi } ##Checksifthereisaserverrunningandifsoifitisaccessible. # #check_aliveinsistsonapingableserver #check_deadalsofailsifthereisalostMysqLdintheprocesslist # #Usage:booleanMysqLd_status[check_alive|check_dead][warn|nowarn] MysqLd_status(){ ping_output=`$MYADMINping2>&1`;ping_alive=$((!$?)) ps_alive=0 pidfile=`MysqLd_get_parampid-file` if[-f"$pidfile"]&&ps`cat$pidfile`>/dev/null2>&1;thenps_alive=1;fi if["$1"="check_alive"-a$ping_alive=1]|| ["$1"="check_dead"-a$ping_alive=0-a$ps_alive=0];then return0#EXIT_SUCCESS else if["$2"="warn"];then echo-e"$ps_aliveprocessesaliveand'$MYADMINping'resultedin\n$ping_output\n"|$ERR_LOGGER-pdaemon.debug fi return1#EXIT_FAILURE fi } # #main() # case"${1:-''}"in 'start') sanity_checks; #Startdaemon log_daemon_msg"StartingMysqLdatabaseserver""MysqLd" ifMysqLd_statuscheck_alivenowarn;then log_progress_msg"alreadyrunning" log_end_msg0 else #Couldberemovedduringboot test-e/var/run/MysqLd||install-m755-oMysqL-groot-d/var/run/MysqLd #StartMysqL! /usr/bin/MysqLd_safe>/dev/null2>&1& #6swasreportedin#352070tobetoofewwhenusingndbcluster #14swasreportedin#736452tobetoofewwithlargeinstalls foriin$(seq130);do sleep1 ifMysqLd_statuscheck_alivenowarn;thenbreak;fi log_progress_msg"." done ifMysqLd_statuscheck_alivewarn;then log_end_msg0 #NowstartMysqLcheckorwhatevertheadminwants. output=$(/etc/MysqL/debian-start) [-n"$output"]&&log_action_msg"$output" else log_end_msg1 log_failure_msg"Pleasetakealookatthesyslog" fi fi ;; 'stop') #*AsapasswordlessMysqLadmin(e.g.via~/.my.cnf)mustbepossible #atleastforcron,wecanrelyonithere,too.(althoughwehave #tospecifyitexplicitase.g.sudoenvironmentspointstothenormal #usershomeandnot/root) log_daemon_msg"StoppingMysqLdatabaseserver""MysqLd" if!MysqLd_statuscheck_deadnowarn;then set+e shutdown_out=`$MYADMINshutdown2>&1`;r=$? set-e if["$r"-ne0];then log_end_msg1 ["$VERBOSE"!="no"]&&log_failure_msg"Error:$shutdown_out" log_daemon_msg"KillingMysqLdatabaseserverbysignal""MysqLd" killall-15MysqLd server_down= foriin12345678910;do sleep1 ifMysqLd_statuscheck_deadnowarn;thenserver_down=1;break;fi done iftest-z"$server_down";thenkillall-9MysqLd;fi fi fi if!MysqLd_statuscheck_deadwarn;then log_end_msg1 log_failure_msg"PleasestopMysqLmanuallyandread/usr/share/doc/MysqL-server-5.5/README.Debian.gz!" exit-1 else log_end_msg0 fi ;; 'restart') set+e;$SELFstop;set-e $SELFstart ;; 'reload'|'force-reload') log_daemon_msg"ReloadingMysqLdatabaseserver""MysqLd" $MYADMINreload log_end_msg0 ;; 'status') ifMysqLd_statuscheck_alivenowarn;then log_action_msg"$($MYADMINversion)" else log_action_msg"MysqLisstopped." exit3 fi ;; *) echo"Usage:$SELFstart|stop|restart|reload|force-reload|status" exit1 ;; esac