从Python脚本中执行Linux命令

发布于 2025-02-08 09:51:46 字数 1046 浏览 1 评论 0原文

我有一台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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

柒夜笙歌凉 2025-02-15 09:51:46

您是否尝试过使用子过程库?

我认为您脚本的解决方案将是以下内容。

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

Have you tried using the subprocess library?

I think the solution for your script will be the following.

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文