8月212015
编写stop.sh脚本kill进程,批量杀掉进程
杀掉一个进程 #!/bin/bash pid=`(ps -ef | grep /home/jiao/8.3.1.4b23/etc/gs_express.cnf | grep -v "grep") | awk '{print $2}'` kill -9 $pid ------------------------------------------------------------------ ps -ef|grep 条件|grep -v grep |awk '{print $2}'|xargs -n1 kill -9 ------------------------------------------------------------------ 批量杀掉进程 #!/bin/sh keys=`(ps -ef |grep "rsync" |grep -v "grep") | awk '{print $2}'` for key in ${keys[*]} do kill -9 $key done ------------------------------------------------------------------ 转一个脚本stop.sh #!/bin/bash #stop.sh if [ ! $1 ]; then echo "Usage: $0 <name> [l]" echo " l just list" else param=$1 fi if [ "$2" = "l" -o "$2" = "L" ]; then ps aux | grep $1 | grep -v "grep" exit fi # process every prog like $1 for pid in `ps aux | grep $param | grep -v "grep" | awk '{print $2}'` ; do ok=true pid_name=` ps aux | grep $pid | sed -n '1P' | awk '{print $11}'` #sed -n '1P' 相当于 grep -v grep # if have such pid,(becaus some time when the first be killed , the forked pid will be killed all the same, for example mozilla.) if [ $pid_name ]; then echo -n "name: $pid_name pid: $pid Kill? [Y/n/q]:" read confirm #want quit... if [ "$confirm" = "q" -o "$confirm" = "Q" ]; then echo " Quit..." exit fi #echo "$confirm" #want to kill.. if [ "$confirm" = "" ]; then echo -n " killing...default" kill -9 $pid echo "ok." else if [ $confirm = 'n' -o $confirm = 'N' ]; then echo -n " Canceled..." echo $'a' continue else if [ "$confirm" = "y" -o "$confirm" = "Y" ]; then echo -n " killing...(confirmed)!" kill -9 $pid echo "ok." fi fi fi # no such pid else echo "no such pid $pid"; continue fi done # kill over. if [ $ok ]; then echo "All prog like "$1" were prcessed by you!" else echo "Sorry, No such prog like "$1"" fi ------------------------------------------------------------------------------------- 使用方法:杀掉进程 sh stop.sh gbased name: /usr/bin/mozilla pid: 26608 Kill? [Y/n/q] 默认为Y,输入时大小定皆可, 直接回车为杀,输Y或y也杀. 输n或N为不杀,并继续下一个. 输q或Q为退出脚本. ---------------------------- 查看进程 sh stop.sh gbased L zhouyan 1833 0.0 0.0 1174556 8924 ? Sl Oct13 0:00 libexec/gbased --defaults-file=etc/gs_express.cnf ------------------------------------------------------------------------------------------- 翻译该脚本 #!/bin/bash #####-----#!表示用之后跟的shell来运行该脚本 #stop.sh if [ ! $1 ]; then #####---------------------------首先判断“要杀掉的进程”是否存在,$1代表第一个用户变量,即你要输入的进程名字。 echo "Usage: $0 <name> [l]" #####---------------------------若不存在,打印当前命令的帮助。Usage无意义,$0代表当前程序,<name> <>代表其内的参数是必须要存在的,[]代表其内的参数是可选的。即打印出来的帮助信息是,执行stop.sh脚本时,必须要跟上想杀掉的进程名字,用l仅列出所有该进程的程序名。 echo " l just list" else param=$1 fi if [ "$2" = "l" -o "$2" = "L" ]; then #####-----------------------------------实现l和L不区分大小写。-o是“或者” ps aux | grep $1 | grep -v "grep" #####-----------------------------------当输入l或者L时,输出所有进程名为$1的程序 exit fi # process every prog like $1 for pid in `ps aux | grep $param | grep -v "grep" | awk '{print $2}'` ; do ok=true pid_name=` ps aux | grep $pid | sed -n '1P' | awk '{print $11}'` #####-----------------------------------------------------------sed -n '1P' 相当于 grep -v grep #####-----------------------------------------------------------将进程名字对应的程序名输出 # if have such pid,(becaus some time when the first be killed , the forked pid will be killed all the same, for example mozilla.) if [ $pid_name ]; then #####-----------------------------------------------当pid_name存在,则印出下句内容 echo -n "name: $pid_name pid: $pid Kill? [Y/n/q]:" read confirm #####-----------------读取confirm变量的内容,即键盘上敲入的Y/n/q #want quit... if [ "$confirm" = "q" -o "$confirm" = "Q" ]; then echo " Quit..." exit fi #echo "$confirm" #want to kill.. if [ "$confirm" = "" ]; then echo -n " killing...default" kill -9 $pid echo "ok." else if [ $confirm = 'n' -o $confirm = 'N' ]; then echo -n " Canceled..." echo $'a' continue else if [ "$confirm" = "y" -o "$confirm" = "Y" ]; then echo -n " killing...(confirmed)!" kill -9 $pid echo "ok." fi fi fi # no such pid else echo "no such pid $pid"; continue fi done # kill over. if [ $ok ]; then echo "All prog like "$1" were prcessed by you!" else echo "Sorry, No such prog like "$1"" fi
发表评论
木有头像就木JJ啦!还木有头像吗?点这里申请属于你的个性Gravatar头像吧!