Fabric: running programs in background using tmux

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

Continue reading “Fabric: running programs in background using tmux”