如何杀死Bash中的进程 [英] How to Kill Processes in Bash

查看:784
本文介绍了如何杀死Bash中的进程的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用了以下bash代码:

I used the following bash code:

for pid in `top -n 1 | awk '{if($8 == "R") print $1;}'`
do
  kill $pid
done

它说:

./kill.sh: line 3: kill: 29162: arguments must be process or job IDs
./kill.sh: line 3: kill: 29165: arguments must be process or job IDs
./kill.sh: line 3: kill: 29166: arguments must be process or job IDs
./kill.sh: line 3: kill: 29169: arguments must be process or job IDs

什么原因导致此错误?如何杀死Bash中的进程?

What causes this error and how do I kill processes in Bash?

推荐答案

在您当前的目录中似乎有一个脚本(可能是别名)kill.sh,它充当中介程序并调用内置的kill.但是,脚本将错误的参数传递给内置函数. (我看不到脚本,无法提供详细信息.)

It seems there may be a (possibly aliased) script kill.sh in your current directory which is acting as an intermediary and calling the kill builtin. However, the script is passing the wrong arguments to the builtin. (I can't give details without seeing the script.)

您的命令将使用内置的kill正常运行.最简单的解决方案是确保您使用内置的bash kill.执行:

Your command will work fine using the kill builtin. The simplest solution is to ensure you use the bash builtin kill. Execute:

chmod a-x kill.sh
unalias kill
unset -f kill

这将阻止脚本运行,并删除任何可能干扰您使用内置kill的别名或函数.

This will prevent the script from running and remove any alias or function that may interfere with your use of the kill builtin.

您还可以简化命令:

kill `top -n 1 | awk '{if($8 == "R") print $1;}'`

或者...

您还可以使用builtin绕过任何功能和别名:

Alternatively...

You can also use builtin to bypass any functions and aliases:

builtin kill `top -n 1 | awk '{if($8 == "R") print $1;}'`

这篇关于如何杀死Bash中的进程的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆