使用ps命令从python ssh到linux服务器不列出PID

2024-05-21 11:29:18 发布

您现在位置:Python中文网/ 问答频道 /正文

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

  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未列出

我不知道为什么会这样

谢谢 苏拉吉特


Tags: 代码import命令服务器脚本linux手动proc