11

I am trying to execute a script on a remote host using a detached screen session. I tried out the example Fabric gives and unfortunately couldn't get it to work.

from fabric.api import run

def yes():
    run('screen -d -m "yes"')

Executing fab yes on my local machine correctly connects it to the remote host and says the command has been run, however nothing is executed on the remote host. Trying screen -d -m "yes" on either machine works as expected.

If anyone could point out what I'm doing wrong I'd greatly appreciate it. Also, on a side note, why are there quotes around the yes in the command? Would it work without the quotes? Thanks!

3 Answers 3

17

run('screen -d -m yes; sleep 1') works.

Not sure if Fabric or screen are to blame for this behaviour though.

4

Although AVB answer is perfect I'll add a small tip which may help someone like me. If you want to run more than one command put them to a executable file.

This will not work:

run('screen -d -m "./ENV/bin/activate; python run.py; sleep 1"')

So create a run.sh file:

#!/bin/bash
source ENV/bin/activate
python run.py

And use it like run('screen -d -m ./run.sh; sleep 1')

1
  • 9
    why is the "sleep 1;" there?
    – arikg
    Nov 28, 2013 at 9:19
0

Use it like this:

run('sudo screen -d -m python app.py && sleep 1', pty=True) 

screen -d -m 

Start a session that starts in disconnected mode

1
  • 1
    Welcome to Stackoverflow. Please explain your answer so others can understand easily.
    – octobus
    Dec 7, 2019 at 14:56

Your Answer

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

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