脚本
#!/bin/bash
# 根据参数,执行进程的启动 停止 重启等
#source /etc/profile
# 非apache用户运行脚本,则退出
if [ `whoami` != "apache" ];then
echo " only apache can run me"
exit 1
fi
process='bin/geo'
##############$process###############
#1.启动 $process
start_geo() {
sleep 1
pid=`ps -ef |grep $process |grep -v grep |awk '{print $2}'`
if [ ! "$pid" ];then
echo "Starting $process process is $process;pid is $pid "
if [ $? -ne 0 ]; then
echo
exit 1
fi
cd /data/www/vhosts/test-api-location.aa.me/httpdocs/geo && nohup /data/www/vhosts/test-api-location.aa.me/httpdocs/geo/$process --config=config/uat_config.toml >/dev/null 2>&1 &
sleep 2
swoft_nu=`netstat -tnlp|grep 20109|wc -l`
if [[ $? == 0 && ${swoft_nu} -ge 1 ]];then
echo "start $process $process ok"
else
echo "start $process $process failed"
fi
else
echo "$process $process is still running!"
exit
fi
}
#2.停止 $process
stop_geo() {
echo -n $"Stopping $process $process: "
pid=`ps -ef |grep $process |grep -v grep |awk '{print $2}'`
if [ ! "$pid" ];then
echo "$process $process is not running"
else
ps -ef|grep ${process}|grep -v grep|awk '{print $2}'|xargs kill -9
sleep 2
swoft_nu=`netstat -tnlp|grep 20109|wc -l`
if [[ ${swoft_nu} -lt 1 ]];then
echo "stop $process $process ok killed $pid"
fi
fi
}
#3.重启 restart_geo
restart_geo() {
stop_geo
start_geo
}
#4.查看 $process 状态
status_geo(){
pid=`ps -ef |grep $process |grep -v grep |awk '{print $2}'`
if [ ! "$pid" ];then
echo "geo $process is not running"
else
echo "geo $process is running"
fi
}
##################### MAIN ###############################
usage () {
echo ""
echo " Please Input server infomation!"
echo ""
echo " USAGE: `basename $0` [start|stop|restart|status]"
echo ""
}
if [ $# != 1 ]
then
usage >&2
exit 1
fi
OPT=$1
case $OPT in
start)
echo "start `basename $0`"
start_geo
;;
stop)
stop_geo
;;
restart)
restart_geo
exit 0
;;
status)
status_geo
;;
*)
echo "Usage:`basename $0` [start|stop|restart|status]"
exit 1
esac
网上有说加 exit 0,或者去掉 source /etc/profile,加nohup或者sleep 1都无效,是因为没有使用终端无法正常退出,否则程序会终止,勾选pty即可