springboot項目可以打成一個jar包,在服務器上部署啟動還是很方便的,但寫一個簡單的腳本會讓部署更加方便,
特別是分布式部署的時候,可以省略很多的ps 查看進程和kill進程的步驟,下面就展示一個簡單的啟動腳本
首先展示一下項目部署的目錄結構 small.jar是要運行的jar包
8080目錄 和8081目錄 8082 8083 目錄類似
其中small_control.sh是 總執行腳本 運行去觸發執行每個文件夾下的 small.sh腳本
這樣可以一起啟動四個不同的端口 也可以單獨啟動不同的端口
執行命令示例:
sh small_control.sh all start
sh small_control.sh 8080 stop
sh small_control.sh 8081 restart
下面是small_control.sh 代碼
#!/bin/bash current_dir=$(cd `dirname $0`; pwd) #當前目錄
#判斷第二個參數 case $2 in start) if [ $1 == "all" ]; then #第一個參數是all 遍歷所有含80目錄 執行small.sh for dir in `ls -d */ | grep 80` do sub_shell=${current_dir}/${dir}small.sh if [ -x "$sub_shell" ]; then $sub_shell stop >/dev/null 2>&1 $sub_shell start fi done else #第一個參數$1是port 類似8080 去執行該端口目錄下的small.sh ${current_dir}/$1/small.sh start fi ;; stop) if[ $1 == "all" ];then for dir in `ls -d */ | grep 80` do sub_shell=${current_dir}/${dir}small.sh if [ -x "$sub_shell" ];then $sub_shell stop fi done else ${current_dir}/$1/small.sh stop ;; restart) if[ $1 == "all" ];then for dir in `ls -d */ | grep 80` do sub_shell=${current_dir}/${dir}small.sh if [ -x "$sub_shell" ];then $sub_shell restart fi done else ${current_dir}/$1/small.sh restart ;; *) echo "Usage: small_control.sh {port|all} {start|stop}" echo "example: small_control.sh all start" echo "example: small_control.sh 8081 stop" exit 1 ;; esac
下面是smal.sh 代碼
#!/bin/bash current_dir=$(cd `dirname $0` ; pwd) #當前目錄 grand_parent_dir=$(cd ${current_dir}/..;pwd) #父目錄 port_num=${current_dir##*/} #端口 截取目錄最后一個/右邊內容 dest_out=${current_dir}/small${port_num}.out #輸出文件 jar_file=${grand_parent_dir}/small.jar pid_file=${current_dir}/${port_num}.pid command="java -jar -Xms1024M -Xmx4096M -Dserver.port=${port_num} ${jar_file}" #Functions 定義方法 #是否正在運行 傳入進程號 isRunning() { ps -p "$1" &> /dev/null } start() { #判斷pid文件是否存在 如存在且在運行則返回0 if [[ -f "$pid_file" ]]; then pid=$(cat "$pid_file") isRunning "$pid" && { echo "Alreday running [$pid]"; return 0 } fi do_start "$@" # $@ 傳遞所有參數到下一個方法 } do_start(){ cd $current_dir echo $command #打印命令 $command > $dest_out 2>&1 & #執行命令 pid=$! #記錄進程號 echo "$pid" > "$pid_file" #進程號輸出到pid file # -z 判斷字符串長度是否為0 為0則true [[ -z $pid ]] && { echo "Failed to start"; return 1; } echo "Started {$pid}" } stop(){ # -f 判斷pid文件是否存在 [[ -f $pid_file ]] || { echo "Not running (Pidfile not found)"; return 0; } pid=$(cat "$pid_file") isRunning "$pid" || { echo "not running (process ${pid}). remove pid file."; rm -f "$pid_file"; return 0; } do_stop "$pid" "$pid_file" } do_stop(){ #殺掉進程 失敗返回 kill "$1" &> /dev/null || { echo "unable to kill process $1"; return 1; } #循環判斷是否還運行 for i in $(seq 1 30); do isRunning "$1" || { echo "stopped [$1]"; rm -rf "$2"; return 0; } [[ $i -eq 10 ]] && kill "$1" &> /dev/null [[ $i -eq 20 ]] && kill -9 "$1" &> /dev/null sleep 1 done echo "unable to kill process $1"; return 1; } restart(){ stop && start } #運行狀態 status(){ [[ -f "$pid_file" ]] || { echo "not running"; return 3; } pid=$(cat "$pid_file") isRunning $pid || { echo "Not running (Pidfile not found)"; return 1; } echo "running {$pid}" return 0 } case "$1" in start)
#執行start方法 start "$@"; exit $? ;; stop) stop "$@"; exit $? ;; restart) restart "$@"; exit $? ;; status) status "$@"; exit $? ;; *) echo "usage $0 {start|stop|restart|status}" echo 1;; esac
注意方法的定義要放在執行方法的語句前面,shell腳本是順序執行的
注意 在windows環境下編寫的腳本每行結尾是\r\n,而Unix系統結尾是\n,所以從windows上傳的shell腳本需執行下面語句進行轉換才能正確運行
sed -i 's/\r//' small_control.sh sed -i 's/\r//' small.sh
如發現問題 歡迎留言!