Get PID from Paramiko

I cannot find a simple answer for this: I use paramiko to log in and execute several processes remotely, and I need the PID of each process to check them later. There seems to be no function in paramiko to get the PID of the executable command, so I tried using the following:

stdin,stdout,stderr = ssh.exec_command('./someScript.sh &;echo $!;) 

I thought then parsing stdout would return the PID, but that is not the case. I assume I have to run the script in the background in order to have a PID (while it is running). Is there an easier, more obvious way to get the PID?

+6
source share
2 answers

Here you can get the identifier of the remote process:

 def execute(channel, command): command = 'echo $$; exec ' + command stdin, stdout, stderr = channel.exec_command(command) pid = int(stdout.readline()) return pid, stdin, stdout, stderr 
+9
source

I usually use the standard UNIX pidof <command name> when I check the process later. AFAIK there is no easier way.

OK, given your comment, you can solve it by wrapping yours. /someScript.sh in a Python process that uses a subprocess module.

wrapper.py:

 import subprocess import sys proc = subprocess.Popen(sys.argv[1]) print proc.pid proc.wait() #probably 

Then run

 stdin,stdout,stderr = ssh.exec_command('./wrapper.py ./someScript.sh') 

and read the conclusion

0
source

Source: https://habr.com/ru/post/911673/


All Articles