如何使用python变量作为参数从python脚本调用bash脚本并设置pid

问题描述 投票:0回答:1

我有以下代码:

port_temp = 8080
process_getpodsnamesipport = subprocess.call(['bash','getpodsnamesipport.sh', port_temp],preexec_fn=os.setsid)

我想做类似的事情:

os.killpg(os.getpgid(process_getpodsnamesipport.pid), signal.SIGKILL)

杀死进程,但是只有subprocess.Popen才有可能。

os.killpg(os.getpgid(process_getpodsnamesipport.pid), signal.SIGKILL)
AttributeError: 'int' object has no attribute 'pid'
python bash
1个回答
0
投票

我会使用Popen并尝试如下操作:

import subprocess
import os
import signal

port_temp = 8080
cmd = "bash getpodsnamesipport.sh {}".format(port_temp)

process_getpodsnamesipport = subprocess.Popen(cmd.split(), stdout=subprocess.PIPE, stderr=subprocess.PIPE, shell=False)
std_out, std_err = process_getpodsnamesipport.communicate()

print(process_getpodsnamesipport.pid)
os.killpg(process_getpodsnamesipport.pid, signal.SIGKILL)
© www.soinside.com 2019 - 2024. All rights reserved.