bash 如何用bash脚本杀死python脚本

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

How to kill python script with bash script

pythonlinuxbashraspberry-pi3

提问by Jeff Pang

I run a bash script with which start a python script to run in background

我运行一个 bash 脚本,用它启动一个 python 脚本在后台运行

#!/bin/bash

python test.py &

So how i can i kill the script with bash script also?

那么我怎样才能用 bash 脚本杀死脚本呢?

I used the following command to kill but output no process found

我使用以下命令杀死但输出 no process found

killall $(ps aux | grep test.py | grep -v grep | awk '{ print  }')

I try to check the running processes by ps aux | lessand found that the running script having command of python test.py

我尝试检查正在运行的进程ps aux | less,发现运行脚本具有以下命令python test.py

Please assist, thank you!

请帮忙,谢谢!

回答by Inian

Use pkillcommand as

使用pkill命令作为

pkill -f test.py

(or) a more fool-proof way using pgrepto search for the actual process-id

(或)pgrep用于搜索实际进程 ID 的更简单的方法

kill $(pgrep -f 'python test.py')

Or if more than one instance of the running program is identified and all of them needs to be killed, use killall(1)on Linux and BSD

或者,如果识别出多个正在运行的程序实例并且需要将它们全部杀死,请在 Linux 和 BSD 上使用killall(1)

killall test.py 

回答by Riccardo Petraglia

You can use the !to get the PID of the last command.

您可以使用!来获取最后一个命令的 PID。

I would suggest something similar to the following, that also check if the process you want to run is already running:

我会建议类似于以下内容,还可以检查您要运行的进程是否已经在运行:

#!/bin/bash

if [[ ! -e /tmp/test.py.pid ]]; then   # Check if the file already exists
    python test.py &                   #+and if so do not run another process.
    echo $! > /tmp/test.py.pid
else
    echo -n "ERROR: The process is already running with pid "
    cat /tmp/test.py.pid
    echo
fi

Then, when you want to kill it:

然后,当你想杀死它时:

#!/bin/bash

if [[ -e /tmp/test.py.pid ]]; then   # If the file do not exists, then the
    kill `cat /tmp/test.py.pid`      #+the process is not running. Useless
    rm /tmp/test.py.pid              #+trying to kill it.
else
    echo "test.py is not running"
fi

Of course if the killing must take place some time after the command has been launched, you can put everything in the same script:

当然,如果必须在命令启动后的一段时间内进行杀戮,您可以将所有内容放在同一个脚本中:

#!/bin/bash

python test.py &                    # This does not check if the command
echo $! > /tmp/test.py.pid          #+has already been executed. But,
                                    #+would have problems if more than 1
sleep(<number_of_seconds_to_wait>)  #+have been started since the pid file would.
                                    #+be overwritten.
if [[ -e /tmp/test.py.pid ]]; then
    kill `cat /tmp/test.py.pid`
else
    echo "test.py is not running"
fi

If you want to be able to run more command with the same name simultaneously and be able to kill them selectively, a small edit of the script is needed. Tell me, I will try to help you!

如果您希望能够同时运行更多具有相同名称的命令并能够有选择地杀死它们,则需要对脚本进行少量编辑。告诉我,我会尽力帮助你!

With something like this you are sure you are killing what you want to kill. Commands like pkillor grepping the ps auxcan be risky.

有了这样的东西,你确定你正在杀死你想杀死的东西。像pkill或 grep 之类的命令ps aux可能有风险。

回答by Afsal Salim

ps -ef | grep python

it will return the "pid" then kill the process by

它将返回“pid”,然后通过以下方式终止进程

sudo kill -9 pid

eg output of ps command: user 13035 4729 0 13:44 pts/10 00:00:00 python (here 13035 is pid)

例如 ps 命令的输出: user 13035 4729 0 13:44 pts/10 00:00:00 python (这里 13035 是 pid)

回答by Moritz Sauter

With the use of bashisms.

使用bashisms。

#!/bin/bash

python test.py &
kill $!

$!is the PID of the last process started in background. You can also save it in another variable if you start multiple scripts in the background.

$!是在后台启动的最后一个进程的PID。如果在后台启动多个脚本,也可以将其保存在另一个变量中。