无法从脚本循环中杀死PID [英] can't kill PID from script loop

查看:44
本文介绍了无法从脚本循环中杀死PID的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一个脚本函数,该脚本为clipit(剪贴板管理器)获取pid并杀死clipit.我认为这很简单,只是出于某种原因,shell会弹出一个有关终止PID的错误.这是我的功能:

I need a function for a script that gets pid for clipit (clipboard manager) and kills clipit. Pretty straightforward stuff I think except for some reason the shell is puking back an error about killing the PID. Here's my function:

stat=0
while [[ "$stat" == 0 ]] ; do
    clipitPid=$(ps aux|egrep -m 1 "[ \t]{1,}[0-9]{1,2}:[0-9]{1,2}[ \t]*clipit$"|sed -r "s/^[^ ]*[ ]*([0-9]{3,5})[ \t]{1,}.*$/\1/") #; echo "\$clipitPid: ${clipitPid}"
    kill "$clipitPid" 0>/dev/null 1>/dev/null 2>/dev/null
    stat="$?"
done

这是终端错误:

10:12 ~ $ stat=0 ; while [[ "$stat" == 0 ]] ; do clipitPid=$(ps aux|egrep "[ \t]{1,}[0-9]{1,2}:[0-9]{1,2}[ \t]*clipit$"|sed -r "s/^[^ ]*[ ]*([0-9]{3,5})[ \t]{1,}.*$/\1/") ; echo "\$clipitPid: ${clipitPid}" ; kill "$clipitPid" 0>/dev/null 1>/dev/null 2>/dev/null ; stat="$?" ; done
$clipitPid: 24760
24791
bash: kill: 24760
24791: arguments must be process or job IDs

我尝试将发送输出删除为null,这没有什么区别.我有两个运行clipit的实例,以确保检测到两个PID并将其杀死.但是,PID可以很好地回显,但不能被否定.但是,如果我手动杀死24760 24791",则PID会被杀死:

I've tried removing the sending output to null and that makes no difference. I have two instances of clipit running to ensure both PIDs are detected and killed; however, the PIDs are echoed ok but not being nuked. But if I manually 'kill 24760 24791' the PIDs are killed:

10:12 ~ $ kill 24760 24791
10:12 ~ $ ps aux|grep clipit
[3]-  Terminated              clipit
[4]+  Terminated              clipit

任何想法,如果我在这里有什么错,我将不胜感激.

Any ideas what I have wrong here I would appreciate reading them.

编辑

实际上,如果我仅运行一个clipit实例,则使用上述onliner罚款将其杀死.2个或更多clipit,那么它就不能杀死任何一个,但可以正常检索PID.

actually if I have only one instance of clipit running then it gets killed using the above onliner fine. 2 clipits or more then it fails to kill any, yet retrieves the PIDs ok.

推荐答案

不要尝试重新发明轮子.如果有 pkill 可用,请使用它.

Don't try to reinvent the wheel. If pkill is available use it.

pkill -f clipit

如果没有,您可以使用类似的东西.

If not you can use something like.

kill $(ps -eo pid,comm | grep clipit | grep -v grep | cut -d''-f1)

这篇关于无法从脚本循环中杀死PID的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆