你怎么能在bash脚本默默地用UNIX kill命令? [英] How can you use the unix kill command silently in a bash script?

查看:314
本文介绍了你怎么能在bash脚本默默地用UNIX kill命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检查是否有特定的进程正在运行,如果是,尝试把它杀了。

我想出迄今如下:

  PID = $(的ps aux | grep的myprocessname | grep的-v grep的| awk的'{$打印2})如果[-z $ PID]
然后
    回显PID $ PID仍在运行,试图杀害..
    杀-9 $ PID>的/ dev / null的
科幻

然而,当我运行它,我得到以下的输出:

 杀:用法:杀死[-s SIGSPEC | -n正负号| -sigspec]的pid | JOBSPEC ......或者杀死-l [SIGSPEC]

什么是杀死在UNIX上运行的进程的最佳方式默默?


解决方案

  [-z $ PID]

是真,如果 $ PID 是空的,而不是其他方式,所以你的测试被反转。

您应该使用指派,如果您有您的系统上了。 (甚至更好: pkill的 并停止担心所有的外壳逻辑。)

I'm trying to check if a particular process is running, and if it is, try and kill it.

I've come up with the following so far:

PID=$(ps aux | grep myprocessname | grep -v grep | awk '{print $2}')

if [ -z $PID];
then    
    echo Still running on PID $PID, attempting to kill..
    kill -9 $PID > /dev/null
fi

However when I run this, I get the following output:

kill: usage: kill [-s sigspec | -n signum | -sigspec] pid | jobspec ... or kill -l [sigspec]

What is the best way to kill a running process on unix silently?

解决方案

[ -z $PID]

is true if $PID is empty, not the other way around, so your test is inverted.

You should be using pgrep if you have that on your system too. (Or even better: pkill and stop worrying about all that shell logic.)

这篇关于你怎么能在bash脚本默默地用UNIX kill命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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