bash 捕获进程 ID 并在存在时将其杀死的 Shell 脚本

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

Shell script to capture Process ID and kill it if exist

bashshellscripting

提问by user1597811

I tried this code and it is not working

我试过这段代码,但它不起作用

#!/bin/sh

#Find the Process ID for syncapp running instance

PID=`ps -ef | grep syncapp 'awk {print }'`

if [[ -z "$PID" ]] then
Kill -9 PID
fi

It is showing a error near awk.

它在 awk 附近显示错误。

Any suggestions please.

请提出任何建议。

回答by Anthony Mutisya

Actually the easiest way to do that would be to pass kill arguments like below:

实际上,最简单的方法是传递如下的 kill 参数:

ps -ef | grep your_process_name | grep -v grep | awk '{print }' | xargs kill

Hope it helps.

希望能帮助到你。

回答by N Dierauf

This works good for me.

这对我很有用。

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

回答by guettli

I use the command pkillfor this:

pkill为此使用命令:

NAME
       pgrep, pkill - look up or signal processes based on name and 
       other attributes

SYNOPSIS
       pgrep [options] pattern
       pkill [options] pattern

DESCRIPTION
       pgrep looks through the currently running processes and lists 
       the process IDs which match the selection criteria to stdout.
       All the criteria have to match.  For example,

              $ pgrep -u root sshd

       will only list the processes called sshd AND owned by root.
       On the other hand,

              $ pgrep -u root,daemon

       will list the processes owned by root OR daemon.

       pkill will send the specified signal (by default SIGTERM) 
       to each process instead of listing them on stdout.

If your code runs via interpreter (java, python, ...) then the name of the process is the name of the interpreter. You need to user the argument --full. This matches against the command name and the arguments.

如果您的代码通过解释器(java、python、...)运行,那么进程的名称就是解释器的名称。您需要使用参数 --full。这与命令名称和参数匹配。

回答by Amadan

You probably wanted to write

你可能想写

`ps -ef | grep syncapp | awk '{print }'`

but I will endorse @PaulR's answer - killall -9 syncappis a much better alternative.

但我会赞同@PaulR 的回答——这killall -9 syncapp是一个更好的选择。

回答by jbr

A lot of *NIX systems also have either or both pkill(1)and killall(1)which, allows you to kill processes by name. Using them, you can avoid the whole parsing psproblem.

许多 *NIX 系统也有pkill(1)killall(1) 之一或两者,它们允许您按名称杀死进程。使用它们,您可以避免整个解析ps问题。

回答by user3743785

Came across somewhere..thought it is simple and useful

在某个地方遇到过..认为它简单而有用

You can use the command in crontab directly ,

可以直接在 crontab 中使用该命令,

* * * * * ps -lf | grep "user" |  perl -ane '($h,$m,$s) = split /:/,$F
+[13]; kill 9, $F[3] if ($h > 1);'

or, we can write it as shell script ,

或者,我们可以把它写成 shell script ,

#!/bin/sh
# longprockill.sh
ps -lf | grep "user" |  perl -ane '($h,$m,$s) = split /:/,$F[13]; kill
+ 9, $F[3] if ($h > 1);'

And call it crontab like so,

并像这样称其为 crontab,

* * * * * longprockill.sh

回答by AnonManon

#!/bin/sh

#Find the Process ID for syncapp running instance

PID=`ps -ef | grep syncapp 'awk {print }'`

if [[ -z "$PID" ]] then
--->    Kill -9 PID
fi

Not sure if this helps, but 'kill' is not spelled correctly. It's capitalized.

不确定这是否有帮助,但“kill”拼写不正确。它是大写的。

Try 'kill' instead.

尝试“杀死”。

回答by EvR2f

This should kill all processes matching the grep that you are permitted to kill.

这应该会杀死与您被允许杀死的 grep 匹配的所有进程。

-9 means "Kill all processes you can kill".

-9 表示“杀死所有可以杀死的进程”。

kill -9 $(ps -ef | grep [s]yncapp | awk '{print }')

回答by Not_a_User

Kill -9 PID

should be

应该

kill -9 $PID

see the difference?

看到不同?

回答by chiranjeevi

Try the following script:

尝试以下脚本:

#!/bin/bash
pgrep  2>&1 > /dev/null
if [ $? -eq 0 ]
then
{
    echo " "" PROCESS RUNNING "
    ps -ef | grep  | grep -v grep | awk '{print }'| xargs kill -9
}
else
{
    echo "  NO  PROCESS RUNNING"
};fi