标签:salt linux saltstack servie脚本
SaltStack通过Service方式管理服务,对于通用的服务,如mysql、apache、php等,自带service服务管理脚本,SaltStack可以方便管理。但是对于一些公司自有的服务,可能这些服务在开发之初,并没有考虑日后会采用Service脚本进行服务管理,为了统一Salt对服务的管理,我采用的方式是为每一个程序编写Service脚本。
这里主要注意以下两个问题:
1.这里有部分机器的优化,放在/etc/profile和/root/.bashrc中,而这两个文件在用户登陆的时候,会自动执行,而通过salt管理服务器,并不会执行这两个文件,所以在脚本中有导入这两个文件的操作;
2.之前服务启动的方式为nohup prog_name &这种形式,这个命令执行过后,还要有个敲回车键的操作,来确认结果输出到nohup.out文件,这里通过制定输出文件为nohup.out的方式,避免这个操作。
脚本如下:
#!/bin/sh
# FileName: MobileStkServerd
# Description: This shell script takes care of starting and stopping MobileStkServer
# Guibin created on Oct. 17th, 2014
# version 1.2-2014.11.07
# Source env
. /etc/profile
. /root/.bashrc
#the service name for example: MobileStkServer
FileName=MobileStkServerd
DAEN_NAME=MobileStkServer
DAEN_PATH=/home/MobileServer/
#the full path and name of the daemon DAEN_RUNram
#Warning: The name of executable file must be identical with service name
DAEN_RUN="nohup ./$DAEN_NAME > nohup.out &"
# check program file
check_profile(){
PRO_FILE="$DAEN_PATH$DAEN_NAME"
if [ ! -f "$PRO_FILE" ]
then
echo "$PRO_FILE: No such file or directory"
exit 1
elif [ ! -x "$PRO_FILE" ]
then
echo "$PRO_FILE: Permission denied"
exit 1
fi
}
# status function
check_status(){
check_profile
DAEN_PID=‘‘
DAEN_PID=$(ps -ef|grep "$DAEN_NAME"|grep -v grep|grep -v "$FileName"|awk ‘{print $2}‘)
if [ -n "$DAEN_PID" ]
then
DAEN_STATUS=‘sstart‘
else
DAEN_STATUS=‘sstop‘
fi
}
status(){
check_status
if [ "$DAEN_STATUS"x == ‘sstartx‘ ]
then
echo -e "$DAEN_NAME is running with pid: $DAEN_PID"
else
echo -e "$DAEN_NAME is not running!"
fi
}
# start function
start() {
#check the daemon status first
check_status
if [ "$DAEN_STATUS"x == ‘sstartx‘ ]
then
echo "$DAEN_NAME has already started!"
else
cd $DAEN_PATH
printf "Starting $DAEN_NAME ..."
eval $DAEN_RUN
sleep 1
DAEN_PID=$(ps -ef|grep "$DAEN_NAME"|grep -v grep|grep -v "$FileName"|awk ‘{print $2}‘)
if [ -n "$DAEN_PID" ]
then
echo -e "\t\t\t\t[ OK ]"
echo -e "$DAEN_NAME is running with pid: $DAEN_PID"
else
echo -e "\t\t\t\t[ Failed ]"
exit 1
fi
fi
}
#stop function
stop() {
#check the daemon status first
check_status
if [ "$DAEN_STATUS"x == ‘sstopx‘ ]
then
echo "$DAEN_NAME has already stopped!"
else
i=0
while [ $i -lt 10 ]
do
`/sbin/pidof $DAEN_NAME |xargs /bin/kill`
sleep 1
check_status
if [ "$DAEN_STATUS"x == ‘sstopx‘ ]
then
echo -e "Stopping $DAEN_NAME ...\t\t\t\t[ OK ]"
break
else
i=`expr $i+1`
fi
done
check_status
if [ "$DAEN_STATUS"x == ‘sstartx‘ ]
then
echo -e "Stopping $DAEN_NAME ...\t\t\t\t[ Failed ]"
exit 1
fi
fi
}
case "$1" in
start)
start
;;
stop)
stop
;;
reload|restart)
stop
sleep 2
start
;;
status)
status
;;
*)
echo $"Usage: $0 {start|stop|restart|status|reload}"
exit 1
esac本文出自 “Guibin的博客” 博客,请务必保留此出处http://guibin.blog.51cto.com/8909901/1577396
标签:salt linux saltstack servie脚本
原文地址:http://guibin.blog.51cto.com/8909901/1577396