SSH remote command execute stays without disconnect when finished execute?

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?

5 Answers

Try redirecting stdin, stdout, and stderr to /dev/null. For an explanation, see .

0

Try using "here document"

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

That should work.

1

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'

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'
2

using option -t (terminal) in ssh command solves the problem too (better than redirecting stdout if command output is something usefull that we want to see):

ssh -t remoteip command 

Your Answer

Sign up or log in

Sign up using Google Sign up using Facebook Sign up using Email and Password

Post as a guest

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

You Might Also Like