I want to run a program on many remote hosts these program may run serveral hours and it needs root privilege I have uploaded the program to those remote hosts I can only ssh into those machines with my username but with my username I can sudo without password

now I want to write a automation script to run them on those hosts

basically I need something like:

for remote_host in host_list:
     run the program on remote_host

and then the script quits. I tried

ssh -t usr@remote_host "sudo /home/usr/program"

but this will block my shell until the program quits so I tried

ssh -t usr@remote_host "sudo /home/usr/program" &

it does'nt work, I use ps -ef to check and find no process

I also tried python paramiko, but failed https://stackoverflow.com/questions/16085502/run-remote-program-with-root-priviledge-using-paramiko-ssh-channel

I also want to log all output from running the program

can anyone give some solutions? thanks



ssh -t usr@remote_host 'sudo nohup bash -c "/home/user/program > /dev/null 2>&1 &"'

The nohup should allow your shell to exit without killing the program that you are running. Drop the -t option if you don't want the nohup.out file to be created.

  • 1
    Why do you need the “bash -c”? Wouldn’t “sudo nohup /home/user/program …” be just as good? – Scott Apr 18 '13 at 18:39
  • @Scott: Good question. Seem to need it in order to keep the & from being associated with the nohup. – Jonathan Ben-Avraham Apr 18 '13 at 18:47
  • If the program intended to run needs tty, e.g., tcpdump, the -t option is necessary, even if you don't want the nohup.out file to be created. – okwap Jan 6 '16 at 8:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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