Shell代码
#!/bin/bash
#description: 启动重启server服务
#端口号,根据此端口号确定PID
PORT1=80
PORT2=8080
#启动命令所在目录
HOME='/home/javajar'
APP1_NAME=/home/javajar/admin-0.0.1-SNAPSHOT.jar
APP2_NAME=/home/javajar/app-0.0.1-SNAPSHOT.jar
#查询出监听了PORT端口TCP协议的程序
pid1=`ps -ef|grep $APP1_NAME|grep -v grep|awk '{print $2}' `
pid2=`ps -ef|grep $APP2_NAME|grep -v grep|awk '{print $2}' `
start(){
if [ -n "$pid1" ]; then
echo "admin server already start,pid:$pid1"
return 0
fi
#进入命令所在目录
cd $HOME
nohup java -jar $HOME/admin-0.0.1-SNAPSHOT.jar > $HOME/admin.log 2>&1 &
#启动聊天服务器 把日志输出到HOME目录的server.log文件中
echo "admin start at port:$PORT1"
if [ -n "$pid2" ]; then
echo "app server already start,pid:$pid2"
return 0
fi
#进入命令所在目录
cd $HOME
nohup java -jar $HOME/app-0.0.1-SNAPSHOT.jar > $HOME/app.log 2>&1 &
#启动聊天服务器 把日志输出到HOME目录的server.log文件中
echo "app start at port:$PORT2"
}
stop(){
if [ -z "$pid1" ]; then
echo "not find admin on port:$PORT1"
return 0
fi
#结束程序,使用讯号2,如果不行可以尝试讯号9强制结束
kill -9 $pid1
rm -rf $pid1
echo "kill admin use signal 2,pid:$pid1"
if [ -z "$pid2" ]; then
echo "not find admin on port:$PORT2"
return 0
fi
#结束程序,使用讯号2,如果不行可以尝试讯号9强制结束
kill -9 $pid2
rm -rf $pid2
echo "kill admin use signal 2,pid:$pid2"
}
status(){
if [ -z "$pid1" ]; then
echo "not find program on port:$PORT1"
else
echo "program is running,pid:$pid1"
fi
if [ -z "$pid2" ]; then
echo "not find program on port:$PORT2"
else
echo "program is running,pid:$pid2"
fi
}
case $1 in
start)
start
;;
stop)
;;
restart)
$0 stop
sleep 2
$0 start
;;
status)
status
;;
*)
echo "Usage: {start|stop|status}"
exit 0
esac
fileformat=unix
使用dos2unix更改win格式到unix格式