Linux 查找正在运行的进程的 PID 并存储为数组

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

Find the PID(s) of running processes and store as an array

arrayslinuxbash

提问by user3255401

I'm trying to write a bash script to find the PID of a running process then issue a kill command. I have it partially working, but the issue I face is that there may be more than one process running. I want to issue a kill command to each PID found.

我正在尝试编写一个 bash 脚本来查找正在运行的进程的 PID,然后发出一个 kill 命令。我让它部分工作,但我面临的问题是可能有多个进程在运行。我想向找到的每个 PID 发出一个 kill 命令。

I presume I need to put each PID in to an array but am at a loss as to how to do that.

我想我需要将每个 PID 放入一个数组中,但不知道如何做到这一点。

What I have so far:

到目前为止我所拥有的:

pid=$(ps -fe | grep '[p]rocess' | awk '{print }')
if [[ -n $pid ]]; then
    echo $pid
    #kill $pid
else
echo "Does not exist"
fi

What this will do is return all PIDs on a single line, but I can't figure out how to split this in to an array.

这将做的是在一行上返回所有 PID,但我无法弄清楚如何将其拆分为一个数组。

采纳答案by SparkyRobinson

Here's a little one liner that might help

这是一个可能有帮助的小衬垫

for pid in `ps -ef | grep your_search_term | awk '{print }'` ; do kill $pid ; done

Just replace your_search_termwith the process name you want to kill.

只需将your_search_term替换为您要终止的进程名称即可。

You could also make it into a script and swap your_search_termfor $1

你也可以将它变成一个脚本,并交换your_search_term$ 1

EDIT:I suppose I should explain how this works.

编辑:我想我应该解释一下这是如何工作的。

The back ticks `` collects the output from the expression inside it. In this case it will return a list of pids for a process name.

反勾号 `` 从其内部的表达式中收集输出。在这种情况下,它将返回进程名称的 pid 列表。

Using a for loop we can iterate through each pid and kill the process.

使用 for 循环,我们可以遍历每个 pid 并终止进程。

EDIT2:replaced kill -9 with kill

EDIT2:用kill替换kill -9

回答by Ell

Your script seems fine, if you want to have each pid list on a new like then replace:

您的脚本看起来不错,如果您想将每个 pid 列表放在一个新的喜欢然后替换:

echo $pid
#kill $pid

with

echo "$pid"
#kill "$pid"

回答by Michael Kropat

You don't need to use an array if you're going to immediately iterate over the results and perform an action:

如果您要立即迭代结果并执行操作,则不需要使用数组:

for pid in $(ps -fe | grep '[p]rocess' | grep -v grep | awk '{print }'); do
    kill "$pid"
done

Notice we have to exclude grep's pid from the list of processes to kill. Or we could just use pgrep(1):

请注意,我们必须grep从要杀死的进程列表中排除的 pid。或者我们可以使用pgrep(1)

for pid in $(pgrep '[p]rocess'); do
    kill "$pid"
done

If you actually needed to store the pids in an array, pgrepis how you would do it:

如果您确实需要将 pid 存储在数组中,pgrep那么您将如何做:

pids=( $(pgrep '[p]rocess') )

Back to killing process. We can still do better. If we're just using pgrepto get a list of processes to kill them, why not go straight for pgrep's sister program: pkill(1)?

回到杀死进程。我们仍然可以做得更好。如果我们只是pgrep用来获取一个进程列表来杀死它们,为什么不直接使用它pgrep的姊妹程序:pkill(1)

pkill '[p]rocess'

As it turns out, no need for bashscripting at all.

事实证明,根本不需要bash编写脚本。

回答by Reinstate Monica Please

Don't know why you would ever grep for a process to kill, unless you didn't know the command name. Most modern versions of pshave the flags

不知道为什么你会用 grep 来杀死一个进程,除非你不知道命令名称。大多数现代版本的ps都有标志

    -C cmdlist
          Select by command name.  This selects the processes whose executable name is given in cmdlist.

and

   -o format
          User-defined format.  format is a single argument in the form of
          a blank-separated or comma-separated list, which offers a way to
          specify individual output columns.  The recognized keywords are
          described in the STANDARD FORMAT SPECIFIERS section below.
          Headers may be renamed (ps -o pid,ruser=RealUser -o
          comm=Command) as desired.  If all column headers are empty (ps
          -o pid= -o comm=) then the header line will not be output.
          Column width will increase as needed for wide headers; this may
          be used to widen up columns such as WCHAN (ps -o pid,wchan=WIDE-
          WCHAN-COLUMN -o comm).  Explicit width control (ps opid,
          wchan:42,cmd) is offered too.  The behavior of ps -o pid=X,
          comm=Y varies with personality; output may be one column named
          "X,comm=Y" or two columns named "X" and "Y".  Use multiple -o
          options when in doubt.  Use the PS_FORMAT environment variable
          to specify a default as desired; DefSysV and DefBSD are macros
          that may be used to choose the default UNIX or BSD columns.

So you can just do

所以你可以做

ps -o pid= -C commandName 

Will return the pid of all processes named exactly commandName and is cleaner and faster. Or kill a loop

将返回完全命名为 commandName 的所有进程的 pid 并且更清晰、更快。或者杀死一个循环

while read -r pid; do 
  kill "$pid" 
done < <(ps -o pid= -C commandName)

But really, you should always just be able to do

但实际上,你应该总是能够做到

> pkill commandName