一、背景条件
1. Linux系统是Debian 8
2. Java程序是test.jar,安装路径是/home/test/test.jar
二、编写java的启动脚本
startTest.sh
#!/bin/shjava -jar /home/test/test.jar &echo $! > /var/run/test.pid
三、编写java的停止脚本
stopTest.sh
#/bin/shPID=$(cat /var/run/test.pid)kill -9 $PIDrm -fr /var/run/test.pid
四、编写testJava.service
[Unit]Description=TestJavaAfter=network.target[Service]Type=forkingExecStart=/home/test/startTest.shExecStop=/home/test/stopTest.sh[Install]WantedBy=multi-user.target
五、添加到自启动
systemctl enable testJava
六、测试
启动: systemctl start testJava
停止: systemctl stop testJava附、tomcat自启动service的配置
[Unit]Description=TomcatAfter=network.target[Service]Type=forkingPIDFile=/usr/local/apache-tomcat-8.5.20/tomcat.pidExecStart=/usr/local/apache-tomcat-8.5.20/bin/catalina.sh startExecReload=/usr/local/apache-tomcat-8.5.20/bin/catalina.sh restartExecStop=/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/