1、創建工作目錄 創建jar部署的目錄,並且將jar上傳到該目錄下 mkdir -p /home/java-server/test-demo 2、創建啟動腳本 進入jar目錄: cd /home/java-server/test-demo 創建腳本文件: vim start.sh 啟動腳本內容: # ...
1、創建工作目錄
創建jar
部署的目錄,並且將jar
上傳到該目錄下
mkdir -p /home/java-server/test-demo
2、創建啟動腳本
進入jar
目錄:
cd /home/java-server/test-demo
創建腳本文件:
vim start.sh
啟動腳本內容:
#!/bin/sh export LANG="en_US.UTF-8" cd /home/java-server/test-demo runMessage=`ps aux | grep \`cat pidfile.txt\`` projectStartCommand="/usr/local/java/jdk1.8.0_131/bin/java -jar xxx.jar" if [[ $runMessage == *$projectStartCommand* ]] then echo "Application has starting ,restarting..." kill -9 `cat pidfile.txt` nohup /usr/local/java/jdk1.8.0_131/bin/java -jar xxx.jar -java.tmp.dir=/home/java-server/test-demo/temp >/dev/null 2>&1 & echo $! > pidfile.txt else echo "Application has stopped ,starting..." nohup /usr/local/java/jdk1.8.0_131/bin/java -jar xxx.jar -java.tmp.dir=/home/java-server/test-demo/temp >/dev/null 2>&1 & echo $! > pidfile.txt fi
-
腳本中
/home/java-server/test-demo
就是jar包
所在的,可以根據實際情況定。 -
xxx.jar
為實際的jar
包名稱。 -
/usr/local/java/jdk1.8.0_131/bin/java
為jdk環境所在路徑。
3、創建停止腳本
進入jar
目錄:
cd /home/java-server/test-demo
創建腳本文件:
vim stop.sh
停止腳本內容:
#!/bin/sh cd /home/java-server/test-demo PID=$(cat pidfile.txt) if [ ${PID} ]; then echo 'Application is stpping...' echo kill $PID DONE kill $PID else echo 'Application is already stopped...' fi
4、設置開機自啟
進入system:
# 進入system
cd /usr/lib/systemd/system/
創建自啟腳本:
vim xxx.service
xxx.service為腳本的名稱,一般來就是jar包首碼名稱一致,比如:test-demo.service
內容如下:
[Unit] Description=xxx After=network.target [Service] Type=forking ExecStart=/home/java-server/test-demo/start.sh ExecStop=/home/java-server/test-demo/stop.sh PrivateTmp=true [Install] WantedBy=multi-user.target
-
Description:描述,一般為項目名稱
-
ExecStart:開啟腳本位置
-
ExecStop:停止腳本位置
設置腳本許可權:
# 自啟文件 chmod 777 xxx.service # 啟動文件 chmod 777 /home/java-server/test-demo/start.sh # 停止文件 chmod 777 /home/java-server/test-demo/stop.sh
加入自啟命令 :
systemctl daemon-reload
systemctl enable xxx.service
啟動|停止命令:
# 啟動
systemctl start xxx
# 停止
systemctl stop xxx
xxx
就是xxx.service
的首碼。