Simple, Pythonic remote execution and deployment. Welcome to Fabric! — Fabric documentation

Putting command in the background with Fabric does not work on some hosts

For testing purposes, I am running the following command, with plain ssh command line tool:

ssh user@host "nohup sleep 100 >> /tmp/xxx 2>&1 < /dev/null &"

This is working as expected, in all my hosts: a sleep process is created in the background, and the ssh finishes immediately.

I am trying to implement this functionality in python using Fabric. I end up doing a run call. This is what the Fabric logging is reporting:

[user@host] run: nohup sleep 100 >> /tmp/xxx 2>&1 < /dev/null &

Which is exactly what I expect. But if I check the processes which are running in my host, sleep 100 is not one of them. Worse yet: the problem happens only on some of my hosts.

I also added some more info to show what process has been created, by appending a "\necho $!" to the command to be run by Fabric. This is what was reported:

[user@host] run: nohup sleep 100 >> /tmp/xxx 2>&1 < /dev/null &
echo $!
[user@host] out: 30935

I am running out of ideas on how to debug this, since Fabric is reporting that the process has been created, but I see no process running in the other end. The syslog reports that an ssh session is being opened and closed:

Dec  6 09:12:09 host sshd[2835]: Accepted publickey for user from port 37732 ssh2
Dec  6 09:12:09 host sshd[2838]: pam_unix(sshd:session): session opened for user user by (uid=0)
Dec  6 09:12:10 host sshd[2838]: pam_unix(sshd:session): session closed for user user

Could I somehow increase the amount of logging that the ssh daemon is producing, so that I can see at least what command is being requested via ssh?

I know that Fabric has some issues with running commands in the background, but that does not seem to be my problem. Could there be other issues with Fabric / ssh / background processes?


I have installed dtach on all my systems. The version packaged in Ubuntu 8.04 is too old, and does not allow calling dtach -n over ssh (problems with terminal), so I had to download and compile the dtach sources. After doing that, I was able to run my command like this, with Fabric:

[user@host] run: dtach -n /tmp/Y sleep 100 >> /tmp/xxx 2>&1

This is working fine in all hosts. But this does not fit my scenario, because:

  • dtach creates two processes: one for dtach itself, another for the process being run.
  • I can not get the pid of the process being started

Source: (StackOverflow)

How to get Fabric to automatically (instead of user-interactively) interact with shell commands? Combine with pexpect?

Seeking means to get Fabric to automatically (instead of user-interactively) interact with shell commands (and not just requests for passwords, but also requested user input when no "stdin/interactive override" like apt-get install -y is available).

This question along with these Fabric docs suggest that Fabric can only "push the interactivity" back to the human user that's running the Fabric program. Seeking to instead fully automate without any human presence. Don't yet have a "real," current problem to solve, just preparing for possible, future obstacle.

Possibly useful to combine with pexpect (or similar, alternative mechanism) if Fabric can't exclusively handle all stdin/prompts automatically? Hoping it doesn't need to be an "either/or" kind of thing. Why not leverage both (pexpect and Fabric) where appropriate, if applicable, in same program/automation?

Source: (StackOverflow)

How do I create a postgresql user with fabric

I want to create a database user for my setup fabric script but createuser has interactive password entering and seams not to like fabric.

Source: (StackOverflow)

How to discover current role in Python Fabric

This is a very Fabric specific question, but more experienced python hackers might be able to answer this, even if they don't know Fabric.

I am trying to specify different behaviour in a command depending on which role it is running for, i.e.:

def restart():
    if (SERVERTYPE == "APACHE"):
        sudo("apache2ctl graceful",pty=True)
    elif (SERVERTYPE == "APE"):
        sudo("supervisorctl reload",pty=True)

I was hacking this with functions like this one:

def apache():
    global SERVERTYPE
    env.hosts = ['xxx.xxx.com']

But that is obviously not very elegant and I just discovered roles, so my question is:

How do I figure out which role a current instance belongs to?

env.roledefs = {
    'apache': ['xxx.xxx.com'],
    'APE': ['yyy.xxx.com'],


Source: (StackOverflow)

Does python fabric support dynamic set env.hosts?

I want to change the env.hosts dynamically because sometimes I want to deploy to one machine first, check if ok then deploy to many machines. Currently I need to set env.hosts first, how could I set the env.hosts in a method and not in global at script start?

Source: (StackOverflow)

Get the current value of env.hosts list with Python Fabric Library

I've got this code (foo and bar are local servers):

env.hosts = ['foo', 'bar']

def mytask():

Which, of course prints foo every iteration.

As you probably know, Fabric iterates through the env.hosts list and executes mytask() on each of them this way:

fab mytask


task is executed on foo
task is executed on bar

I'm looking for a way to get the current host in every iteration.


Source: (StackOverflow)

fabric appears to start apache2 but doesn't

I'm using fabric to remotely start a micro aws server, install git and a git repository, adjust apache config and then restart the server.

If at any point, from the fabfile I issue either

sudo('service apache2 restart') or run('sudo service apache2 restart') or a stop and then a start, the command apparently runs, I get the response indicating apache has started, for example

[ec2-184-73-1-113.compute-1.amazonaws.com] sudo: service apache2 start
[ec2-184-73-1-113.compute-1.amazonaws.com] out:  * Starting web server apache2
[ec2-184-73-1-113.compute-1.amazonaws.com] out:    ...done.
[ec2-184-73-1-113.compute-1.amazonaws.com] out: 

However, if I try to connect, the connection is refused and if I ssh into the server and run sudo service apache2 status it says that "Apache is NOT running"

Whilst sshed in, if run sudo service apache start, the server is started and I can connect. Has anyone else experienced this? Or does anyone have any tips as to where I could look, in log files etc to work out what has happened. There is nothing in apache2/error.log, syslog or auth.log.

It's not that big a deal, I can work round it. I just don't like such silent failures.

Source: (StackOverflow)

How to ForwardAgent yes using fabric?

I am successfully run()ning commands on remote server with my private key pair.

However, I'd like to do git clone ssh://private/repo on remote server using my local key (or using local ssh agent I'm in).

How to do it using fabric?

Source: (StackOverflow)

Install Python Fabric on Windows [closed]

How to get a working Python Fabric installation on Windows?

Source: (StackOverflow)

Fabric's cd context manager does not work

I have set up my development environment on a new PC and seems I am having strange error with Fabric. Its 'cd' context manager seems does not change the current directory, and thus a lot of my commands don't work. I have written the test and it showed me results I have not expected to get:

from __future__ import with_statement
from fabric.api import local, run, cd

def xxx():
    with cd("src"):

Here are the results after running fab xxx:

[localhost] local: pwd

But instead of /home/pioneer/workspace/myproject there should be /home/pioneer/workspace/myproject/src, I think.

Source: (StackOverflow)

Python Fabric: How to answer to keyboard input?

I would like to automate the response for some question prompted by some programs, like mysql prompting for a password, or apt asking for a 'yes' or ... when I want to rebuild my haystack index with a ./manage.py rebuild_index.

For MySQL, I can use the --password= switch, and I'm sure that apt has a 'quiet' like option. But how can I pass the response to other programs ?

Source: (StackOverflow)

fabric password

Every time fabric runs, it asks for root password, can it be sent along same for automated proposes.

fab staging test

Source: (StackOverflow)

Connecting to EC2 using keypair (.pem file) via Fabric

Anyone has any Fabric recipe that shows how to connect to EC2 using the pem file?

I tried writing it with this manner: Python Fabric run command returns "binascii.Error: Incorrect padding"

But I'm faced with some encoding issue, when I execute the run() function.

Source: (StackOverflow)

Python 3 support for fabric

Does fabric (http://docs.fabfile.org/en/1.7/) support Python 3 yet. As per Python 3 Wall of Superpowers it does not yet. If not what is the best alternative if using Django 1.5 with Python 3.

Source: (StackOverflow)

Best way to add an environment variable in fabric?

I would like to pass a few values from fabric into the remote environment, and I'm not seeing a great way to do it. The best I've come up with so far is:

with prefix('export FOO=BAR'):
    run('env | grep BAR')

This does seem to work, but it seems like a bit of a hack.

I looked in the GIT repository and it looks like this is issue #263.

Source: (StackOverflow)