Python script to run background services over SSH
Question: Summary: I am ssh'ing to a remote server and executing a fork1.py script over
there which is shown below. But the trouble is that I want the processes to
execute in the background, so that I can start multiple services. I know we
can use nohup, etc. but they are not working. Even when I use a & at the end,
the process starts, but gets killed when the script terminates.

Here is the code:

    
    
    import os
    import sys
    import commands
    from var import key_loc
    import subprocess
    import pipes
    import shlex
    
    def check(status):
        if status != 0:
            print 'Error! '
            quit()
        else:
            print 'Success :) '
        file1=open('/home/modiuser/status.txt','a')
        file1.write("Success :)\n") 
    
    if(sys.argv[1]=="ES"):
        os.chdir('/home/modiuser/elasticsearch-0.90.0/bin/')
        proc1=subprocess.Popen(shlex.split("nohup ./elasticsearch -p /home/modiuser/es.pid"))
    if(sys.argv[1]=="REDIS"):
        os.chdir('/home/modiuser/redis-2.6.13/src')
        proc2=subprocess.Popen(shlex.split("./redis_ss -p /home/modiuser/redis.pid"))
    if(sys.argv[1]=="PARSER"):
        proc3=subprocess.Popen(shlex.split("nohup java -jar logstash-1.1.12-flatjar.jar agent -f parser.conf"))
        file1=open('/home/modiuser/pid.txt','a')
        file1.write("PARSER-"+str(proc3.pid)+"\n")
        file1.write(str(proc3.poll()))
        file1.close()
    if(sys.argv[1]=="SHIPPER_TCP"):
        proc4=subprocess.Popen(shlex.split("nohup java -jar logstash-1.1.12-flatjar.jar agent -f shipper_TCP.conf"))
        file1=open('/home/modiuser/pid.txt','a')
        file1.write("SHIPPER_TCP-"+str(proc4.pid)+"\n")
        file1.close()
    

Where am I going wrong?


Answer: just try with

    
    
    import os
    os.system('python program1.py &')  #this one runs in the background
    os.system('python program2.py')   #this one runs in the foreground
    

