版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/mingY_life/article/details/81870848
将springboot jar包作为linux系统服务启动 #!/bin/bash
# chkconfig: 2345 65 35
# description: mes service
#端口号,根据此端口号确定PID
PORT=8070
#jar包所在目录
HOME='/usr/mesdemo'
#查询出监听了PORT端口TCP协议的程序
pid=`netstat -anp|grep $PORT|awk '{printf $7}'|cut -d/ -f1`
start(){
echo "INFO: Starting mes ..."
if [ -n "$pid" ]; then
echo "server already start,pid:$pid"
return 0
fi
#进入命令所在目录
cd $HOME
nohup java -jar $HOME/mes-0.0.1-SNAPSHOT.jar > $HOME/server.log 2>&1 & #启动聊天服务器 把日志输出到HOME目录的server.log文件中
echo "start at port:$PORT"
}
stop(){
if [ -z "$pid" ]; then
echo "not find program on port:$PORT"
return 0
fi
#结束程序,使用讯号2,如果不行可以尝试讯号9强制结束
kill -9 $pid
rm -rf $pid
while true
do
process=`netstat -anp|grep $PORT|awk '{printf $7}'|cut -d/ -f1`;
echo "process id is"+$process;
if [ "$process" != "" ]; then
sleep 1;
echo "stoping ...";
else
echo "service mes stop";
break;
fi
done
}
status(){
if [ -z "$pid" ]; then
echo "not find program on port:$PORT"
else
echo "program is running,pid:$pid"
fi
}
case $1 in
start)
start
;;
stop)
stop
;;
restart)
$0 stop
sleep 2
$0 start
;;
status)
status
;;
*)
echo "Usage: {start|stop|status}"
;;
esac
exit 0 将上面文件复制到linux中mes(新建该文件)中,注意回车符号(win和linux不一样),将mes放置在/etc/init.d下,然后添加到系统服务中去,如下图
然后就可以通过service mes start/stop/restart/status操作了。 |