简体   繁体   English

如何用bash脚本杀死python脚本

[英]How to kill python script with bash script

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 $1 }')

I try to check the running processes by ps aux | less我尝试通过ps aux | less检查正在运行的进程ps aux | less ps aux | less and found that the running script having command of python test.py ps aux | less和发现具有命令行书python test.py

Please assist, thank you!请帮忙,谢谢!

Use pkill command as使用pkill命令作为

pkill -f test.py

(or) a more fool-proof way using pgrep to 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 

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 pkill or grepping the ps aux can be risky.pkill或 grepping ps aux这样的命令可能有风险。

With the use of bashisms.使用bashisms。

#!/bin/bash

python test.py &
kill $!

$! is the PID of the last process started in background.是在后台启动的最后一个进程的PID。 You can also save it in another variable if you start multiple scripts in the background.如果在后台启动多个脚本,也可以将其保存在另一个变量中。

killall python3

将中断任何和所有正在运行的 python3 脚本。

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM