原文地址:https://blog.csdn.net/zhuchunyan_aijia/article/details/53811368
xxx.sh
文中. /etc/profile需要注意添加
#!/bin/sh
#nohup java -Xbootclasspath/a:./config -Dloader.path=.,lib -jar $APP_NAME --logging.config=./config/logback.xml >/dev/null 2>&1 &
#这里可替换为你自己的执行程序,其他代码无需更改
APP_NAME=hg-zhwg-web-1.0.0.jar
#启动方法
start(){
pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'`
if [ "$pid" ]; then
echo "$APP_NAME is already running. pid=$pid ."
else
. /etc/profile
nohup java -jar -Dloader.path=.,lib $APP_NAME --logging.config=./config/log4j2.xml >/dev/null 2>&1 &
echo "$APP_NAME now is running"
fi
}
#停止方法
stop(){
pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'`
if [ "$pid" ]; then
kill -9 $pid
echo "Pid:$pid stopped"
else
echo "$APP_NAME is not running"
fi
}
#输出运行状态
status(){
pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}'`
if [ "$pid" ]; then
echo "$APP_NAME is running. Pid is ${pid}"
else
echo "$APP_NAME is NOT running."
fi
}
#根据输入参数,选择执行对应方法,不输入则执行使用说明
case "$1" in
start)
start
;;
stop)
stop
;;
status)
status
;;
restart)
stop
sleep 5
start
;;
*)
echo "Usage:{start|stop|status|restart}"
;;
esac
exit 0
在/etc/init.d下新建文件
my.sh
chkconfig --add my (首先,添加为系统服务,注意add前面有两个横杠)
chkconfig my on (开机自启动)
chkconfig --list (列表显示)
service my start(启动服务,就是执行my的脚本)
#!/bin/bash
#
# kenny kenny.zhou@tom.com
# /etc/rc.d/init.d/hgzhwg
# init script for hgzhwg precesses
#
# processname: hgzhwg
# description: hgzhwg is a j2se server
# chkconfig: 2345 86 16
# description: Start up the hgzhwg servlet engine.
RETVAL=$?
case "$1" in
start)
echo $"Starting hgzhwg"
cd /home/xxxx/install/springboot
./xxx.sh restart
;;
stop)
echo $"Stopping Tomcat"
;;
*)
echo $"Usage: $0 {start|stop}"
exit 1
;;
esac
exit $RETVAL