1.打開Xshell,連接linux服務(wù)器,輸入用戶名和密碼,登錄后找到項目jar包所在路徑。

image.png
輸入:cd 文件目錄,找到j(luò)ar包位置
輸入:ls -l ,查看所有文件

image.png
在同目錄下新建shell文件并編寫文件內(nèi)容:
輸入:touch runapi2【文件名】.sh ,創(chuàng)建文件
輸入:vi runapi2【文件名】.sh ,進入編寫文件
點擊Esc 開始編寫程序:如下
#!/bin/bash
#name:jar包啟動腳本;
#date:2019-8-26;
#author:Yu-Luozi
#此處修改腳本名稱:
APP_NAME=kweb-api-1.0-SNAPSHOT.jar
#腳本菜單項
usage() {
echo "Usage: sh 腳本名.sh [start|stop|restart|status]"
exit 1
}
is_exist(){
pid=`ps -ef|grep $APP_NAME|grep -v grep|awk '{print $2}' `
#如果不存在返回1,存在返回0
if [ -z "${pid}" ]; then
return 1
else
return 0
fi
}
#啟動腳本
start(){
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is already running. pid=${pid} ."
else
#此處注意修改jar和log文件文件位置:
nohup java -jar /home/clkt/KeAndScExtract/$APP_NAME > bootdolog.file 2>&1 &
#此處打印log日志:
tail -f /home/clkt/KeAndScExtract/bootdolog.file
fi
}
#停止腳本
stop(){
is_exist
if [ $? -eq "0" ]; then
kill -9 $pid
else
echo "${APP_NAME} is not running"
fi
}
#顯示當前jar運行狀態(tài)
status(){
is_exist
if [ $? -eq "0" ]; then
echo "${APP_NAME} is running. Pid is ${pid}"
else
echo "${APP_NAME} is NOT running."
fi
}
#重啟腳本
restart(){
stop
start
}
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
usage
;;
esac
編寫完畢,按下Esc退出編寫功能,輸入“:wq”回車保存,輸入“:q!”回車不保存退出
輸入 “:q”退出編輯,若E37: No write since last change (add ! to override)問題則強制存盤:使用命令:w!強制存盤即可
測試
輸入:sh runapi.sh【腳本名】
按菜單輸入執(zhí)行方法。

image.png
啟動成功。