最新消息:

ubuntu16.4 开机启动配置,jar配置成服务

运维 luther 311浏览 0评论

https://blog.csdn.net/ljh618625/article/details/77899310

mytest.service

[Unit]
Description = mytest service
After network.target = mytest.service

[Service]
Type = forking
ExecStart = /javaapps/mytestum/mytest_service.sh start
ExecStop = /javaapps/mytestum/mytest_service.sh stop
ExecReload = /javaapps/mytestum/mytest_service.sh restart

[Install]
WantedBy=multi-user.target

mytest_service.sh,如果出错,请进行手动输入创建文件。更改头部的相关参数为你的实际情况

#!/bin/sh
SERVICE_NAME=mytest
HOME=/javaapps/mytestum
PATH_TO_JAR=$HOME/mytest.jar
PID_PATH_NAME=/tmp/mytest.pid
LOG=$HOME/logs/product.log
ERROR_LOG=$HOME/logs/production.err
CONFIG=$HOME/application-production.yml
case $1 in
start)
echo "strting $SERVICE_NAME..."
if [ ! -f $PID_PATH_NAME ]; then
cd $HOME
nohup java -jar $PATH_TO_JAR --spring.config.location=$CONFIG > $LOG 2> $ERROR_LOG &
echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started..."
else
echo "$SERVICE_NAME is already running ..."
fi
;;
stop)

if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "$SERVICE_NAME stoping..."
kill -9 $PID;
echo "$SERVICE_NAME stoped..."
rm $PID_PATH_NAME
else
echo "$SERVICE_NAME is not running..."
fi
;;
restart)
if [ -f $PID_PATH_NAME ]; then
PID=$(cat $PID_PATH_NAME);
echo "$SERVICE_NAME stoping..."
kill -9 $PID;
echo "$SERVICE_NAME stoped..."
rm $PID_PATH_NAME
echo "strting $SERVICE_NAME..."
cd $HOME
nohup java -jar $PATH_TO_JAR --spring.config.location=$CONFIG > $LOG 2> $ERROR_LOG &
echo $! > $PID_PATH_NAME
echo "$SERVICE_NAME started..."
else
echo "$SERVICE_NAME is not runing..."
fi
;;
*)
echo "please enter the parameters:start|stop|restart"
esac

Configure mytest as a system service and start automatically when the server starts

*Copy the file mytest.service to the cloud server directory / etc/systemd/system/.

*Copy the file mytest_service.sh to the working directory of the mytest / javaapps/mytestum/

* Copy the file to the working directory of the mytest / javaapps/mytestum/

Run the following commad to enable mytest.service:

chmod +755 /javaapps/mytestum/ mytest_service.sh

sudo systemctl enable mytest.service

now ,you can Use the following command to start\stop\restart mytest:

service mytest start

service mytest stop

service mytest restart

转载请注明:应用享评 » ubuntu16.4 开机启动配置,jar配置成服务

您必须 登录 才能发表评论!