Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to start a background process with nohup using Fabric?

Tags:

python

fabric

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))           
like image 995
Mo J. Mughrabi Avatar asked Jan 08 '12 05:01

Mo J. Mughrabi


2 Answers

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.

like image 157
danodonovan Avatar answered Sep 22 '22 08:09

danodonovan


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) 
like image 43
Marius Cotofana Avatar answered Sep 20 '22 08:09

Marius Cotofana