从Python脚本中执行Linux命令
我有一台Ubuntu机器正在运行Kubernetes群集。我基本上是在尝试从Python3脚本的后台运行Kubernetes命令,但它不起作用,请提供帮助...
以下是较大脚本的代码的一部分,我使用格式的字符串创建CMD4,然后将CMD4传递给OS。系统为OS.System(CMD4)。但是,一旦我执行脚本,它就会开始在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
============================================
-n core {podname} -container = {containername}&gt; {podname} _ {containername} _log&lt;/dev/dev/dev/null&gt&gt;/dev/dev/dev/dev/dev
; O/P
UpdatedReplicas:1
Deployment.Apps/Core-PCF已配置 NOHUP:忽略输入和将输出附加到'nohup.out'
I have an ubuntu machine where kubernetes cluster is running. I am basically trying to run a Kubernetes command in background from python3 script but it's not working, please help...
Below is a part of code from a larger script, I am creating cmd4 using a formatted string and then passing cmd4 to os.system as os.system(cmd4). But as soon as I execute the script it starts showing logs in cmdline. I tried this using nohup as well as mentioned below but it starts populating logs in 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
=================================
tried with nohup as:
cmd4 = f"nohup kubectl logs -f -n core {podname} --container={containername} > {podname}_{containername}_log </dev/null &>/dev/null &&"
But it gives this o/p
updatedReplicas: 1
deployment.apps/core-pcf configured
nohup: ignoring input and appending output to 'nohup.out'
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过使用子过程库?
我认为您脚本的解决方案将是以下内容。
Have you tried using the subprocess library?
I think the solution for your script will be the following.