Run Python program remotely in the background

I need to use fabfile to run a program remotely in remote boxes from time to time and get the results. Since the program takes a long time to complete, I want it to work in the background, and therefore I do not need to wait. So I tried os.fork () to make it work. The problem is that when I ssh in a remote box and run the program with os.fork () there, the program may work in the background fine, but when I tried to use fabfile, sudo to run the program remotely, os. fork () cannot work, the program just dies silently. So I switched to Python-daemon to demonize the program. Over time, it worked perfectly. But now that I have started making my program for reading some Python proxies, python-daemon can no longer work.It seems that if you use python-daemon, the dicts shelves cannot be loaded correctly, and I don't know why. Someone has an idea besides os.fork () and Python-daemon, what else can I try to solve my problem?

+3
source share
2 answers

If I understand your question correctly, I think you are doing it too hard. os.fork()Designed for multiprocessing, and not for running the program in the background.

Say, for the sake of discussion, that you wanted to run program.shand collect what it sends to standard output. To do this with a cloth, create locally:

fabfile.py:

from fabric.api import run
def runmyprogram():
    run('./program.sh > output 2> /dev/null < /dev/null &')

Then, locally, run:

fab -H remotebox runmyprogram

The program will be executed remotely, but the fabric will not wait for its completion. You will need to collect the output files later, possibly using scp. "&" makes this run in the background on a remote computer, and output redirection is necessary to avoid a hanging network session .

, . ssh

nohup ./program.sh > output &

, .

, , , cron , , .

, :

fabfile.py:

from fabric.api import run
def runmyprogram():
    run('./program.sh')

:

fab -H remotebox runmyprogram > output &

. , . , , , .

+4

, . Python-daemon . , dicts . shelve dicts , python-daemon , dict . , .

, !

+2
source

All Articles