shell 批量启动脚本
大约 5 分钟
需求
在进行服务器部署测试的时候经常需要重启大量项目,需要一个能自动识别项目目录启动jar的shell脚本,并统一管理日志输出
#在此项目文件夹中,我只想执行二层目录的启动.jar,而不是递归所有jar,并且项目并不固定
项目文件夹
--项目A
----lib
------依赖.jar
----配置文件
----启动.jar
--项目B
----lib
------依赖.jar
----配置文件
----启动.jar启动脚本
设计思路:轮询启动子文件夹内的*.jar,日志输出至当前目录的run_log,Pid保存至pids
#!/bin/bash
#单个命令
nohup java -jar miko/app.jar >> run_log/miko-app.jar-$(date +%Y-%m-%d).log 2>&1 &
echo $! >> pids#!/bin/bash
#循环命令
for folder in $(ls -d */)
do
#去子目录
cd $folder
#这里的2>/dev/null是为了防止找不到文件ls输出报错影响程序运行
#如果当前目录没有*.jar,则会输出报错
#ls: cannot access '*.jar': No such file or directory
for file in $(ls -q *.jar 2>/dev/null)
do
# ${folder/\//-} 实现文件名 folder/ 更换为 folder- 方便拼接
# $(date +%Y-%m-%d) 实现按时间输出日志文件
nohup java -jar $file >> ../run_log/${folder/\//-}$file-$(date +%Y-%m-%d).log 2>&1 &
# 保存pid方便暂停/重启使用
echo ${folder/\//-}$file $! >> ../pids
echo ${folder/\//-}$file
done
#返回上层目录
cd ..
done暂停脚本
设计思路: 根据pids列表,kill所有相应pid,并清空pids
#!/bin/bash
cat pids | while read line
do
pid=$(echo ${line} | awk -F " " '{print $2}')
#查询进程是否存在,存在则kill
PID_EXIST=$(ps aux | awk '{print $2}'| grep -w $pid)
#-n 非空判断
if [[ -n $PID_EXIST ]];then
echo "停止" $line
kill -9 ${line[1]}
else
echo "进程不存在" $line
fi
done
#清空pids
cat /dev/null > pids重启脚本
设计思路: 执行暂停脚本,再执行启动脚本
#!/bin/bash
start() {
#启动脚本
}
stop() {
#停止脚本
}
# 重启方法
restart() {
stop
start
}详情脚本
设计思路: 根据pid列表输出启动命令,pid,对应端口号
#!/bin/bash
cat pids | while read line
do
pid=$(echo ${line} | awk -F " " '{print $2}')
#查询进程是否存在,存在则kill
PID_EXIST=$(ps aux | awk '{print $2}'| grep -w $pid)
#-n 非空判断
if [[ -n $PID_EXIST ]];then
port=$(lsof -i | grep $pid | awk '{print $9}')
#输出项目-app.jar 端口号
echo "运行中" $line $port
else
echo "进程不存在" $line
fi
done完整代码
#!/bin/bash
#复制粘贴记得执行 chmod +x *.sh
start() {
pids=$(cat pids)
#-n 非空判断
if [[ -n $pids ]];then
echo $pids
echo "存在历史启动,请先暂停"
exit
fi
for folder in $(ls -d */)
do
#去子目录
cd $folder
#这里的2>/dev/null是为了防止找不到文件ls输出报错影响程序运行
#如果当前目录没有*.jar,则会输出报错
#ls: cannot access '*.jar': No such file or directory
for file in $(ls -q *.jar 2>/dev/null)
do
# ${folder/\//-} 实现文件名 folder/ 更换为 folder- 方便拼接
# $(date +%Y-%m-%d) 实现按时间输出日志文件
nohup java -jar $file >> ../run_log/${folder/\//-}$file-$(date +%Y-%m-%d).log 2>&1 &
# 保存pid方便暂停/重启使用
echo ${folder/\//-}$file $! >> ../pids
echo ${folder/\//-}$file
done
#返回上层目录
cd ..
done
}
stop() {
cat pids | while read line
do
pid=$(echo ${line} | awk -F " " '{print $2}')
#查询进程是否存在,存在则kill
PID_EXIST=$(ps aux | awk '{print $2}'| grep -w $pid)
#-n 非空判断
if [[ -n $PID_EXIST ]];then
echo "停止" $line
kill -9 $pid
else
echo "进程不存在" $line
fi
done
#清空pids
cat /dev/null > pids
}
restart() {
stop
start
}
status(){
cat pids | while read line
do
pid=$(echo ${line} | awk -F " " '{print $2}')
#查询进程是否存在,存在则kill
PID_EXIST=$(ps aux | awk '{print $2}'| grep -w $pid)
#-n 非空判断
if [[ -n $PID_EXIST ]];then
port=$(lsof -i | grep $pid | grep LISTEN | awk '{print $9}')
#输出项目-app.jar 端口号 并且排除非监听连接端口
echo "运行中" $line $port
else
echo "进程不存在" $line
fi
done
}
# 根据输入参数执行对应方法,不输入则提示方法
case "$1" in
"start")
start
;;
"stop")
stop
;;
"status")
status
;;
"restart")
restart
;;
*)
echo "start | stop | status | restart"
;;
esac