为了能将自己写的一个监视linux系统服务或者是自己写的监视应用服务的程序做成符合linux的规范,特写了如下程序,如果次程序命名为dcp_im_server,可以使用一下命令来启动 monitorserver.sh脚本:service dcp_im_server start/stop/restart 启动.停止.重启 该脚本。
下面是dcp_im_server脚本代码:
#!/bin/bash
. /etc/init.d/functions
# The daemon name
prog="monitorserver.sh"
# Path of the daemon
path="/opt/dcp/IM2000/bin"
lockfile=/var/lock/subsys/$prog
start() {
[ -x $path/$prog ] || exit 5
# Start daemons.
SUM=`ps aux | grep $path/$prog | grep -v grep | wc -l`
if [ $SUM -eq 1 ]
then
echo "$prog is running"
exit 7
elif [ $SUM -gt 1 ]
then
echo -e "\033[0;31m\033[5mWARNING:\033[0m\033[0;32m$SUM $prog process is already running.Before start $prog,all of $prog process will be terminated \033[0m"
killproc $prog
fi
echo -n $"Starting $prog: "
su -dcp -c $path/$prog &
RETVAL=$?
if [ $RETVAL -eq 0 ]
then
echo_success
else
echo_failure
fi
echo
[ $RETVAL -eq 0 ] && touch $lockfile
}
stop() {
echo -n $"Shutting down $prog: "
killproc $prog
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f $lockfile
}
# See how we were called.
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status $prog
;;
restart|force-reload)
stop
start
;;
try-restart|condrestart)
if status $prog > /dev/null; then
stop
start
fi
;;
reload)
exit 3
;;
*)
echo $"Usage: $0 {start|stop|status|restart|try-restart|force-reload}"
exit 2
esac
阅读(1177) | 评论(0) | 转发(0) |