Search code examples
pythonmultiprocessingblocking

Executing 2 blocking processes and need the control back in the same ssh session


I'm trying to automate the start up procedure for a Linux Agent machine. I need to run two executable files located in two different directories and get the control back to the prompt so as to proceed with some other task like do a grep to check if the two processes are still running. Here are two different ways using Python in which I tried to do this:

Code snippet 1:(By forking a child process)

 import os
 import pdb

    def child():

    cwd = os.getcwd()  
    os.chdir("THoT") 
    os.chdir("TH_Node")  
    print "Executing TH_Node.exe........."
    command = "mono TH_Node.exe"
    os.system(command)
    os._exit(0)

    def parent():

    i = 0
      while i < 1:
        i = i + 1
      newpid = os.fork()
      if newpid == 0:
        child()
      else:
        cwd = os.getcwd()
        os.chdir("THoT")
        os.chdir("TH_Protocol")
        print "Executing TH_Protocol.exe........."
        command1 = "mono TH_Protocol.exe"
       os.system(command1)          

    parent()

Code snippet 2:(Using multiprocessing)

 import multiprocessing
 import time
 import sys
 import os
 import pdb

def TH_Protocol():

 os.chdir("THoT")
 os.chdir("TH_Protocol")
 command = "mono TH_Protocol.exe"
 os.system(command)


def TH_Node():

 os.chdir("THoT") 
 os.chdir("TH_Node")  
 command1 = "mono TH_Node.exe"
 os.system(command1)


if __name__ == '__main__':
      d = multiprocessing.Process(name='TH_Protocol', target=TH_Protocol)
      d.TH_Protocol = True

      n = multiprocessing.Process(name='TH_Node', target=TH_Node)
      n.TH_Protocol = False

      d.start()
      n.start()
      d.join(1)
      n.join()

The problem is although I get both the processes TH_Protocol.exe and TH_Node.exe to run, I need to ssh to another session to run a grep command to check if the two processes are running. I need to get the control back in the same session as the session in which I run my python script. I tried to use the subprocess.Popen as well, but I face the same problem. Is there any way I can solve this issue?


Solution

  • If you just want to run this script in the background, and get control of your ssh session back while it's running… that has nothing to do with Python, or ssh, it's basic shell job control.

    For example, assuming your shell on the remote machine is sh/bash/similar, instead of this:

    remote_machine$ python script.py
    

    … do this:

    remote_machine$ python script.py &
    [1] 55341
    remote_machine$
    

    Now you've got the prompt back. You can interact with the main interpreter process as %1 or PID 55341. After it finally finishes, the next prompt you get will show something like this:

    [1]+ Done python
    

    You can't directly interact with the two child processes this way. You can always grep for them if you want, or search for child processes of PID 55341… but you might find your life easier if you had the child processes do something like print('TH_Protocol on {}'.format(os.getpid())) as soon as they start up, so you don't have to do that.