# chkconfig: - 85 15
# description: xxxx is a xxxxx server. It is used to serve
#!/bin/bash
DAEMONPATH=/usr/bin
#SERVICE=node_linux
NUM=`getconf LONG_BIT`
getPid(){
#将命令结果变为数组
#echo "$SERVICE"
var=($(ps aux | grep $SERVICE_BAK | grep -v grep | awk '{print $2,$11}'))
#获取数组长度
#echo ${#var[@]}
for i in $(seq ${#var[@]} -1 1)
do
if [ $(($i % 2)) = 0 ]
then
#找到对应的pid号
cmd=$( expr ${var[$(($i - 1 ))]} : "^\(/usr.*$SERVICE_BAK\)$" )
if [[ $cmd = $DAEMONPATH/$SERVICE_BAK ]]
then
echo ${var[$(($i - 2 ))]}
exit 0
fi
fi
done
exit 1
}
start() {
pid=$( getPid )
if [ -z "$pid" ];then
$DAEMONPATH/$SERVICE_BAK &
pid=$( getPid )
if [ -z "$pid" ];then
echo "$SERVICE start error"
exit 2
else
echo "$SERVICE start success"
exit 0
fi
else
echo "$SERVICE is running(pid :$pid)"
exit 0
fi
exit 0
}
stop() {
pid=$( getPid )
if [ -z "$pid" ];then
echo "$SERVICE is aleady stop"
exit 0
else
kill -9 $pid
pid=$( getPid )
if [ -z "$pid" ];then
echo "$SERVICE stop success"
exit 0
else
echo "$SERVICE stop error"
exit 2
fi
fi
exit 2
}
restart() {
pid=$( getPid )
if [ -z "$pid" ];then
echo "$SERVICE is stop"
echo "try to start $SERVICE"
start
else
echo "$SERVICE is running(pid :$pid)"
kill -9 $pid
pid=$( getPid )
if [ -z "$pid" ];then
echo "$SERVICE stop success"
else
echo "$SERVICE stop error"
exit 2
fi
start
fi
exit 0
}
status(){
pid=$( getPid )
if [ -z "$pid" ];then
echo "$SERVICE is stop"
exit 1
else
echo "$SERVICE is running(pid :$pid)"
exit 0
fi
exit 1
}
SERVICE=$1
SERVICE_BAK=`echo "$SERVICE-amd$NUM"`
case "$1" in
cloudmonitor)
case "$2" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
*)
echo "Usage:service $1 {start|stop|restart|status}"
exit 2
;;
esac
;;
cloudmonitor_daemon)
case "$2" in
start)
start
;;
stop)
stop
;;
restart)
restart
;;
status)
status
;;
*)
echo "Usage:service $1 {start|stop|restart|status}"
exit 2
;;
esac
;;
*)
echo "Usage:service service_name{cloudmonitor_daemon|cloudmonitor}"
exit 2
;;
esac