Ubuntu: SSH remote command execute stays without disconnect when finished execute?



Question:

I am trying to build a script/command that will restart our Asterisk/Elastix service on our PBX. The script is basically using simple command with auto log-in public key that run from Ubuntu 14.04LTS:

The command script is:

ssh user@iphost 'service asterisk restart'  

But after the command finished and service restarted, the ssh stays and only after ctrlc its breaks out.

My question, is how can I terminate the session after the command completes?


Solution:1

Try redirecting stdin, stdout, and stderr to /dev/null. For an explanation, see https://serverfault.com/questions/36419/using-ssh-to-remotely-start-a-process.


Solution:2

Try using "here document"

ssh user@iphos << endexe     service asterisk restart     exit  endexe  

That should work.


Solution:3

You could just background it as soon as it runs:

ssh user@iphost 'service asterisk restart &'  

I've got a similar issue with an xinit-based upstart service that hogs the stage and needs manually exiting (doesn't harm the Upstart job), but yeah, wang a & on the end and that should force it into the background.

If that gives you gyp, there are a number of other options like:

ssh user@iphost 'setsid service asterisk restart'  


Solution:4

You can use one of the following commands instead:

  • Restarts the service and then exits:

    ssh user@iphost 'service asterisk restart; exit'  
  • Restarts the service and then exits on success:

    ssh root@iphost 'service asterisk restart && exit'  

Note:If u also have question or solution just comment us below or mail us on toontricks1994@gmail.com
Previous
Next Post »