start a background process with nohup using fabric

后端 未结 10 2077
广开言路
广开言路 2020-12-04 22:31

am trying to start a celerycam process using fabric using the below nohup command. Unfortunately, nothing is happening, manually using the same command i could start the pro

相关标签:
10条回答
  • 2020-12-04 23:03

    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!

    0 讨论(0)
  • 2020-12-04 23:06

    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 讨论(0)
  • 2020-12-04 23:06

    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

    0 讨论(0)
  • 2020-12-04 23:09

    you just need to run

    run("(nohup yourcommand >& /dev/null < /dev/null &) && sleep 1")
    
    0 讨论(0)
提交回复
热议问题