![](/img/trans.png)
[英]Python: Executing the windows command in windows command prompt from python script
[英]Executing Linux command in background from a python script
我有一台運行 kubernetes 集群的 ubuntu 機器。 我基本上是在嘗試從 python3 腳本在后台運行 Kubernetes 命令,但它不起作用,請幫助...
下面是來自較大腳本的部分代碼,我正在使用格式化字符串創建 cmd4,然后將 cmd4 作為 os.system(cmd4) 傳遞給 os.system。 但是,一旦我執行腳本,它就會開始在 cmdline 中顯示日志。 我嘗試使用 nohup 以及下面提到的方法,但它開始在 nohup.out 中填充日志。
for podname in pod_names:
if "smf" in podname or "pcf" in podname or "udm" in podname:
containername = 'worker'
else:
containername = 'cppe'
**cmd4 = f"kubectl logs -f -n core {podname} --container={containername} > {podname}_{containername}_log </dev/null &>/dev/null &&"**
cmd5 = f"echo $! >> pid.txt"
os.system(cmd4)
os.system(cmd5)
pid_file = open('pid.txt', 'a+')
pid_file.write("\n")
pid_file.close
==================================
嘗試使用 nohup 為:
cmd4 = f"nohup kubectl logs -f -n core {podname} --container={containername} > {podname}_{containername}_log </dev/null &>/dev/null &&"
但它給出了這個 o/p
更新副本:1
deployment.apps/core-pcf 配置 nohup:忽略輸入並將輸出附加到“nohup.out”
您是否嘗試過使用子流程庫?
我認為您的腳本的解決方案如下。
import subprocess
for podname in pod_names:
if "smf" in podname or "pcf" in podname or "udm" in podname:
containername = 'worker'
else:
containername = 'cppe'
**cmd4 = f"kubectl logs -f -n core {podname} --container={containername} > {podname}_{containername}_log </dev/null &>/dev/null &&"**
cmd5 = f"echo $! >> pid.txt"
cmd4_bash = subprocess.Popen(cmd4.split(), stdout=subprocess.PIPE)
cmd5_bash = subprocess.Popen(cmd5.split(), stdout=subprocess.PIPE)
pid_file = open('pid.txt', 'a+')
pid_file.write("\n")
pid_file.close
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.