Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Start a background process on remote machine using python

I am running a python script which involves running a "burnP6" background process on a remote machine.

I tried to use fabric:

import fabric.api
fabric.api.execute(run_burnP6_bg, hosts=[remote_machine])

def run_burnP6_bg():
    fabric.api.run("burnP6 &")

Also I tried using ssh -f with subprocess:

import subprocess

def cpu_load(receiver, load_percent='100', time_seconds='3'):
    if 1<= load_percent and 100 >= load_percent:
        cmd = 'ssh -f xyz@{1} '.format(ip_addr) + "'burnP6 &'"
        subprocess.call(cmd.split(' '))
    elif 0 == load_percent:
        # No load to be added
        pass

But both of them did not work. On running top in the remote server I did not see any burnP6 process.

Is there something I am missing?

like image 892
Akshya11235 Avatar asked Dec 11 '25 10:12

Akshya11235


1 Answers

Login with ssh and start any job process with job&. Login with ssh in a different window and do ps to check for your job: you should see it running. Now logout of your first ssh and check again for your job process. You will notice that it is now gone. This happens because jobs are attached to a terminal by default and are sent a SIGHUP when the terminal is closed.

Now repeat the process with running nohup job& or disown job&. These both prevent the SIGHUP from killing the job process.

To fix your code you can use either of the following:

import fabric.api
fabric.api.execute(run_burnP6_bg, hosts=[remote_machine])

def run_burnP6_bg():
    fabric.api.run("nohup burnP6 &")

or with subprocess

import subprocess
cmd = 'ssh -f xyz@{1} '.format(ip_addr) + "'nohup burnP6 &'"
subprocess.call(cmd,shell=True)

these should prevent your job from dying when the ssh session ends.

like image 68
Andrew Johnson Avatar answered Dec 12 '25 22:12

Andrew Johnson



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!