下面幾個腳本僅供參考,請根據本身須要作調整
start.shhtml
#!/bin/shrm -f tpid
APP_NAME=fm-eureka-client-1.0-SNAPSHOT
APP_JAR=$APP_NAME".jar"##nohup命令提交做業,那麼在缺省狀況下該做業的全部輸出都被重定向到一個名爲nohup.out的文件中,除非另外指定了輸出文件。這裏指定輸出文件在爲./fm-eureka-client-1.0-SNAPSHOT.lognohup java -jar $APP_JAR > $APP_NAME".log" 2>&1 &echo $! > $APP_NAME".tpid"echo $APP_NAME Start Success!12345678910111213
stop.shjava
#!/bin/shAPP_NAME=fm-eureka-client-1.0-SNAPSHOT
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`if [ ${tpid} ]; then
echo 'Stopping' $APP_NAME '...'
kill -15 $tpidfisleep 5tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`if [ ${tpid} ]; then
echo 'Kill' $APP_NAME 'Process!'
kill -9 $tpidelse
echo $APP_NAME 'Stoped Success!'fi12345678910111213141516
kill.shspring
#!/bin/shAPP_NAME=fm-eureka-client-1.0-SNAPSHOT
tpid=`ps -ef|grep $APP_NAME|grep -v grep|grep -v kill|awk '{print $2}'`if [ ${tpid} ]; then
echo 'Kill Process!'
kill -9 $tpidelse
echo $APP_NAME 'is not running!'fi12345678910
授予執行權限vim
chmod +x start.shchmod +x stop.shchmod +x kill.sh123
只要修改變量 $APP_NAME 就能夠用這種方式運行jar項目app
使用Linux服務的方式運行
在項目pom.xml中配置插件dom
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<executable>true</executable>
</configuration>
</plugin>
</plugins></build>
須要注意一下:<executable>true<executable>maven
1ide
2spring-boot
3oop
4
5
6
7
8
9
10
11
打包jar
正常使用 mvn clean package -Dmaven.test.skip=true 將工程打成jar包
部署
假設部署路徑爲 /var/apps/fm-eureka-client.jar ,使用命令作一個軟鏈接到 /etc/init.d 目錄,命令:
ln -s /var/apps/fm-eureka-client.jar /etc/init.d/fm-eureka-client
/etc/init.d/fm-eureka-client中的fm-eureka-client就是服務名。
另刪除軟鏈的命令是:
rm /etc/init.d/fm-eureka-client
授予jar文件可執行權限
命令:
chmod +x /var/apps/fm-eureka-client.jar
啓動服務
接下來,就可使用 service fm-eureka-client start|stop|restart|status 來對應用進行啓停了。
執行命令後將獲得形如 Started|Stopped [PID] 的結果反饋。
默認PID文件路徑:/var/run/{servicename}/{servicename}.pid
默認服務日誌文件路徑:/var/log/{servicename}.log(能夠經過下面.conf 的方式修改LOG_FOLDER)
自定義.conf文件來變動默認配置
在jar包相同路徑下建立一個.conf文件,名稱應該與.jar的名稱相同,如fm-eureka-client.conf(若是咱們打包的文jar文件爲 fm-eureka-client-1.0-SNAPSHOT.jar 那麼這裏的conf文件也應該是 fm-eureka-client-1.0-SNAPSHOT.conf),其內容配置能夠以下:
JAVA_HOME=/usr/local/jdkJAVA_OPTS=-Xmx1024MLOG_FOLDER=/data/logs/myapp
注:LOG_FOLDER 對應的文件夾目錄要必須存在,若是目錄不存在,服務並不會自從建立目錄。
- - - - - - - - - - - 分 - - - - - - - 割 - - - - - - - 線 - - - - - - - - - - - - - -12
若是你是CentOS 7或紅帽7以上,你還能夠用下面的方法處理,爲何要用這樣的方法(請自行研究),這裏直接提供結果
編輯服務文件 vim /usr/lib/systemd/system/fm-eureka-client.service
[Unit][Unit]Description=test[Service]WorkingDirectory=/servers/eureka_clientPrivateTmp=trueRestart=alwaysType=simpleExecStart=/usr/bin/java -jar /servers/eureka_client/fm-eureka-client-1.0-SNAPSHOT.jarExecStop=/usr/bin/kill -15 $MAINPID[Install]WantedBy=multi-user.target1234567891011121314
systemctl 管理,讓配置生效。如果修改配置文件,須要reload:
sudo systemctl daemon-reloadsudo systemctl enable fm-eureka-client.service12
啓動服務
systemctl start fm-eureka-client1
查看服務狀態
systemctl status eureka-client.service1
顯示以下:
eureka-client.service - test
Loaded: loaded (/usr/lib/systemd/system/eureka-client.service; enabled; vendor preset: disabled)
Active: active (running) since Wed 2017-10-25 21:32:24 CST; 16min ago
Main PID: 17080 (java)
Memory: 478.0M
CGroup: /system.slice/eureka-client.service
└─17080 /usr/bin/java -jar /servers/eureka_client/fm-eureka-client-1.0-SNAPSHOT.jar
Oct 25 21:47:03 localhost.localdomain java[17080]: gen usersig times:1Oct 25 21:47:05 localhost.localdomain java[17080]: load times:0Oct 25 21:47:05 localhost.localdomain java[17080]: gen usersig times:0Oct 25 21:47:07 localhost.localdomain java[17080]: load times:0Oct 25 21:47:07 localhost.localdomain java[17080]: gen usersig times:1Oct 25 21:47:28 localhost.localdomain java[17080]: 2017-10-25 21:47:28.647 INFO 17080 --- [trap-executor-0] c.n.d.s.r.aws.ConfigClusterResolver : Resolving eureka...figurationOct 25 21:47:43 localhost.localdomain java[17080]: load times:0Oct 25 21:47:43 localhost.localdomain java[17080]: gen usersig times:0Oct 25 21:47:46 localhost.localdomain java[17080]: load times:0Oct 25 21:47:46 localhost.localdomain java[17080]: gen usersig times:0Warning: eureka-client.service changed on disk. Run 'systemctl daemon-reload' to reload units.
Hint: Some lines were ellipsized, use -l to show in full.1234567891011121314151617181920
查看日誌(實時):
journalctl _PID=17080 -f//或journalctl -u eureka-client.service -f123
使用Linux 7 之後服務新的啓動方式,相關命令
//啓動systemctl start myapp//中止systemctl stop myapp//重啓systemctl restart myapp//查看日誌journalctl -u myapp//關於更多 systemctl 命令的使用方法,度娘有不少。123456789
轉載:http://blog.csdn.net/catoop/article/details/50588851
journalctl 資料:http://blog.csdn.net/qq_31863071/article/details/52047593
原地址:https://blog.csdn.net/u011861035/article/details/78646141
https://docs.spring.io/spring-boot/docs/current/reference/html/deployment-install.html#deployment-script-customization-conf-file