Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I launch background jobs w/ paramiko?

Here is my scenario: I am trying to automate some tasks using Paramiko. The tasks need to be started in this order (using the notation (host, task)): (A, 1), (B, 2), (C, 2), (A,3), (B,3) -- essentially starting servers and clients for some testing in the correct order. Further, because in the tests networking may get mucked up, and because I need some of the output from the tests, I would like to just redirect output to a file.

In similar scenarios the common response is to use 'screen -m -d' or to use 'nohup'. However with paramiko's exec_cmd, nohup doesn't actually exit. Using:

bash -c -l nohup test_cmd & 

doesnt work either, exec_cmd still blocks to process end.

In the screen case, output redirection doesn't work very well, (actually, doesnt work at all the best I can figure out).

So, after all that explanation, my question is: is there an easy elegant way to detach processes and capture output in such a way as to end paramiko's exec_cmd blocking?

Update

The dtach command works nicely for this!

like image 560
sophacles Avatar asked Feb 04 '10 18:02

sophacles


1 Answers

without using nohup or screen.

def command(channel, cmd):
    channel.exec_command(cmd + ' > /dev/null 2>&1 &')

this says "Redirect STDOUT from cmd into dev/null, then redirect STDERR back into STDOUT, which goes into /dev/null. Then push it into the background."

exec_command wont get hung up on any output (thats not coming), thus it'll return.

like image 73
sbartell Avatar answered Oct 05 '22 22:10

sbartell