start a background process with nohup using fabric

后端 未结 10 2076
广开言路
广开言路 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 22:45

    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.

    0 讨论(0)
  • 2020-12-04 22:47

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

    0 讨论(0)
  • 2020-12-04 22:50

    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.

    0 讨论(0)
  • 2020-12-04 22:50

    You could be running into this issue

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

    0 讨论(0)
  • 2020-12-04 22:58

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

    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)
    
    0 讨论(0)
提交回复
热议问题