在 Linux 上如何通过名称而不是 PID 来终止进程?

发布于 2024-07-07 16:09:06 字数 589 浏览 8 评论 0原文

有时,当我尝试启动 Firefox 时,它会显示“Firefox 进程已在运行”。 所以我必须这样做:

jeremy@jeremy-desktop:~$ ps aux | grep firefox
jeremy    7451 25.0 27.4 170536 65680 ?        Sl   22:39   1:18 /usr/lib/firefox-3.0.1/firefox
jeremy    7578  0.0  0.3   3004   768 pts/0    S+   22:44   0:00 grep firefox
jeremy@jeremy-desktop:~$ kill 7451

我想要的是一个可以为我做这一切的命令。 它将在进程列表中获取输入字符串和 grep(或其他内容),并杀死输出中的所有进程:

jeremy@jeremy-desktop:~$ killbyname firefox

我尝试在 PHP 中执行此操作,但 exec(' ps aux') 似乎只显示在 PHP 脚本本身中使用 exec() 执行的进程(因此它显示的唯一进程就是它本身。)

Sometimes when I try to start Firefox it says "a Firefox process is already running". So I have to do this:

jeremy@jeremy-desktop:~$ ps aux | grep firefox
jeremy    7451 25.0 27.4 170536 65680 ?        Sl   22:39   1:18 /usr/lib/firefox-3.0.1/firefox
jeremy    7578  0.0  0.3   3004   768 pts/0    S+   22:44   0:00 grep firefox
jeremy@jeremy-desktop:~$ kill 7451

What I'd like is a command that would do all that for me. It would take an input string and grep for it (or whatever) in the list of processes, and would kill all the processes in the output:

jeremy@jeremy-desktop:~$ killbyname firefox

I tried doing it in PHP but exec('ps aux') seems to only show processes that have been executed with exec() in the PHP script itself (so the only process it shows is itself.)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(18

迟月 2024-07-14 16:09:06

也可以使用:

pkill -f "Process name"

对我来说,它的效果非常好。 这就是我一直在寻找的东西。
pkill 不适用于没有标志的名称。

当设置-f时,完整的命令行用于模式匹配。

Also possible to use:

pkill -f "Process name"

For me, it worked up perfectly. It was what I have been looking for.
pkill doesn't work with name without the flag.

When -f is set, the full command line is used for pattern matching.

黑寡妇 2024-07-14 16:09:06

您可以使用 killall< 按名称终止进程/代码>

killall 向所有人发送信号
运行任何指定的进程
命令。 如果没有信号名称
指定时,发送 SIGTERM。

信号可以通过以下方式指定
名称(例如 -HUP-SIGHUP )或按数字(例如
-1)或通过选项-s

如果命令名称不规则
表达式(选项-r)并包含
斜杠 (/),执行该操作的进程
将选择特定文件
杀戮,与他们的名字无关。

但是,如果您没有使用 ps aux 看到该进程,您可能无权杀死它......

You can kill processes by name with killall <name>

killall sends a signal to all
processes running any of the specified
commands. If no signal name is
specified, SIGTERM is sent.

Signals can be specified either by
name (e.g. -HUP or -SIGHUP ) or by number (e.g.
-1) or by option -s.

If the command name is not regular
expression (option -r) and contains a
slash (/), processes executing that
particular file will be selected for
killing, independent of their name.

But if you don't see the process with ps aux, you probably won't have the right to kill it ...

最冷一天 2024-07-14 16:09:06

最简单的方法是首先检查您是否获得了正确的进程 ID:

pgrep -f [part_of_a_command]

如果结果符合预期。 继续:

pkill -f [part_of_a_command]

如果进程卡住并且无法完成请求,您可以使用kill。

kill -9 $(pgrep -f [part_of_a_command])

如果您想安全起见并且只终止您最初启动的进程,请添加 -u 以及您的用户名

pkill -f [part_of_a_command] -u [username]

The easiest way to do is first check you are getting right process IDs with:

pgrep -f [part_of_a_command]

If the result is as expected. Go with:

pkill -f [part_of_a_command]

If processes get stuck and are unable to accomplish the request you can use kill.

kill -9 $(pgrep -f [part_of_a_command])

If you want to be on the safe side and only terminate processes that you initially started add -u along with your username

pkill -f [part_of_a_command] -u [username]
不气馁 2024-07-14 16:09:06

更长一点的替代方案:

kill `pidof firefox`

A bit longer alternative:

kill `pidof firefox`
苏大泽ㄣ 2024-07-14 16:09:06

终止启动路径中具有snippet的所有进程。您可以通过将 /directory/ 作为代码段来终止从某个目录启动的所有应用程序。 当您从同一应用程序目录启动同一应用程序的多个组件时,这非常有用。

ps ax | grep <snippet> | grep -v grep | awk '{print $1}' | xargs kill

* 如果可用的话我更喜欢 pgrep

Kill all processes having snippet in startup path. You can kill all apps started from some directory by for putting /directory/ as a snippet. This is quite usefull when you start several components for the same application from the same app directory.

ps ax | grep <snippet> | grep -v grep | awk '{print $1}' | xargs kill

* I would prefer pgrep if available

时光沙漏 2024-07-14 16:09:06

奇怪,但我还没有看到这样的解决方案:

kill -9 `pidof firefox`

它还可以杀死多个进程(多个pid),例如:

kill -9 `pgrep firefox`

我更喜欢pidof,因为它有单行输出:

> pgrep firefox
6316
6565
> pidof firefox
6565 6316

Strange, but I haven't seen the solution like this:

kill -9 `pidof firefox`

it can also kill multiple processes (multiple pids) like:

kill -9 `pgrep firefox`

I prefer pidof since it has single line output:

> pgrep firefox
6316
6565
> pidof firefox
6565 6316
倾城月光淡如水﹏ 2024-07-14 16:09:06

使用killall命令:

killall processname

使用-9-KILL强行杀死程序(选项与kill类似) > 命令)。

Using killall command:

killall processname

Use -9 or -KILL to forcefully kill the program (the options are similar to the kill command).

伴随着你 2024-07-14 16:09:06

在 Mac 上,我找不到 pgrep 和 pkill,killall 也不起作用,因此编写了一个简单的单行脚本:-

export pid=`ps | grep process_name | awk 'NR==1{print $1}' | cut -d' ' -f1`;kill $pid

如果有更简单的方法来执行此操作,请分享。

On Mac I could not find the pgrep and pkill neither was killall working so wrote a simple one liner script:-

export pid=`ps | grep process_name | awk 'NR==1{print $1}' | cut -d' ' -f1`;kill $pid

If there's an easier way of doing this then please share.

小傻瓜 2024-07-14 16:09:06

使用 grep 杀死:

kill -9 `pgrep myprocess`

To kill with grep:

kill -9 `pgrep myprocess`
梦巷 2024-07-14 16:09:06

更正确的是:

export pid=`ps aux | grep process_name | awk 'NR==1{print $2}' | cut -d' ' -f1`;kill -9 $pid

more correct would be:

export pid=`ps aux | grep process_name | awk 'NR==1{print $2}' | cut -d' ' -f1`;kill -9 $pid
哥,最终变帅啦 2024-07-14 16:09:06

我通常使用 killall 命令。

查看此链接了解此命令的详细信息。

I normally use the killall command.

Check this link for details of this command.

白鸥掠海 2024-07-14 16:09:06

我问自己同样的问题,但当前答案的问题是它们不能安全检查要终止的进程,所以...这可能会导致可怕的错误:)...尤其是如果有多个进程与该模式匹配

作为免责声明,我不是 sh 专业人士,而且肯定还有改进的空间。

所以我写了一个sh脚本:

#!/bin/sh

killables=$(ps aux | grep $1 | grep -v mykill | grep -v grep)
if [ ! "${killables}" = "" ]
then
  echo "You are going to kill some process:"
  echo "${killables}"
else
  echo "No process with the pattern $1 found."
  return
fi
echo -n "Is it ok?(Y/N)"
read input
if [ "$input" = "Y" ]
then
  for pid in $(echo "${killables}" | awk '{print $2}')
  do
    echo killing $pid "..."
    kill $pid 
    echo $pid killed
  done
fi

I was asking myself the same question but the problem with the current answers is that they don't safe check the processes to be killed so... it could lead to terrible mistakes :)... especially if several processes matches the pattern.

As a disclaimer, I'm not a sh pro and there is certainly room for improvement.

So I wrote a little sh script :

#!/bin/sh

killables=$(ps aux | grep $1 | grep -v mykill | grep -v grep)
if [ ! "${killables}" = "" ]
then
  echo "You are going to kill some process:"
  echo "${killables}"
else
  echo "No process with the pattern $1 found."
  return
fi
echo -n "Is it ok?(Y/N)"
read input
if [ "$input" = "Y" ]
then
  for pid in $(echo "${killables}" | awk '{print $2}')
  do
    echo killing $pid "..."
    kill $pid 
    echo $pid killed
  done
fi
燃情 2024-07-14 16:09:06

Kill -9 $(ps aux | grep -e myprocessname| awk '{ print $2 }')

kill -9 $(ps aux | grep -e myprocessname| awk '{ print $2 }')

勿忘心安 2024-07-14 16:09:06

如果您运行 GNOME,则可以使用系统监视器(系统 -> 管理 -> 系统监视器)来终止进程,就像在 Windows 下一样。 KDE 也会有类似的东西。

If you run GNOME, you can use the system monitor (System->Administration->System Monitor) to kill processes as you would under Windows. KDE will have something similar.

天赋异禀 2024-07-14 16:09:06

默认的 kill 命令接受命令名称作为 PID 的替代。 请参阅kill (1)。 经常出现的问题是 bash 提供了自己的 kill,它接受作业编号,例如 kill %1,但不接受命令名称。 这会妨碍默认命令。 如果前一个功能比后者对您更有用,您可以通过调用

enable -n Kill

禁用 bash 版本。有关详细信息,请参阅 kill > 和 bash 中的 enable 条目(1)

The default kill command accepts command names as an alternative to PID. See kill (1). An often occurring trouble is that bash provides its own kill which accepts job numbers, like kill %1, but not command names. This hinders the default command. If the former functionality is more useful to you than the latter, you can disable the bash version by calling

enable -n kill

For more info see kill and enable entries in bash (1).

尴尬癌患者 2024-07-14 16:09:06
ps aux | grep processname | cut -d' ' -f7 | xargs kill -9 $
ps aux | grep processname | cut -d' ' -f7 | xargs kill -9 $
℉絮湮 2024-07-14 16:09:06

awk oneliner,它解析 ps 输出的标题,因此您不需要关心列号(但列名)。 支持正则表达式。 例如,要终止所有可执行文件名称(不带路径)包含单词“firefox”的进程,请尝试

ps -fe | awk 'NR==1{for (i=1; i<=NF; i++) {if ($i=="COMMAND") Ncmd=i; else if ($i=="PID") Npid=i} if (!Ncmd || !Npid) {print "wrong or no header" > "/dev/stderr"; exit} }$Ncmd~"/"name"$"{print "killing "$Ncmd" with PID " $Npid; system("kill "$Npid)}' name=.*firefox.*

awk oneliner, which parses the header of ps output, so you don't need to care about column numbers (but column names). Support regex. For example, to kill all processes, which executable name (without path) contains word "firefox" try

ps -fe | awk 'NR==1{for (i=1; i<=NF; i++) {if ($i=="COMMAND") Ncmd=i; else if ($i=="PID") Npid=i} if (!Ncmd || !Npid) {print "wrong or no header" > "/dev/stderr"; exit} }$Ncmd~"/"name"$"{print "killing "$Ncmd" with PID " $Npid; system("kill "$Npid)}' name=.*firefox.*
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文