在 Linux 上获取进程 ID 的 Shell 脚本

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/6437602/
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-08-05 04:39:14  来源:igfitidea点击:

Shell script to get the process ID on Linux

linuxbashshellscripting

提问by sameera207

I want to write a shell script (.shfile) to get a given process id. What I'm trying to do here is once I get the process ID, I want to kill that process. I'm running on Ubuntu (Linux).

我想编写一个 shell 脚本(.sh文件)来获取给定的进程 ID。我在这里要做的是,一旦获得进程 ID,我就想终止该进程。我在 Ubuntu (Linux) 上运行。

I was able to do it with a command like

我能够用这样的命令来做到这一点

ps -aux|grep ruby
kill -9 <pid>

but I'm not sure how to do it through a shell script.

但我不确定如何通过 shell 脚本来做到这一点。

采纳答案by Mark Longair

Using grepon the results of psis a bad idea in a script, since some proportion of the time it will also match the grep process you've just invoked. The command pgrepavoids this problem, so if you need to know the process ID, that's a better option. (Note that, of course, there may be many processes matched.)

在脚本中使用grep结果ps是一个坏主意,因为在某些情况下它也会匹配您刚刚调用的 grep 进程。该命令pgrep避免了这个问题,因此如果您需要知道进程 ID,这是一个更好的选择。(注意,当然,可能有很多进程匹配。)

However, in your example, you could just use the similar command pkillto kill all matching processes:

但是,在您的示例中,您可以使用类似的命令pkill来终止所有匹配的进程:

pkill ruby

Incidentally, you should be aware that using -9is overkill (ho ho) in almost every case - there's some useful advice about that in the text of the "Useless Use of kill -9form letter ":

顺便说一句,您应该意识到-9在几乎所有情况下使用都是矫枉过正(ho ho) - 在“无用的套用kill -9信函”的文本中有一些关于此的有用建议:

No no no. Don't use kill -9.

It doesn't give the process a chance to cleanly:

  1. shut down socket connections
  2. clean up temp files
  3. inform its children that it is going away
  4. reset its terminal characteristics

and so on and so on and so on.

Generally, send 15, and wait a second or two, and if that doesn't work, send 2, and if that doesn't work, send 1. If that doesn't, REMOVE THE BINARY because the program is badly behaved!

Don't use kill -9. Don't bring out the combine harvester just to tidy up the flower pot.

不不不。不要使用kill -9.

它没有给进程一个干净的机会:

  1. 关闭套接字连接
  2. 清理临时文件
  3. 通知它的孩子它正在消失
  4. 重置其终端特性

等等等等等等。

通常,发送 15,然后等待一两秒钟,如果这不起作用,则发送 2,如果不起作用,则发送 1。如果没有,请删除二进制文件,因为该程序表现不佳!

不要使用kill -9. 不要为了整理花盆而拿出联合收割机。

回答by Fredrik Pihl

As a start there is no need to do a ps -aux | grep...The command pidofis far better to use. And almost never ever do kill -9see here

作为开始,不需要做一个ps -aux | grep...命令pidof,使用起来要好得多。而几乎从此再也kill -9这里

to get the output from a command in bash, use something like

要从 bash 中的命令获取输出,请使用类似

pid=$(pidof ruby)

or use pkilldirectly.

或者pkill直接使用。

回答by Francisco R

You can use the command killall:

您可以使用命令killall

$ killall ruby

回答by Michael Dillon

If you are going to use psand grepthen you should do it this way:

如果你要使用psgrep那么你应该这样做:

ps aux|grep r[u]by

Those square brackets will cause grep to skip the line for the grep command itself. So to use this in a script do:

这些方括号将导致 grep 跳过 grep 命令本身的行。因此,要在脚本中使用它,请执行以下操作:

output=`ps aux|grep r\[u\]by`
set -- $output
pid=
kill $pid
sleep 2
kill -9 $pid >/dev/null 2>&1

The backticks allow you to capture the output of a comand in a shell variable. The set --parses the ps output into words, and $2 is the second word on the line which happens to be the pid. Then you send a TERM signal, wait a couple of seconds for ruby to to shut itself down, then kill it mercilessly if it still exists, but throw away any output because most of the time kill -9 will complain that the process is already dead.

反引号允许您在 shell 变量中捕获命令的输出。的set --解析ps输出成单词,和$ 2是其上恰好是PID行第二个字。然后你发送一个 TERM 信号,等待几秒钟让 ruby​​ 关闭自己,然后如果它仍然存在就无情地杀死它,但是丢弃任何输出,因为大多数时候 kill -9 会抱怨进程已经死了.

I know that I have used this without the backslashes before the square brackets but just now I checked it on Ubuntu 12 and it seems to require them. This probably has something to do with bash's many options and the default config on different Linux distros. Hopefully the [ and ] will work anywhere but I no longer have access to the servers where I know that it worked without backslash so I cannot be sure.

我知道我在方括号前没有使用反斜杠,但刚才我在 Ubuntu 12 上检查了它,它似乎需要它们。这可能与 bash 的许多选项和不同 Linux 发行版上的默认配置有关。希望 [ 和 ] 可以在任何地方工作,但我不再可以访问我知道它可以在没有反斜杠的情况下工作的服务器,所以我不能确定。

One comment suggests grep-v and that is what I used to do, but then when I learned of the [] variant, I decided it was better to spawn one fewer process in the pipeline.

一个评论建议使用 grep-v,这就是我以前所做的,但是当我了解到 [] 变体时,我决定最好在管道中少生成一个进程。

回答by Steven Penny

This works in Cygwin but it should be effective in Linux as well.

这在 Cygwin 中有效,但在 Linux 中也应该有效。

ps -W | awk '/ruby/,NF=1' | xargs kill -f

or

或者

ps -W | awk '
ps -w | grep sshd | grep -v grep | awk '{print }' to get sshd id
~z,NF=1' z=ruby | xargs kill -f

Bash Pitfalls

Bash 陷阱

回答by user2223691

option -vis very important. It can exclude a grepexpression itself

选项-v很重要。它可以排除grep表达式本身

e.g.

例如

getprocess=`ps -ef|grep servername`
#echo $getprocess
set $getprocess 
pid=
#echo $pid
kill -9 $pid

回答by para

To kill the process in shell

在shell中杀死进程

PID=`ps -eaf | grep <process> | grep -v grep | awk '{print }'`
if [[ "" !=  "$PID" ]]; then
echo "killing $PID"
kill -9 $PID
fi

回答by abhishekrana

Its pretty simple. Simply Run Any Program like this :- x= gedit & echo $! this will give you PID of this process. then do this kill -9 $x

它很简单。只需像这样运行任何程序:- x= gedit & echo $! 这会给你这个过程的PID。然后这样做 kill -9 $x

回答by Francis

If you already know the process then this will be useful:

如果您已经知道该过程,那么这将很有用:

##代码##