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

时间:2021-03-19 16:24:53

This question already has an answer here:

这个问题已经有了答案:

I want to write a shell script (.sh file) 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脚本(。为了获得一个给定的进程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脚本来实现它。

9 个解决方案

#1


35  

Using grep on the results of ps is 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 pgrep avoids 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.)

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

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

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

pkill ruby

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

顺便说一句,你应该知道在几乎所有的情况下使用-9都是过份的——在“无用地使用-9表格字母”的文本中有一些有用的建议:

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

不不不不要使用kill - 9。

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

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

  1. shut down socket connections
  2. 关闭套接字连接
  3. clean up temp files
  4. 清理临时文件
  5. inform its children that it is going away
  6. 告诉它的孩子它要走了
  7. reset its terminal characteristics
  8. 重置其终端特征

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!

一般情况下,发送15,然后等一两秒,如果不行,发送2,如果不行,发送1。如果没有,删除二进制,因为程序表现不佳!

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

不要使用kill - 9。不要把联合收割机拿出来只是为了清理花盆。

#2


17  

If you are going to use ps and grep then you should do it this way:

如果你打算使用ps和grep,那么你应该这样做:

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=$2
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.

backticks允许您捕获一个comand在一个shell变量中的输出。这个集合——将ps的输出解析成单词,而$2则是这一行的第二个单词,恰好是pid。然后,您发送一个术语信号,等待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发行版上的默认配置有关。希望[and]可以在任何地方工作,但是我不能再访问服务器,因为我知道它在没有反斜杠的情况下工作,所以我不能确定。

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,我以前就是这么做的,但是当我了解了[]变体之后,我决定最好在管道中少生成一个进程。

#3


6  

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

首先,不需要做ps -aux | grep…使用命令pidof要好得多。几乎从来没有杀过-9看到这里。

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

要从bash中的命令获取输出,请使用以下命令

pid=$(pidof ruby)

or use pkill directly.

或直接使用pkill。

#4


4  

option -v is very important. It can exclude a grep expression itself

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

e.g.

如。

ps -w | grep sshd | grep -v grep | awk '{print $1}' to get sshd id

#5


3  

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 '$0~z,NF=1' z=ruby | xargs kill -f

Bash Pitfalls

Bash的陷阱

#6


2  

You can use the command killall:

你可以使用命令killall:

$ killall ruby

#7


0  

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。然后这样做-9美元x

#8


-1  

To kill the process in shell

在shell中杀死进程

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

#9


-1  

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

如果你已经知道这个过程,那么这将是有用的:

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

#1


35  

Using grep on the results of ps is 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 pgrep avoids 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.)

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

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

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

pkill ruby

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

顺便说一句,你应该知道在几乎所有的情况下使用-9都是过份的——在“无用地使用-9表格字母”的文本中有一些有用的建议:

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

不不不不要使用kill - 9。

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

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

  1. shut down socket connections
  2. 关闭套接字连接
  3. clean up temp files
  4. 清理临时文件
  5. inform its children that it is going away
  6. 告诉它的孩子它要走了
  7. reset its terminal characteristics
  8. 重置其终端特征

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!

一般情况下,发送15,然后等一两秒,如果不行,发送2,如果不行,发送1。如果没有,删除二进制,因为程序表现不佳!

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

不要使用kill - 9。不要把联合收割机拿出来只是为了清理花盆。

#2


17  

If you are going to use ps and grep then you should do it this way:

如果你打算使用ps和grep,那么你应该这样做:

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=$2
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.

backticks允许您捕获一个comand在一个shell变量中的输出。这个集合——将ps的输出解析成单词,而$2则是这一行的第二个单词,恰好是pid。然后,您发送一个术语信号,等待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发行版上的默认配置有关。希望[and]可以在任何地方工作,但是我不能再访问服务器,因为我知道它在没有反斜杠的情况下工作,所以我不能确定。

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,我以前就是这么做的,但是当我了解了[]变体之后,我决定最好在管道中少生成一个进程。

#3


6  

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

首先,不需要做ps -aux | grep…使用命令pidof要好得多。几乎从来没有杀过-9看到这里。

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

要从bash中的命令获取输出,请使用以下命令

pid=$(pidof ruby)

or use pkill directly.

或直接使用pkill。

#4


4  

option -v is very important. It can exclude a grep expression itself

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

e.g.

如。

ps -w | grep sshd | grep -v grep | awk '{print $1}' to get sshd id

#5


3  

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 '$0~z,NF=1' z=ruby | xargs kill -f

Bash Pitfalls

Bash的陷阱

#6


2  

You can use the command killall:

你可以使用命令killall:

$ killall ruby

#7


0  

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。然后这样做-9美元x

#8


-1  

To kill the process in shell

在shell中杀死进程

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

#9


-1  

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

如果你已经知道这个过程,那么这将是有用的:

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