bash 试图通过 shell 脚本杀死进程

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19700022/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-10 00:22:51  来源:igfitidea点击:

Trying to kill process by shell scripting

bashshellubuntu

提问by Suresh Kamrushi

I want to kill process through shell scripting it is giving me error. Here is what i tried so far:

我想通过 shell 脚本终止进程,它给了我错误。这是我到目前为止尝试过的:

When i try to kill memcache it give me error like "kill: No such process" , i used below command:

当我尝试杀死 memcache 时,它​​给了我类似“杀死:没有这样的进程”这样的错误,我使用了以下命令:

ps -ef | grep "memcache" | awk '{print }' | xargs kill; 

or if try like below:

或者如果尝试如下:

kill -9 $(pidof memcache)

i get error like below:": arguments must be process or job IDs"

我收到如下错误:“:参数必须是进程或作业 ID”

When i run directly on command prompt process is running:

当我直接在命令提示符下运行时,进程正在运行:

ring@ubuntu:~/parasol$ ps aux | grep memcache     
memcache   873  0.0  0.0 323220  1188 ?        Sl   22:56   0:00 /usr/bin/memcached -m 64 -p 11211 -u memcache -l 127.0.0.1
ring      1714  0.0  0.0   9384   920 pts/0    S+   23:45   0:00 grep --color=auto memcache

I reff to https://askubuntu.com/questions/239923/shell-script-to-9-kill-based-on-nameAND Shell script to capture Process ID and kill it if exist

我参考https://askubuntu.com/questions/239923/shell-script-to-9-kill-based-on-nameAND Shell 脚本来捕获进程 ID 并在存在时将其杀死

My Ubuntu Version:

我的 Ubuntu 版本:

DISTRIB_ID=Ubuntu
DISTRIB_RELEASE=12.04
DISTRIB_CODENAME=precise
DISTRIB_DESCRIPTION="Ubuntu 12.04.2 LTS"
NAME="Ubuntu"
VERSION="12.04.2 LTS, Precise Pangolin"
ID=ubuntu
ID_LIKE=debian
PRETTY_NAME="Ubuntu precise (12.04.2 LTS)"
VERSION_ID="12.04"

回答by Ashish

Acutally

实际上

ps -ef | grep memcache

will give two lines..

会给两行..

so you can go this way

所以你可以走这条路

ps -ef | grep memcache | grep -v "grep" | awk '{print }' | xargs kill; 

This can get you exact one PID

这可以为您提供确切的一个 PID

if I has to do I would break it in 2 lines in start

如果我必须这样做,我会在开始时将其分成 2 行

#!/bin/bash
PID=`ps -ef | grep memcache | grep -v "grep" | awk '{print }'`
echo $PID
#to check PID is right
kill -9 $PID

save it in scrip files say test.sh

将它保存在脚本文件中说 test.sh

then on terminal

然后在终端

chmod +x test.sh

then

然后

./test.sh

回答by BlackMamba

You can use these commands: pkill memcachedor pgrep memcached | xargs kill -9or killall memcached.

您可以使用以下命令: pkill memcachedpgrep memcached | xargs kill -9killall memcached

pgrep, pkill - look up or signal processes based on name and other attributes.

pgrep, pkill - 根据名称和其他属性查找或发送进程信号。

回答by jlliagre

Your first command might be killing itself before having chance to kill the target processes.

在有机会杀死目标进程之前,您的第一个命令可能会杀死自己。

For a reliable way, run pkill /usr/bin/memcachedor pkill -9 /usr/bin/memcachedalthough the latter is a bad practice.

对于可靠的方式,运行pkill /usr/bin/memcachedpkill -9 /usr/bin/memcached尽管后者是一种不好的做法。

回答by Some programmer dude

For the first command, you see that there are twoprocesses that matches the pattern you grep for. The actual memcachedprocess and your grepprocess. That is probably the reason for the error of the first command line.

对于第一个命令,您会看到有两个进程与您搜索的模式匹配。实际memcached过程和你的grep过程。这可能是第一个命令行出错的原因。

Try narrowing the search down, for example by grepping for e.g. "^memcache.*/usr/bin/memcached".

尝试缩小搜索范围,例如通过 grepping for eg "^memcache.*/usr/bin/memcached"



The problem with the second error is that you're calling pidofwith the usernameinstead of the process name, so the command is essentially kill -9without any process id. Try instead e.g. pidof memcachedto get the process id of the correct process.

第二错误的问题是,你打电话pidof的用户名,而不是进程名,因此命令基本上是kill -9没有任何进程ID。尝试改为例如pidof memcached获取正确进程的进程 ID。

回答by Idriss Neumann

ps -ef | grep "memcache" | awk '{print }' | xargs kill; 
PID=`ps -ef | grep memcache | grep -v "grep" | awk '{print }'`
#...
ps -ef | grep "memcache" | awk '{print }' | xargs kill; 
PID=`ps -ef | grep memcache | grep -v "grep" | awk '{print }'`
#...

First, you could use cutinstead of awkin this case. No need to use a tank to kill a fly ;)

首先,在这种情况下,您可以使用cut代替awk。无需使用坦克杀死苍蝇;)

Then, why is it necessary to brutally kill memcache? You have a daemon to stop it :

那么,为什么要野蛮杀掉memcache呢?你有一个守护进程来阻止它:

/etc/init.d/memcached stop
service memcached stop