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

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

David Miller

David Miller

Executive Financial & Market Analyst

David Miller brings 15 years of experience in global economics, personal finance strategy, and market dynamics. He specializes in turning complex economic trends into actionable insights for everyday readers.

Share this article
Twitter Facebook Pinterest