Skip to content

Fabric: running programs in background using tmux

April 24, 2014

Sometimes there is a situation when you need to run daemon process in background using fabric, but running daemons with &? makes fabric hang.

Let’s see to fabric faq:

Fabric executes a shell on the remote end for each invocation of run, background a process via the shell will not work as expexted. Backgrounded processes may still prevent the calling shell from exiting until they stop running, and this in turn prevents Fabric from continuing on with its own execution…. Use tmux, screen or dtach to fully detach the process from the running shell; these tools have the benefit of allowing you to reattach to the process later on if needed (though they are more ad-hoc than supervisord-like tools)….

So to solve bacgrounding problem we need to  use tmux. Here is an example how to implement it:

tmux list-session 2>&1 | grep -q "^$sess" || tmux new-session -s $sess -d
tmux list-window -t $sess 2>&1 | grep -q ": $wn \[" || tmux new-window -t $sess -n $wn
tmux send-keys -t $sess:$wn "fab fabric_task:param=value,param1=value2" Enter

To kill the process created in the way describes above it is usefull to use the following command:

sudo kill $(pgrep -f process_name | grep -v ^$$\$)

Here also some additional information about tmux usage (useful for debugging):


 tmux a 

Attach to named:

 tmux a -t sessname 

List sessions:

 tmux ls 

Kill session:

 tmux kill-session -t sessname 

I hope it will help you in your deployment automation.

From → Automation, CI

  1. In case of testing using Python py.test and Fabric one needs to run py.test with key -s as Fabric uses virtual stout files.
    E.g.: py.test ./path/to/tests -s

  2. Also one can use daemonize tool.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: