在启动微服务项目的时候,由于 jar 文件过多,所以需要一套快速启动的命令进行启动。
以下是命令:
放到执行文件夹内
start.sh
#!/usr/bin/env bash # 启动一个目录下的所有jar包 function read_dir(){ for file in `ls` do # 当前文件不是一个文件夹 if [[ -f $file ]] then # 如果当前文件是一个.jar结尾的文件则启动它 if [[ ${file:0-4} == '.jar' ]]; then echo $file 开始启动... nohup java -jar $file >./logs/$file.log 2>&1 & echo 启动完成! fi fi done } #读取第一个参数 read_dir
shutdown.sh
#!/usr/bin/env bash # 停止一个目录下的所有jar程序 function read_dir(){ for file in `ls` do # 当前文件不是一个文件夹 if [[ -f $file ]] then if [[ ${file:0-4} == '.jar' ]]; then # 获取pid pid=`ps -ef | grep $file | grep -v grep | awk '{print $2}'` # -z 表示如果$pid为空时则输出提示 if [ -z $pid ]; then echo "" echo "Service $file is not running! It's not necessary to stop it!" echo "" else # 杀死进程 kill -9 $pid echo "" echo "Service stop successfully!pid:${pid} which has been killed forcibly!" echo "" fi fi fi done } #读取第一个参数 read_dir
status.sh
#!/usr/bin/env bash # 查看某个目录下所有jar程序的状态 function read_dir(){ for file in `ls` do # 当前文件不是一个文件夹 if [[ -f $file ]] then if [[ ${file:0-4} == '.jar' ]]; then # 获取pid pid=`ps -ef | grep $file | grep -v grep | awk '{print $2}'` # -z 表示如果$pid为空时则输出提示 if [ -z $pid ];then echo "" echo "Service $file is not running!" echo "" else echo "" echo "Service $file is running. It's pids=${pid}" echo "" fi fi fi done } #读取第一个参数 read_dir
迭代文件夹并带命令版本
使用方式
sh status.sh /Users/jarList/
status.sh
#!/usr/bin/env bash # 查看某个目录下所有jar程序的状态 function read_dir(){ for file in `ls $1` do #如果当前文件是文件夹则递归处理 if [ -d $1"/"$file ] then read_dir $1"/"$file else # 当前文件不是一个文件夹 if [[ -f $1"/"$file ]] then if [[ ${file:0-4} == '.jar' ]]; then # 获取pid pid=`ps -ef | grep $1"/"$file | grep -v grep | awk '{print $2}'` # -z 表示如果$pid为空时则输出提示 if [ -z $pid ];then echo "" echo "Service $1"/"$file is not running!" echo "" else echo "" echo "Service $1"/"$file is running. It's pids=${pid}" echo "" fi fi fi fi done } #读取第一个参数 read_dir $1