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))

        

10 Answers 10

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.

3
  • 3
    I tried a bunch of different ways of doing this. This was the one that finally worked. Aug 15, 2012 at 21:52
  • I tried this, it works! But when I add nohup in 'cmd', still not working, so I just removed nohup and '&'.
    – Vimos
    Jan 26, 2014 at 3:39
  • 1
    How can I see the logs of the detached execution. Can it be configured?Or will there be a specific location where I can look for? Apr 16, 2018 at 12:56
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)
1
  • 1
    mesg: ttyname failed: Inappropriate ioctl for devic
    – andilabs
    Jan 7, 2018 at 0:56
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.

2
  • +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? May 30, 2014 at 0: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 6, 2016 at 9:37
6

you just need to run

run("(nohup yourcommand >& /dev/null < /dev/null &) && sleep 1")
1
  • 3
    Wow, how did you find this? A slightly simpler version would also be (nohup yourcommand &> /dev/null < /dev/null &) && /bin/true Jul 3, 2017 at 2: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!

2
  • 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, 2015 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? Apr 16, 2018 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)
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

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?)

1
  • yeah still it hangs even with pty=False Jan 13, 2012 at 10: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)
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...)

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.