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环境下编写的脚本每行结尾是rn,而Unix系统结尾是n,所以从windows上传的shell脚本需执行下面语句进行转换才能正确运行
sed -i 's/r//' small_control.sh sed -i 's/r//' small.sh
如发现问题 欢迎留言!
参与评论
手机查看
返回顶部