使用 bash 和正则表达式在一行中查找并终止进程

发布于 2024-09-15 05:30:22 字数 444 浏览 5 评论 0原文

我在编程时经常需要杀死一个进程。

我现在的做法是:

[~]$ ps aux | grep 'python csp_build.py'
user    5124  1.0  0.3 214588 13852 pts/4    Sl+  11:19   0:00 python csp_build.py
user    5373  0.0  0.0   8096   960 pts/6    S+   11:20   0:00 grep python csp_build.py
[~]$ kill 5124

如何自动提取进程ID并在同一行中杀死它?

像这样:

[~]$ ps aux | grep 'python csp_build.py' | kill <regex that returns the pid>

I often need to kill a process during programming.

The way I do it now is:

[~]$ ps aux | grep 'python csp_build.py'
user    5124  1.0  0.3 214588 13852 pts/4    Sl+  11:19   0:00 python csp_build.py
user    5373  0.0  0.0   8096   960 pts/6    S+   11:20   0:00 grep python csp_build.py
[~]$ kill 5124

How can I extract the process id automatically and kill it in the same line?

Like this:

[~]$ ps aux | grep 'python csp_build.py' | kill <regex that returns the pid>

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

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

发布评论

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

评论(30

哭了丶谁疼 2024-09-22 05:30:22

bash 中,仅使用问题(1)中列出的基本工具,您应该能够执行以下操作:

kill $(ps aux | grep '[p]ython csp_build.py' | awk '{print $2}')

其工作原理的详细信息如下:

  • ps 为您提供所有进程的列表。
  • grep 根据您的搜索字符串进行过滤,[p] 是一种阻止您获取实际 grep 进程本身的技巧。
  • awk 只是为您提供每行的第二个字段,即 PID。
  • $(x) 构造意味着执行 x 然后获取其输出并将其放在命令行上。上面构造中的 ps 管道的输出是进程 ID 列表,因此您最终会得到类似 kill 1234 1122 7654 的命令。

这是显示其实际操作的文字记录:

pax> sleep 3600 &
[1] 2225
pax> sleep 3600 &
[2] 2226
pax> sleep 3600 &
[3] 2227
pax> sleep 3600 &
[4] 2228
pax> sleep 3600 &
[5] 2229
pax> kill $(ps aux | grep '[s]leep' | awk '{print $2}')
[5]+  Terminated              sleep 3600
[1]   Terminated              sleep 3600
[2]   Terminated              sleep 3600
[3]-  Terminated              sleep 3600
[4]+  Terminated              sleep 3600

您可以看到它终止了所有睡眠者。

更详细地解释 grep '[p]ython csp_build.py' 位:当您执行 sleep 3600 & 后跟 ps -ef | grep sleep,您往往会得到两个个带有 sleep 的进程,sleep 3600grep sleep< /code> (因为它们都有睡眠,这不是火箭科学)。

但是,ps -ef | grep '[s]leep' 不会创建一个包含 sleepgrep 进程,而是使用命令 grep '[ 创建一个进程s]leep' 这就是棘手的一点:grep 找不到那个,因为它正在寻找正则表达式“字符类 [s] 中的任何字符” (基本上就是 s),然后是 leep

换句话说,它正在寻找 sleep 但 grep 过程是 sleep。 code>grep '[s]leep' 其中没有文本 sleep

当我看到这个(由 SO 上的某人)时,我立即开始使用它,因为。

  • 这比添加 | grep -v grep 少了一个过程,而且
  • 又很狡猾,是一种罕见的组合:-)

(1); 仅限于使用这些基本工具,有一个漂亮的 pgrep 命令,它可以根据特定条件查找进程(当然,假设您的系统上有它) 。

例如,您可以使用 pgrep sleep 输出所有 sleep 命令的进程 ID(默认情况下,它与进程名称匹配)。如果您想匹配整个命令行,如 ps 所示,您可以执行 pgrep -f 'sleep 9999' 之类的操作。

顺便说一句,如果您执行 pgrep pgrep,它不会列出自身,因此在这种情况下不需要上面显示的棘手过滤方法。

您可以使用 -a 显示完整的进程名称来检查这些进程是否是您感兴趣的进程。您还可以使用 -u-U 将范围限制为您自己的进程(或一组特定的用户)。有关更多选项,请参阅 pgrep/pkillman 页面。

一旦您满意,它只会显示您感兴趣的进程,然后您可以使用具有相同参数的 pkill 向所有这些进程发送信号。

In bash, using only the basic tools listed in your question(1), you should be able to do:

kill $(ps aux | grep '[p]ython csp_build.py' | awk '{print $2}')

Details on its workings are as follows:

  • The ps gives you the list of all the processes.
  • The grep filters that based on your search string, [p] is a trick to stop you picking up the actual grep process itself.
  • The awk just gives you the second field of each line, which is the PID.
  • The $(x) construct means to execute x then take its output and put it on the command line. The output of that ps pipeline inside that construct above is the list of process IDs so you end up with a command like kill 1234 1122 7654.

Here's a transcript showing it in action:

pax> sleep 3600 &
[1] 2225
pax> sleep 3600 &
[2] 2226
pax> sleep 3600 &
[3] 2227
pax> sleep 3600 &
[4] 2228
pax> sleep 3600 &
[5] 2229
pax> kill $(ps aux | grep '[s]leep' | awk '{print $2}')
[5]+  Terminated              sleep 3600
[1]   Terminated              sleep 3600
[2]   Terminated              sleep 3600
[3]-  Terminated              sleep 3600
[4]+  Terminated              sleep 3600

and you can see it terminating all the sleepers.

Explaining the grep '[p]ython csp_build.py' bit in a bit more detail: when you do sleep 3600 & followed by ps -ef | grep sleep, you tend to get two processes with sleep in it, the sleep 3600 and the grep sleep (because they both have sleep in them, that's not rocket science).

However, ps -ef | grep '[s]leep' won't create a grep process with sleep in it, it instead creates one with the command grep '[s]leep' and here's the tricky bit: the grep doesn't find that one, because it's looking for the regular expression "any character from the character class [s] (which is basically just s) followed by leep.

In other words, it's looking for sleep but the grep process is grep '[s]leep' which doesn't have the text sleep in it.

When I was shown this (by someone here on SO), I immediately started using it because

  • it's one less process than adding | grep -v grep; and
  • it's elegant and sneaky, a rare combination :-)

(1) If you're not limited to using those basic tools, there's a nifty pgrep command which will find processes based on certain criteria (assuming you have it available on your system, of course).

For example, you can use pgrep sleep to output the process IDs for all sleep commands (by default, it matches the process name). If you want to match the entire command line as shown in ps, you can do something like pgrep -f 'sleep 9999'.

As an aside, it doesn't list itself if you do pgrep pgrep, so the tricky filter method shown above is not necessary in this case.

You can check that the processes are the ones you're interested in by using -a to show the full process names. You can also limit the scope to your own processes (or a specific set of users) with -u or -U. See the man page for pgrep/pkill for more options.

Once you're satisfied it will only show the processes you're interested in, you can then use pkill with the same parameters to send a signal to all those processes.

时光磨忆 2024-09-22 05:30:22

如果你有 pkill,

pkill -f csp_build.py

如果你只想对进程名称(而不是完整的参数列表)进行 grep,那么请忽略 -f

if you have pkill,

pkill -f csp_build.py

If you only want to grep against the process name (instead of the full argument list) then leave off -f.

风和你 2024-09-22 05:30:22

一行:

ps aux | grep -i csp_build | grep -i csp_build | grep -i csp_build | grep -i csp_build | awk '{print $2}' | xargs sudo Kill -9

  • 打印第 2 列: awk '{print $2}'
  • sudo 是可选的
  • Run kill -9 5124kill -9 5373 等(kill -15 更优雅,但速度稍慢)

额外:

我还在 .bash_profile 中定义了 2 个快捷函数
(~/.bash_profile 适用于 osx,你必须看看什么适用于你的 *nix 机器)。

  1. p 关键字
    • 列出所有包含关键字的进程流程
    • 用法例如:p csp_buildp python

bash_profile 代码:

# FIND PROCESS
function p(){
        ps aux | grep -i $1 | grep -v grep
}
  1. ka 关键字
    • 杀死所有具有此关键字的进程
    • 用法例如:ka csp_buildka python
    • 可选终止级别,例如:ka csp_build 15ka python 9

bash_profile代码:

# KILL ALL
function ka(){

    cnt=$( p $1 | wc -l)  # total count of processes found
    klevel=${2:-15}       # kill level, defaults to 15 if argument 2 is empty

    echo -e "\nSearching for '$1' -- Found" $cnt "Running Processes .. "
    p $1

    echo -e '\nTerminating' $cnt 'processes .. '

    ps aux  |  grep -i $1 |  grep -v grep   | awk '{print $2}' | xargs sudo kill -klevel
    echo -e "Done!\n"

    echo "Running search again:"
    p "$1"
    echo -e "\n"
}

One liner:

ps aux | grep -i csp_build | awk '{print $2}' | xargs sudo kill -9

  • Print out column 2: awk '{print $2}'
  • sudo is optional
  • Run kill -9 5124, kill -9 5373 etc (kill -15 is more graceful but slightly slower)

Bonus:

I also have 2 shortcut functions defined in my .bash_profile
(~/.bash_profile is for osx, you have to see what works for your *nix machine).

  1. p keyword
    • lists out all Processes containing keyword
    • usage e.g: p csp_build , p python etc

bash_profile code:

# FIND PROCESS
function p(){
        ps aux | grep -i $1 | grep -v grep
}
  1. ka keyword
    • Kills All processes that have this keyword
    • usage e.g: ka csp_build , ka python etc
    • optional kill level e.g: ka csp_build 15, ka python 9

bash_profile code:

# KILL ALL
function ka(){

    cnt=$( p $1 | wc -l)  # total count of processes found
    klevel=${2:-15}       # kill level, defaults to 15 if argument 2 is empty

    echo -e "\nSearching for '$1' -- Found" $cnt "Running Processes .. "
    p $1

    echo -e '\nTerminating' $cnt 'processes .. '

    ps aux  |  grep -i $1 |  grep -v grep   | awk '{print $2}' | xargs sudo kill -klevel
    echo -e "Done!\n"

    echo "Running search again:"
    p "$1"
    echo -e "\n"
}
寻找我们的幸福 2024-09-22 05:30:22
killall -r regexp

-r, --regexp

将进程名称模式解释为扩展的正则表达式。

killall -r regexp

-r, --regexp

Interpret process name pattern as an extended regular expression.

情未る 2024-09-22 05:30:22

这将仅返回 pid

pgrep -f 'process_name'

因此,要在一行中终止任何进程:

kill -9 $(pgrep -f 'process_name')

或者,如果您知道进程的确切名称,您也可以尝试 pidof:

kill -9 $(pidof 'process_name')

但是,如果您不知道进程的确切名称,pgrep 会更好。

如果有多个同名进程正在运行,并且您想杀死第一个进程,那么:

kill -9 $(pgrep -f 'process_name' | head -1)

还要注意,如果您担心区分大小写,那么您可以像在 grep 中一样添加 -i 选项。例如:

kill -9 $(pgrep -fi chrome)

有关信号和 pgrep 的更多信息,请参见 man 7 signalman signalman pgrep

This will return the pid only

pgrep -f 'process_name'

So to kill any process in one line:

kill -9 $(pgrep -f 'process_name')

or, if you know the exact name of the process you can also try pidof:

kill -9 $(pidof 'process_name')

But, if you do not know the exact name of the process, pgrep would be better.

If there is multiple process running with the same name, and you want to kill the first one then:

kill -9 $(pgrep -f 'process_name' | head -1)

Also to note that, if you are worried about case sensitivity then you can add -i option just like in grep. For example:

kill -9 $(pgrep -fi chrome)

More info about signals and pgrep at man 7 signal or man signal and man pgrep

云巢 2024-09-22 05:30:22

尝试使用

ps aux | grep 'python csp_build.py' | head -1 | cut -d " " -f 2 | xargs kill

Try using

ps aux | grep 'python csp_build.py' | head -1 | cut -d " " -f 2 | xargs kill
北斗星光 2024-09-22 05:30:22

您只能使用 pkill '^python*' 进行正则表达式进程终止。

如果您想在杀死之前查看要杀死或找到的内容,只需使用 pgrep -l '^python*' ,其中 -l 还输出进程的名称。如果你不想使用
pkill,仅使用:

pgrep '^python*' | xargs 杀死

You may use only pkill '^python*' for regex process killing.

If you want to see what you gonna kill or find before killing just use pgrep -l '^python*' where -l outputs also name of the process. If you don't want to use
pkill, use just:

pgrep '^python*' | xargs kill

去了角落 2024-09-22 05:30:22

使用 pgrep - 在许多平台上可用:

kill -9 `pgrep -f cps_build`

pgrep -f 将返回与“cps_build”一致的所有 PID

Use pgrep - available on many platforms:

kill -9 `pgrep -f cps_build`

pgrep -f will return all PIDs with coincidence "cps_build"

完美的未来在梦里 2024-09-22 05:30:22

解决方案将使用精确的模式过滤进程,解析pid,并构造一个用于执行终止进程的参数列表:

ps -ef  | grep -e <serviceNameA> -e <serviceNameB> -e <serviceNameC> |
awk '{print $2}' | xargs sudo kill -9

来自文档的解释

ps 实用程序显示标题行,后跟包含以下内容的行
有关具有控制终端的所有进程的信息。

-e 显示有关其他用户进程的信息,包括那些

-f显示uid、pid、父pid、最近CPU使用率、进程启动

grep 实用程序搜索任何给定的输入文件,选择符合以下条件的行

-e 模式,--regexp=pattern
指定搜索输入期间使用的模式:输入
如果该行与任何指定模式匹配,则该行被选中。
当使用多个 -e 选项时,此选项最有用
指定多个模式,或者当模式以破折号开头时
(`-')。

xargs - 构造参数列表并执行实用程序

kill - 终止或向进程发出信号

数字9信号 - KILL(不可捕获、不可忽略的杀戮)

示例

ps -ef  | grep -e node -e loggerUploadService.sh -e applicationService.js |
awk '{print $2}' | xargs sudo kill -9

The solution would be filtering the processes with exact pattern , parse the pid, and construct an argument list for executing kill processes:

ps -ef  | grep -e <serviceNameA> -e <serviceNameB> -e <serviceNameC> |
awk '{print $2}' | xargs sudo kill -9

Explanation from documenation:

ps utility displays a header line, followed by lines containing
information about all of your processes that have controlling terminals.

-e Display information about other users' processes, including those

-f Display the uid, pid, parent pid, recent CPU usage, process start

The grep utility searches any given input files, selecting lines that

-e pattern, --regexp=pattern
Specify a pattern used during the search of the input: an input
line is selected if it matches any of the specified patterns.
This option is most useful when multiple -e options are used to
specify multiple patterns, or when a pattern begins with a dash
(`-').

xargs - construct argument list(s) and execute utility

kill - terminate or signal a process

number 9 signal - KILL (non-catchable, non-ignorable kill)

Example:

ps -ef  | grep -e node -e loggerUploadService.sh -e applicationService.js |
awk '{print $2}' | xargs sudo kill -9
好听的两个字的网名 2024-09-22 05:30:22

您可以使用 awk 和 backtics $2 in awk 打印第 2 列来完成此操作

ps auxf |grep 'python csp_build.py'|`awk '{ print "kill " $2 }'`

,并且 backtics 运行打印的语句。

但更简洁的解决方案是让 python 进程将其进程 ID 存储在 /var/run 中,然后您可以简单地读取该文件并杀死它。

you can do it with awk and backtics

ps auxf |grep 'python csp_build.py'|`awk '{ print "kill " $2 }'`

$2 in awk prints column 2, and the backtics runs the statement that's printed.

But a much cleaner solution would be for the python process to store it's process id in /var/run and then you can simply read that file and kill it.

萝莉病 2024-09-22 05:30:22

我的任务是杀死放置在特定目录中的所有匹配正则表达式的内容(在硒测试之后,并非所有内容都停止)。这对我有用:

for i in `ps aux | egrep "firefox|chrome|selenium|opera"|grep "/home/dir1/dir2"|awk '{print $2}'|uniq`; do kill $i; done

My task was kill everything matching regexp that is placed in specific directory (after selenium tests not everything got stop). This worked for me:

for i in `ps aux | egrep "firefox|chrome|selenium|opera"|grep "/home/dir1/dir2"|awk '{print $2}'|uniq`; do kill $i; done
窗影残 2024-09-22 05:30:22

这里有很多好的答案 - 我使用了OP接受的答案。只需添加有关 pkillpgrep 的小警告。正如您可能从其手册页中看到的,在您的操作系统上,某些操作系统有 15-进程名称的字符限制。 -f 选项在我的操作系统上解决了这个问题,但在找到该选项之前我遇到了麻烦!

Lots of good answers here - I used the answer accepted by the OP. Just adding a small caveat note about pkill and pgrep. As you might see from their manual pages, on your OS, some OS's have a 15-character limit on the process name. The -f option gets around that on my OS, but I was in trouble until I found that option!

一抹苦笑 2024-09-22 05:30:22

通过关键字 midori 终止进程,例如:

kill -SIGTERM $(pgrep -i midori)

To kill process by keyword midori, for example:

kill -SIGTERM $(pgrep -i midori)

爱殇璃 2024-09-22 05:30:22
ps -o uid,pid,cmd|awk '{if($1=="username" && $3=="your command") print $2}'|xargs kill -15
ps -o uid,pid,cmd|awk '{if($1=="username" && $3=="your command") print $2}'|xargs kill -15
最近可好 2024-09-22 05:30:22

仅使用 awk(和 ps)的方法:

ps aux | awk '$11" "$12 == "python csp_build.py" { system("kill " $2) }'

通过使用字符串相等性测试,我可以防止匹配此过程本身。

A method using only awk (and ps):

ps aux | awk '$11" "$12 == "python csp_build.py" { system("kill " $2) }'

By using string equality testing I prevent matching this process itself.

巷子口的你 2024-09-22 05:30:22

给 -f 给 pkill

pkill -f /usr/local/bin/fritzcap.py

.py 文件的确切路径是

# ps ax | grep fritzcap.py
 3076 pts/1    Sl     0:00 python -u /usr/local/bin/fritzcap.py -c -d -m

Give -f to pkill

pkill -f /usr/local/bin/fritzcap.py

exact path of .py file is

# ps ax | grep fritzcap.py
 3076 pts/1    Sl     0:00 python -u /usr/local/bin/fritzcap.py -c -d -m
我很OK 2024-09-22 05:30:22

我开始使用这样的东西:

kill $(pgrep 'python csp_build.py')

I started using something like this:

kill $(pgrep 'python csp_build.py')
一身骄傲 2024-09-22 05:30:22
pkill -f 'PATTERN'

将杀死模式 PATTERN 匹配的所有进程。使用 -f 选项,将考虑整个命令行(即包括参数)。如果没有 -f 选项,则仅考虑命令名称。

pkill -f 'PATTERN'

Will kill all the processes that the pattern PATTERN matches. With the -f option, the whole command line (i.e. including arguments) will be taken into account. Without the -f option, only the command name will be taken into account.

十年九夏 2024-09-22 05:30:22

ps 不需要用户切换。

kill `ps ax | grep 'python csp_build.py' | awk '{print $1}'`

You don't need the user switch for ps.

kill `ps ax | grep 'python csp_build.py' | awk '{print $1}'`
梦在深巷 2024-09-22 05:30:22

在某些情况下,我想像这样同时终止进程:

➜  ~  sleep 1000 &
[1] 25410
➜  ~  sleep 1000 &
[2] 25415
➜  ~  sleep 1000 &
[3] 25421
➜  ~  pidof sleep
25421 25415 25410
➜  ~  kill `pidof sleep`
[2]  - 25415 terminated  sleep 1000                                                             
[1]  - 25410 terminated  sleep 1000
[3]  + 25421 terminated  sleep 1000

但是,我认为这在你的情况下有点不合适。(可能后台正在运行 python a、python b、python x...。 )

In some cases, I'd like kill processes simutaneously like this way:

➜  ~  sleep 1000 &
[1] 25410
➜  ~  sleep 1000 &
[2] 25415
➜  ~  sleep 1000 &
[3] 25421
➜  ~  pidof sleep
25421 25415 25410
➜  ~  kill `pidof sleep`
[2]  - 25415 terminated  sleep 1000                                                             
[1]  - 25410 terminated  sleep 1000
[3]  + 25421 terminated  sleep 1000

But, I think it is a little bit inappropriate in your case.(May be there are running python a, python b, python x...in the background.)

半步萧音过轻尘 2024-09-22 05:30:22

在 bash 的一行中查找并杀死所有进程。

kill -9 $(ps -ef | grep '<exe_name>' | grep -v 'grep' | awk {'print $2'})
  • ps -ef | ps -ef | ps -ef | ps -ef | ps -ef | ps -ef | grep '' - 给出与模式匹配的正在运行的进程详细信息(uname、pid 等)列表。输出列表包含此 grep 命令,该命令也可对其进行搜索。现在为了杀死我们需要忽略这个 grep 命令过程。
  • ps -ef | ps -ef | ps -ef | ps -ef | ps -ef | ps -ef | grep '' | grep -v 'grep' - 使用 -v 'grep' 添加另一个 grep 会删除当前的 grep 进程。
  • 然后使用awk单独获取进程ID。
  • 然后将此命令保留在 $(...) 中并将其传递给 kill 命令,以终止所有进程。

Find and kill all the processes in one line in bash.

kill -9 $(ps -ef | grep '<exe_name>' | grep -v 'grep' | awk {'print $2'})
  • ps -ef | grep '<exe_name>' - Gives the list of running process details (uname, pid, etc ) which matches the pattern. Output list includes this grep command also which searches it. Now for killing we need to ignore this grep command process.
  • ps -ef | grep '<exec_name>' | grep -v 'grep' - Adding another grep with -v 'grep' removes the current grep process.
  • Then using awk get the process id alone.
  • Then keep this command inside $(...) and pass it to kill command, to kill all process.
意犹 2024-09-22 05:30:22

如果 pkill -f csp_build.py 没有终止进程,您可以添加 -9 来发送不会被忽略的终止信号。即 pkill -9 -f csp_build.py

If pkill -f csp_build.py doesn't kill the process you can add -9 to send a kill signall which will not be ignored. i.e. pkill -9 -f csp_build.py

∞琼窗梦回ˉ 2024-09-22 05:30:22

使用 ps 命令的 -C 标志

<前><代码>-C cmdlist
按命令名称选择。这将选择其进程
可执行文件名称在 cmdlist 中给出。

第一种情况,简单的命令

因此,如果您通过标准 shebang 运行脚本并通过他的名字调用它们:

/path/to/csp_build.py

您可能会发现它们

ps -C csp_build.py

所以

kill $(ps -C csp_build.py ho pid)

可能就足够了。

第二种情况,搜索 cmd

更强一点,但仍然比这个问题中的大多数其他答案快得多...

如果您不知道这是运行的,或者如果您运行它们,

python csp_build.py
python3 csp_build.py
python /path/to/csp_build.py

您可以通过运行找到它们:

ps -C python,python3,csp_build.py who pid,cmd | grep csp_build.py

然后使用 sed:

kill $(ps -C python,python3,csp_build.py who pid,cmd |
    sed -ne '/csp_build.py/s/^ *\([0-9]\+\) .*$/\1/p')

Using -C flag of ps command

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

1st case, simple command

So if you run your script by standard shebang and calling them by his name:

/path/to/csp_build.py

You may find them whith

ps -C csp_build.py

So

kill $(ps -C csp_build.py ho pid)

may be enough.

2nd case, search for cmd

A little more strong, but still a lot quicker than most other answer in this SO question...

If you don't know ho this is run, or if you run them by

python csp_build.py
python3 csp_build.py
python /path/to/csp_build.py

You may find them by running:

ps -C python,python3,csp_build.py who pid,cmd | grep csp_build.py

Then using sed:

kill $(ps -C python,python3,csp_build.py who pid,cmd |
    sed -ne '/csp_build.py/s/^ *\([0-9]\+\) .*$/\1/p')
影子的影子 2024-09-22 05:30:22

基于 https://stackoverflow.com/a/3510879/15603477 答案。小幅优化。

ps aux | grep 'python csp_build.py' | head -1 | tr -s ' ' | cut -d " " -f 2 | xargs kill

通过使用 tr -s ' ' 将多个空格(如果有)压缩为 1 个空格。

如果您遇到不允许操作,请按照此>>> https://unix. stackexchange.com/questions/89316/how-to-kill-a-process-that-says-operation-not-permissed-when-attempted

Based on https://stackoverflow.com/a/3510879/15603477 answer. Minor optimization.

ps aux | grep 'python csp_build.py' | head -1 | tr -s ' ' | cut -d " " -f 2 | xargs kill

By using tr -s ' ' to squeeze multi whitespaces (if have) to 1 white space.

In case you encountered Operation not permitted follow this>> https://unix.stackexchange.com/questions/89316/how-to-kill-a-process-that-says-operation-not-permitted-when-attempted

找回味觉 2024-09-22 05:30:22

终止从公共 PPID 启动的我们自己的进程非常频繁,与 -P 标志关联的 pkill 对我来说是赢家。使用 @ghostdog74 示例:

# sleep 30 &                                                                                                      
[1] 68849
# sleep 30 &
[2] 68879
# sleep 30 &
[3] 68897
# sleep 30 &
[4] 68900
# pkill -P $                                                                                                         
[1]   Terminated              sleep 30
[2]   Terminated              sleep 30
[3]-  Terminated              sleep 30
[4]+  Terminated              sleep 30

Kill our own processes started from a common PPID is quite frequently, pkill associated to the –P flag is a winner for me. Using @ghostdog74 example :

# sleep 30 &                                                                                                      
[1] 68849
# sleep 30 &
[2] 68879
# sleep 30 &
[3] 68897
# sleep 30 &
[4] 68900
# pkill -P $                                                                                                         
[1]   Terminated              sleep 30
[2]   Terminated              sleep 30
[3]-  Terminated              sleep 30
[4]+  Terminated              sleep 30
王权女流氓 2024-09-22 05:30:22

当 Firefox 受到脚本攻击和 cpu 攻击时,我用它来杀死 Firefox :)
将“Firefox”替换为您想要终止的应用程序。我使用的是 Bash shell - OS X 10.9.3 Darwin。

kill -Hup $(ps ux | grep Firefox | awk 'NR == 1 {next} {print $2}' | uniq | sort)

I use this to kill Firefox when it's being script slammed and cpu bashing :)
Replace 'Firefox' with the app you want to die. I'm on the Bash shell - OS X 10.9.3 Darwin.

kill -Hup $(ps ux | grep Firefox | awk 'NR == 1 {next} {print $2}' | uniq | sort)

哭了丶谁疼 2024-09-22 05:30:22

我使用 gkill processname,其中 gkill 是以下脚本:

cnt=`ps aux|grep $1| grep -v "grep" -c`
if [ "$cnt" -gt 0 ]
then
    echo "Found $cnt processes - killing them"
    ps aux|grep $1| grep -v "grep"| awk '{print $2}'| xargs kill
else
    echo "No processes found"
fi

注意:它不会杀死命令行中包含“grep”的进程。

I use gkill processname, where gkill is the following script:

cnt=`ps aux|grep $1| grep -v "grep" -c`
if [ "$cnt" -gt 0 ]
then
    echo "Found $cnt processes - killing them"
    ps aux|grep $1| grep -v "grep"| awk '{print $2}'| xargs kill
else
    echo "No processes found"
fi

NOTE: it will NOT kill processes that have "grep" in their command lines.

夏雨凉 2024-09-22 05:30:22

如果您想主要awk中完成,请尝试

  for i in $(jot 5); do 
 (python3 -c 'import sys; [ print(_) for _ in sys.stdin ]' ) & done; 
  
  sleep 1; ps aux | {m,g}awk '

       /[p]ython/ { 
              _=(_)" "$2 
       } END { 
           system("echo \47 kill "(_)" \47")
           system(      "kill -9 " _) }'


[302] 14236
[303] 14237
[304] 14238
[305] 14239
[306] 14240
[303]  + suspended (tty input)  ( python3 -c 'import sys; [ print(_) for _ in sys.stdin ]'; )
[305]  + suspended (tty input)  ( python3 -c 'import sys; [ print(_) for _ in sys.stdin ]'; )
[304]  + suspended (tty input)  ( python3 -c 'import sys; [ print(_) for _ in sys.stdin ]'; )
[302]  + suspended (tty input)  ( python3 -c 'import sys; [ print(_) for _ in sys.stdin ]'; )
[306]  + suspended (tty input)  ( python3 -c 'import sys; [ print(_) for _ in sys.stdin ]'; )
 
 kill  14239 14237 14236 14240 14238 
 
[305]    killed     ( python3 -c 'import sys; [ print(_) for _ in sys.stdin ]'; )
[303]    killed     ( python3 -c 'import sys; [ print(_) for _ in sys.stdin ]'; )
[306]  + killed     ( python3 -c 'import sys; [ print(_) for _ in sys.stdin ]'; )
[304]  - killed     ( python3 -c 'import sys; [ print(_) for _ in sys.stdin ]'; )
[302]  + killed     ( python3 -c 'import sys; [ print(_) for _ in sys.stdin ]'; )

if you wanna do it mostly within awk, try

  for i in $(jot 5); do 
 (python3 -c 'import sys; [ print(_) for _ in sys.stdin ]' ) & done; 
  
  sleep 1; ps aux | {m,g}awk '

       /[p]ython/ { 
              _=(_)" "$2 
       } END { 
           system("echo \47 kill "(_)" \47")
           system(      "kill -9 " _) }'


[302] 14236
[303] 14237
[304] 14238
[305] 14239
[306] 14240
[303]  + suspended (tty input)  ( python3 -c 'import sys; [ print(_) for _ in sys.stdin ]'; )
[305]  + suspended (tty input)  ( python3 -c 'import sys; [ print(_) for _ in sys.stdin ]'; )
[304]  + suspended (tty input)  ( python3 -c 'import sys; [ print(_) for _ in sys.stdin ]'; )
[302]  + suspended (tty input)  ( python3 -c 'import sys; [ print(_) for _ in sys.stdin ]'; )
[306]  + suspended (tty input)  ( python3 -c 'import sys; [ print(_) for _ in sys.stdin ]'; )
 
 kill  14239 14237 14236 14240 14238 
 
[305]    killed     ( python3 -c 'import sys; [ print(_) for _ in sys.stdin ]'; )
[303]    killed     ( python3 -c 'import sys; [ print(_) for _ in sys.stdin ]'; )
[306]  + killed     ( python3 -c 'import sys; [ print(_) for _ in sys.stdin ]'; )
[304]  - killed     ( python3 -c 'import sys; [ print(_) for _ in sys.stdin ]'; )
[302]  + killed     ( python3 -c 'import sys; [ print(_) for _ in sys.stdin ]'; )
生活了然无味 2024-09-22 05:30:22

在Mac上,我尝试了从此线程中截取的每一个片段来杀死卡住并阻止注销的slack,但唯一有效的方法是打开ActivityMonitor,找到slack和4个slackhelper进程并强制退出它们。

on Mac, I tried every snipped from this thread to kill slack that got stuck and prevented logout, but the only thing that worked was to open ActivityMonitor, find there slack and 4 slackhelper processes and forcefully quit them all.

安穩 2024-09-22 05:30:22

以下命令会很方便:

kill $(ps -elf | grep| awk {'print $4'})

例如,
ps -elf | ps -elf | ps -elf | ps -elf | ps -elf | ps -elf grep top

    0 T ubuntu    6558  6535  0  80   0 -  4001 signal 11:32 pts/1    00:00:00 top
    0 S ubuntu    6562  6535  0  80   0 -  2939 pipe_w 11:33 pts/1    00:00:00 grep --color=auto top

kill -$(ps -elf | grep top| awk {'print $4'})

    -bash: kill: (6572) - No such process
    [1]+  Killed                  top

如果进程仍然卡住,请使用“-9”扩展名来hardkill,如下:

kill -9 $(ps -elf | grep top| awk {'print $4'})

希望有帮助...!

The following command will come handy:

kill $(ps -elf | grep <process_regex>| awk {'print $4'})

eg.,
ps -elf | grep top

    0 T ubuntu    6558  6535  0  80   0 -  4001 signal 11:32 pts/1    00:00:00 top
    0 S ubuntu    6562  6535  0  80   0 -  2939 pipe_w 11:33 pts/1    00:00:00 grep --color=auto top

kill -$(ps -elf | grep top| awk {'print $4'})

    -bash: kill: (6572) - No such process
    [1]+  Killed                  top

If the process is still stuck, use "-9" extension to hardkill, as follows:

kill -9 $(ps -elf | grep top| awk {'print $4'})

Hope that helps...!

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文