示例的守护脚本有具有以下功能:
- 守护脚本能以daemon模式进行自举
- 守护脚本运行期间,将目标程序重启时间节点及其pid记录在日志中
- 守护脚本的日志文件名称的末尾是守护脚本的pid
- 守护进程启动时会停止当前运行的守护进程
- 通过SIGTERM、SIGINT、SIGUSR1关闭守护进程,守护进程会关闭目标程序
- 守护进程在关闭守护进程或目标程序时,都会检测指定进程是否是守护进程或目标程序,以防误杀进程
#!/bin/bash
app=unimrcpserver
app_pid=-1
p='*/'
script_app=${0##$p}
exit_handler()
{
if (( ${app_pid} != -1 ));then
counter=`ps -fp ${app_pid}|grep ${app}|wc -l`
if (( ${counter} != 0 ));then
kill -s SIGKILL ${app_pid}
fi
app_pid=-1
wait
fi
exit
}
trap "exit_handler" EXIT SIGUSR1 SIGINT SIGTERM
# 进入脚本所在目录
p='/*'
cd "${0%$p}/"
log=${app}.start.info.*
p='*.'
for file in ${log};do
if test -e ${file};then
id=${file##$p}
counter=`ps -fp ${id}|grep ${s