【问题标题】:ssh to a linux server not listing the PID from python using ps commandssh 到没有使用 ps 命令从 python 列出 PID 的 linux 服务器
【发布时间】:2020-08-27 05:32:04
【问题描述】:

我正在尝试使用 2 种方法在 linux 服务器中执行以下代码,

  1. 手动登录到 linux 服务器,运行脚本
  2. 从本地(使用 ssh 命令)

请查找代码:

from subprocess import Popen, PIPE
import os

print os.getpid()

def proc_func():
    proc = Popen(['ps', '-eo', 'pid,lstart', '-a', '-f'], stdout=PIPE, stderr=PIPE)
    for line in proc.stdout:
        print line.strip()

proc_func()

所以当我手动登录到 linux 服务器并运行脚本时,

29706
PID                  STARTED
28811 Wed Oct  3 06:23:51 2018
13474 Wed Oct  3 06:30:24 2018
13484 Wed Oct  3 06:30:29 2018
13485 Wed Oct  3 06:30:29 2018
29706 Wed Oct  3 07:15:19 2018
29708 Wed Oct  3 07:15:19 2018
12030 Wed Oct  3 06:42:11 2018
21910 Wed Oct  3 06:58:40 2018
3445 Fri Jul 20 02:35:07 2018
3444 Fri Jul 20 02:35:07 2018

在这里您可以看到PID29706 已列出

但是当我尝试从本地运行以下命令时

ssh -T <user_id>@<linux_server_address> "python /path/to/the/code/in/linux/server/proc.py"

我得到以下输出:

13175
PID                  STARTED
28811 Wed Oct  3 06:23:51 2018
13474 Wed Oct  3 06:30:24 2018
13484 Wed Oct  3 06:30:29 2018
13485 Wed Oct  3 06:30:29 2018
12030 Wed Oct  3 06:42:11 2018
21910 Wed Oct  3 06:58:40 2018
3445 Fri Jul 20 02:35:07 2018
3444 Fri Jul 20 02:35:07 2018

在这里你可以看到PID13175 未列出

我不知道为什么会这样。

谢谢 苏拉吉特

【问题讨论】:

  • 你是在远程主机上运行这个吗?你没有得到完整的输出。
  • 我的 linux 服务器是远程的。我正在从我的 mac 运行它(使用 ssh 命令)。我没有得到使用ps 命令的python 脚本生成的pid

标签: python linux ssh


【解决方案1】:

您不要使用-x 标志调用ps-x 标志是列出所有进程所必需的,即使是那些不与ps 命令调用共享终端或根本没有终端的进程。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2023-03-12
    • 1970-01-01
    • 2015-04-01
    • 2013-07-29
    • 1970-01-01
    • 2020-09-22
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多