一、背景条件
1. Linux系统是Debian 8
2. Java程序是test.jar,安装路径是/home/test/test.jar
二、编写java的启动脚本
startTest.sh
#!/bin/sh
java -jar /home/test/test.jar &
echo $! > /var/run/test.pid
三、编写java的停止脚本
stopTest.sh
#/bin/sh
PID=$(cat /var/run/test.pid)
kill -9 $PID
rm -fr /var/run/test.pid
四、编写testJava.service
[Unit]
Description=TestJava
After=network.target
[Service]
Type=forking
ExecStart=/home/test/startTest.sh
ExecStop=/home/test/stopTest.sh
[Install]
WantedBy=multi-user.target
五、添加到自启动
systemctl enable testJava
六、测试
启动: systemctl start testJava
停止: systemctl stop testJava
附、tomcat自启动service的配置
[Unit]
Description=Tomcat
After=network.target
[Service]
Type=forking
PIDFile=/usr/local/apache-tomcat-8.5.20/tomcat.pid
ExecStart=/usr/local/apache-tomcat-8.5.20/bin/catalina.sh start
ExecReload=/usr/local/apache-tomcat-8.5.20/bin/catalina.sh restart
ExecStop=/usr/local/apache-tomcat-8.5.20/bin/catalina.sh stop
[Install]
WantedBy=multi-user.target
1. 需要在catalina.sh中添加CATALINA_PID的参数配置和PID一致。
2. tomcat.service文件放置路径 /lib/systemd/system/