#!/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 # 模糊匹配 $file 进程| 过滤自身命令进程 | 输出进程表中的进程号 pid=`ps -ef | grep $file | grep -v grep | awk '{print $2}'` # -z 表示如果$pid为空时则输出提示 if [ -z $pid ]; then echo "Service $file is not running! It's not necessary to stop it!" else # 杀死进程 kill -9$pid echo "Service stop successfully!pid:${pid} which has been killed forcibly!" echo "" fi fi fi fi done } #读取第一个参数 read_dir $1