29

Through Fabric, I am trying to start a celerycam process using the below nohup command. Unfortunately, nothing is happening. Manually using the same command, I could start the process but not through Fabric. Any advice on how can I solve this?

def start_celerycam():
    '''Start celerycam daemon'''
    with cd(env.project_dir):
        virtualenv('nohup bash -c "python manage.py celerycam --logfile=%scelerycam.log --pidfile=%scelerycam.pid &> %scelerycam.nohup &> %scelerycam.err" &' % (env.celery_log_dir,env.celery_log_dir,env.celery_log_dir,env.celery_log_dir))

        
Milan
  • 1,743
  • 2
  • 13
  • 36
Mo J. Mughrabi
  • 6,747
  • 16
  • 85
  • 143

10 Answers10

32

I'm using Erich Heine's suggestion to use 'dtach' and it's working pretty well for me:

def runbg(cmd, sockname="dtach"):
    return run('dtach -n `mktemp -u /tmp/%s.XXXX` %s' % (sockname, cmd))

This was found here.

andilabs
  • 22,159
  • 14
  • 114
  • 151
danodonovan
  • 19,636
  • 10
  • 70
  • 78
21

As I have experimented, the solution is a combination of two factors:

  • run process as a daemon: nohup ./command &> /dev/null &
  • use pty=False for fabric run

So, your function should look like this:

def background_run(command):
    command = 'nohup %s &> /dev/null &' % command
    run(command, pty=False)

And you can launch it with:

execute(background_run, your_command)
Marius Cotofana
  • 1,149
  • 1
  • 11
  • 14
8

This is an instance of this issue. Background processes will be killed when the command ends. Unfortunately on CentOS 6 doesn't support pty-less sudo commands.

The final entry in the issue mentions using sudo('set -m; service servicename start'). This turns on Job Control and therefore background processes are put in their own process group. As a result they are not terminated when the command ends.

For even more information see this link.

Community
  • 1
  • 1
Tim Ludwinski
  • 2,704
  • 30
  • 34
  • +1: Thanks! The `pty=false` suggestion wasn't working for me on RHEL (because sudo required TTY), but prefixing `set -m;` worked great! Could we get more information on what the ramifications of `set -m;` are, though? What exactly is that doing / how is this solution working? Is there something I should be wary of? Is there some cleanup I should perform later on? – ArtOfWarfare May 30 '14 at 00:18
  • For me, both `run("nohup %s >& /dev/null < dev/null &" % cmd, pty=False)` as well as `run ("set -m; nohup %s &" % cmd)` worked for me. – haridsv Sep 06 '16 at 09:37
6

you just need to run

run("(nohup yourcommand >& /dev/null < /dev/null &) && sleep 1")
idaren
  • 59
  • 1
  • 4
  • 3
    Wow, how did you find this? A slightly simpler version would also be `(nohup yourcommand &> /dev/null < /dev/null &) && /bin/true` – Adam Terrey Jul 03 '17 at 02:01
4

DTACH is the way to go. It's a software you need to install like a lite version of screen. This is a better version of the "dtach"-method found above, it will install dtach if necessary. It's to be found here where you can also learn how to get the output of the process which is running in the background:

from fabric.api import run
from fabric.api import sudo
from fabric.contrib.files import exists


def run_bg(cmd, before=None, sockname="dtach", use_sudo=False):
    """Run a command in the background using dtach

    :param cmd: The command to run
    :param output_file: The file to send all of the output to.
    :param before: The command to run before the dtach. E.g. exporting
                   environment variable
    :param sockname: The socket name to use for the temp file
    :param use_sudo: Whether or not to use sudo
    """
    if not exists("/usr/bin/dtach"):
        sudo("apt-get install dtach")
    if before:
        cmd = "{}; dtach -n `mktemp -u /tmp/{}.XXXX` {}".format(
            before, sockname, cmd)
    else:
        cmd = "dtach -n `mktemp -u /tmp/{}.XXXX` {}".format(sockname, cmd)
    if use_sudo:
        return sudo(cmd)
    else:
        return run(cmd)

May this help you, like it helped me to run omxplayer via fabric on a remote rasberry pi!

leosok
  • 302
  • 1
  • 5
  • 15
  • 1
    Did you actually every get it to pump the results to an output_file? Your code doesn't seem to actually use that parameter. I'm trying to understand how I can log events? – jimmyb Feb 10 '15 at 16:51
  • 1
    Hi, I'm looking for a way to get the output logs. Did you guys managed to find any solution to store the logs to an output file? – Sudhakar Reddy Apr 16 '18 at 12:59
2

You can use :

run('nohup /home/ubuntu/spider/bin/python3 /home/ubuntu/spider/Desktop/baidu_index/baidu_index.py > /home/ubuntu/spider/Desktop/baidu_index/baidu_index.py.log 2>&1 &', pty=False)
g00glen00b
  • 41,995
  • 13
  • 95
  • 133
xpepsi
  • 21
  • 1
2

nohup did not work for me and I did not have tmux or dtach installed on all the boxes I wanted to use this on so I ended up using screen like so:

run("screen -d -m bash -c '{}'".format(command), pty=False)

This tells screen to start a bash shell in a detached terminal that runs your command

theannouncer
  • 1,148
  • 16
  • 28
1

This worked for me:

sudo('python %s/manage.py celerycam --detach --pidfile=celerycam.pid' % siteDir)

Edit: I had to make sure the pid file was removed first so this was the full code:

# Create new celerycam
sudo('rm celerycam.pid', warn_only=True)
sudo('python %s/manage.py celerycam --detach --pidfile=celerycam.pid' % siteDir)
Chad Vernon
  • 593
  • 4
  • 12
1

You could be running into this issue

Try adding 'pty=False' to the sudo command (I assume virtualenv is calling sudo or run somewhere?)

Community
  • 1
  • 1
rupello
  • 8,361
  • 2
  • 37
  • 34
0

I was able to circumvent this issue by running nohup ... & over ssh in a separate local shell script. In fabfile.py:

@task
def startup():
    local('./do-stuff-in-background.sh {0}'.format(env.host))

and in do-stuff-in-background.sh:

#!/bin/sh

set -e
set -o nounset

HOST=$1

ssh $HOST -T << HERE
   nohup df -h 1>>~/df.log 2>>~/df.err &
HERE

Of course, you could also pass in the command and standard output / error log files as arguments to make this script more generally useful.

(In my case, I didn't have admin rights to install dtach, and neither screen -d -m nor pty=False / sleep 1 worked properly for me. YMMV, especially as I have no idea why this works...)

candu
  • 2,827
  • 23
  • 18