1

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?

kirelagin
  • 12,085
  • 1
  • 35
  • 54
user2359303
  • 231
  • 2
  • 6
  • 15
  • You need daemons. This has nothing to do with python or ssh, that's how Linux works. – kirelagin Jun 04 '13 at 08:09
  • take a look at the answers of this http://stackoverflow.com/questions/89228/calling-an-external-command-in-python#2251026. Scroll down a bit. I think this could be what you are looking for. – pypat Jun 04 '13 at 08:11

1 Answers1

-1

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
abhi
  • 372
  • 2
  • 13