2

I want to run some script through ssh and detach it, so it run even after ssh session is closed. I need to do this with sudo.

I can run some script but I am not able to detach it. I tried following commands:

ssh 10.0.139.120 -t "sudo -b nohup some_script"

and

ssh 10.0.139.120 -t "sudo nohup some_script &"

Neither of above worked. Every time I get communicate "Connection to 10.0.139.171 closed." and no process is running on server.

I have also tried to do it with screen:

ssh 10.0.139.120 -t "sudo screen -S script -md some_script"

It did not work either, even though command works when I enter it on server locally. How can I do it then?

sbagin13
  • 153

2 Answers2

3

I did small survey and found this similar question on Stack Overflow. At all you must add path to the script or must go inside the directory, where the script is located:

ssh remote-host -t "sudo -b sh -c 'nohup /path/some_script > /dev/null 2>&1 &'"
ssh remote-host -t "sudo -b sh -c 'cd /path; nohup ./some_script > /dev/null 2>&1 &'"

Without sh -c or bash -c the ampersand & at the end breaks the command in some way.


I couldn't manage to use disown for this purpose, but setsid works well:

 ssh remote-host -t 'sudo -b setsid /path/some_script'
pa4080
  • 30,621
0

You can try using disown to avoid the process being killed after you close you ssh session.