shell脚本获取进程ID并杀死的实现及问题解析

经常需要杀死某个进程,操作了几次之后,对一个熟练的码农来说,就要制作自己的工具了。有些工具虽然很小,但是却能节省一大部分的时间。

输入某个进程的ID并杀死的方法。这种事情,一般是先搜索再进行优化,这种对我这种不是大牛的人来说,是最快的方法:

果然不出所料,很快我就找到一个能用的脚本:

#!/bin/bash
echo "Input process name first"
read input1

PID=$(ps -e|grep $input1|awk ‘{printf $1}‘)

if [ $? -eq 0 ]; then
    echo "process id:$PID"
else
    echo "process $input1 not exit"
    exit
fi

kill -9 ${PID}

if [ $? -eq 0 ];then
    echo "kill $input1 success"
else
    echo "kill $input1 fail"
fi

可是,运行了几次之后,我发现了一个问题,就是这个脚本会把自己删除PID的线程也删掉,这种是删不掉的,所以总会报错。

找了一些资料,才发现里面有个问题需要解决:就是需要忽略当前的脚本即可:

pid=`ps -ef | grep "$filename" | grep -v "cgroup" | grep -v "grep" | awk ‘{print $2}‘`

接下来,把这个脚本改成这样就很完美了:

#!/bin/bash
#echo "Input process name first"
#read input1

PID=`ps -ef | grep "arm-none-eabi-gdb" | grep -v "cgroup" | grep -v "grep" | awk ‘{print $2}‘`

if [ $? -eq 0 ]; then
    echo "process id:$PID"
else
    echo "process $input1 not exit"
    exit
fi

kill -9 ${PID}

if [ $? -eq 0 ];then
    echo "kill $input1 success"
else
    echo "kill $input1 fail"
fi

顺便说明一下,grep -v 这个命令要好好用,以前用的太少了。也许,下面的这个例子能够清楚的给出这个关键字的含义和用法:

?  scripts git:(master) ? ps -ef | grep "kill_gdb"
  501 58728 40753   0  2:30下午 ttys003    0:00.00 sh kill_gdb.sh
  501 58894 40753   0  2:33下午 ttys003    0:00.01 vim kill_gdb.sh
  501 59004 40753   0  2:38下午 ttys003    0:00.02 vim kill_gdb.sh
  501 59015 40753   0  2:39下午 ttys003    0:00.01 vim kill_gdb.sh
  501 59053 40753   0  2:40下午 ttys003    0:00.01 vim kill_gdb.sh
  501 59610 40753   0  3:02下午 ttys003    0:00.02 vim kill_gdb.sh
  501 59675 40753   0  3:04下午 ttys003    0:00.00 grep --color=auto --exclude-dir=.bzr --exclude-dir=CVS --exclude-dir=.git --exclude-dir=.hg --exclude-dir=.svn kill_gdb
?  scripts git:(master) ? ps -ef | grep "kill_gdb" | grep -v "grep"
  501 58728 40753   0  2:30下午 ttys003    0:00.00 sh kill_gdb.sh
  501 58894 40753   0  2:33下午 ttys003    0:00.01 vim kill_gdb.sh
  501 59004 40753   0  2:38下午 ttys003    0:00.02 vim kill_gdb.sh
  501 59015 40753   0  2:39下午 ttys003    0:00.01 vim kill_gdb.sh
  501 59053 40753   0  2:40下午 ttys003    0:00.01 vim kill_gdb.sh
  501 59610 40753   0  3:02下午 ttys003    0:00.02 vim kill_gdb.sh

原文地址:https://www.cnblogs.com/dylancao/p/10547068.html

时间: 2024-07-30 12:09:06

shell脚本获取进程ID并杀死的实现及问题解析的相关文章

shell获取进程ID的方法: pidof | pgrep | ps-A+grep+awk

shell获取进程ID的方法: 我知道和实践过的有这么三种: ps -A |grep "cmdname"| awk '{print $1}' pidof "cmdname" pgrep "cmdname" 这三种在bash和busybox ash里面的运行结果稍有不同, 第一种完全相同,但是因为调用命令次数较多,性能上是不行的. 第二种: pidof 只能获取程序的文件名匹配到的进程号,在ash中 比如 pidof "usr/bin/t

【转载】用shell脚本监控进程是否存在 不存在则启动的实例

用shell脚本监控进程是否存在 不存在则启动的实例,先上代码干货: #!/bin/shps -fe|grep processString |grep -v grepif [ $? -ne 0 ]thenecho "start process....."elseecho "runing....."fi#####processString 表示进程特征字符串,能够查询到唯一进程的特征字符串0表示存在的$? -ne 0 不存在,$? -eq 0 存在 ---------

根据进程名称获取进程id

# -*- conding:utf-8-*- import subprocess def getpid_windows(process_name):    """利用cmd_str = tasklist|find /i "xdict.exe" 来查找windows平台的进程id"""    cmd_line = 'tasklist|find /i "%s"' %process_name    pp = su

用shell脚本监控进程是否存在 不存在则启动的实例附带if判断详细条件

#!/bin/shps -fe|grep processString |grep -v grepif [ $? -ne 0 ]thenecho "start process....."elseecho "runing....."fi#####processString 表示进程特征字符串,能够查询到唯一进程的特征字符串0表示存在的$? -ne 0 不存在,$? -eq 0 存在 --------------------------------------------

linux shell脚本监控进程是否存在

用shell脚本监控进程是否存在 不存在则启动的实例,先上代码干货:    #!/bin/shps -fe|grep processString |grep -v grepif [ $? -ne 0 ]thenecho "start process....."elseecho "runing....."fi #####processString 表示进程特征字符串,能够查询到唯一进程的特征字符串0表示存在的$? -ne 0 不存在,$? -eq 0 存在 定时执行:

shell脚本监控进程是否存在,不存在则启动实例

用shell脚本监控进程是否存在 不存在则启动的实例,先上代码干货: #!/bin/sh ps -fe|grep processString |grep -v grep if [ $? -ne 0 ] then echo "start process....." else echo "runing....." fi processString 表示进程特征字符串,能够查询到唯一进程的特征字符串 0表示存在的 $? -ne 0 不存在,$? -eq 0 存在 定时执行

linux shell 脚本获取和替换文件中特定内容

1.从一串字符串中获取特定的信息 要求1:获取本机IP:menu.lst为系统镜象的IP配置文件,需要从中获取到本机IP信息(从文件获取信息) 1 timeout 1 2 default 0 3 4 title live 5 find --set-root /casper/vmlinuz 6 kernel /casper/vmlinuz boot=casper ignore_uuid showmounts ip=eth0,10.0.66.66,255.255.240.0,10.0.64.3 7

shell脚本获取随机数random

用C提供的取随机数的方法srand和rand, 前者是给后者设置随机数种子seed. int rnd_num = 0; srand(seed); // time(NULL) 通常使用时间做种子 rnd_num = rand(); // 产生随机数 产生种子的方法: 1. 使用日期时间 时间作为种子很简单,取当前日期和时间,但是存在一个陷阱:当多台机器并发执行产生随机数时,出现相同随机数的概率非常高.不推荐这种方式 2. 使用$RANDOM 需要系统支持,通过echo来检测, 打印出一个随机数字,

对于shell脚本获取参数的一些小技巧

问题如下: 根据脚本参数的个数$#进行一个循环,在依次输出每个参数$1 $2 $3...... 我有一个循环变量i $i  取到这时的i为1,我想使用这个1再去调用$1,也是就是打印出第一个参数 就是$($i)的意思来取到第几个参数,当然$($i)是不好用的 当时纠结了好久,最后上百度提问,两位高手给出了答案: 1) #!/bin/sh NUMBER=$# echo $NUMBER i=1 while [ $i -le $NUMBER ] do a[$i]=$1 #将数组a[i]赋值为$1,即取