2012-01-08 31 views
25

上午嘗試使用以下nohup命令使用結構啓動celerycam進程。不幸的是,沒有任何事情發生,手動使用相同的命令,我可以啓動過程,但不通過結構。任何建議我如何解決這個問題?使用結構nohup啓動後臺進程

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

回答

27

我使用的埃裏希·海涅建議使用「dtach」和它的工作非常適合我:

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

發現這here

+3

我嘗試了一堆不同的方法來做到這一點。這是最後工作的那個。 – 2012-08-15 21:52:25

+0

我試過了,它的工作原理!但是當我在'cmd'中添加nohup時,仍然無法工作,所以我只刪除了nohup和'&'。 – Vimos 2014-01-26 03:39:30

+0

非常感謝,解決了我所有的問題 – Cmag 2015-01-10 05:04:53

1

你可能會運行到this issue

嘗試增加「PTY =假」的sudo命令(我假設的virtualenv呼籲須藤或運行某個地方?)

+0

耶日生病它掛起即使與pty = False – 2012-01-13 10:28:26

+0

沒有希望在這一個面料的方式我猜。 – 2013-04-12 15:35:15

1

這爲我工作:

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

編輯:我必須確保pid文件是先取出所以這是全碼:

# Create new celerycam 
sudo('rm celerycam.pid', warn_only=True) 
sudo('python %s/manage.py celerycam --detach --pidfile=celerycam.pid' % siteDir) 
8

這是this issue一個實例。後臺進程將在命令結束時被終止。不幸的是,CentOS 6不支持pty-less sudo命令。

該問題的最後一項提到使用sudo('set -m; service servicename start')。這會打開作業控制,因此後臺進程將放入其自己的進程組中。因此,當命令結束時它們不會被終止。

欲瞭解更多信息,請參閱this鏈接。

+0

+1:謝謝! 'pty = false'建議在RHEL上不適用於我(因爲sudo需要TTY),但在'set -m;'前加上了''''!但是,我們可以獲得更多關於'set -m;'的後果的信息嗎?究竟是做什麼/這個解決方案如何工作?有什麼我應該警惕的嗎?有沒有我應該稍後執行的一些清理? – ArtOfWarfare 2014-05-30 00:18:59

+0

@ArtOfWarfare,請參閱[此鏈接](http://ss64.com/bash/set.html)或'man 1 set'。 – 2015-10-27 20:31:33

+0

對我來說,無論是'run(「nohup%s>&dev/null haridsv 2016-09-06 09:37:03

3

DTACH是要走的路。這是一個你需要安裝的軟件,像一個精簡版的屏幕。 這是上面找到的「dtach」 - 方法的更好版本,它將在必要時安裝dtach。它被發現here在這裏你還可以瞭解如何獲得它在後臺運行的進程的輸出:

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) 

願這幫助你,就像幫助我通過結構上的遠程樹莓運行omxplayer PI!

+0

你是否真的每次都把結果輸出到output_file?您的代碼似乎並未實際使用該參數。我想了解我如何記錄事件? – jimmyb 2015-02-10 16:51:55

13

正如我已經試驗,該溶液是兩個因素的組合:

  • 運行過程作爲一個守護程序:nohup的。/命令&>的/ dev/null的&
  • 使用PTY =假爲織物運行

所以,你的函數應該是這樣的:

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

而且你可以啓動它:

execute(background_run, your_command) 
+0

爲我工作 (Ubuntu 14.04) – 2016-12-13 20:18:11

+1

'mesg:ttyname失敗:不適當的ioctl爲devic' – andi 2018-01-07 00:56:36

0

我能夠繞過這個問題,運行nohup ... &ssh的獨立本地shell腳本中。在fabfile.py

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

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 

當然,你也可以通過在命令和標準輸出/錯誤日誌文件作爲參數,使該腳本更普遍有用。

(在我的情況,我沒有管理員權限來安裝dtach,也不screen -d -m也不pty=False/sleep 1正常工作對我來說,情況因人而異,尤其是當我不知道爲什麼 ...作品)

2

你只需要運行

run("(nohup yourcommand >& /dev/null < /dev/null &) && sleep 1") 
+0

哇,你是怎麼找到這個的?一個稍微簡單的版本也可能是 '(nohup yourcommand&>/dev/null 2017-07-03 02:01:40

1

您可以使用:

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) 
相關問題