5

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?

Aaron
  • 6,714
user1289947
  • 127
  • 1
  • 3
  • 9

5 Answers5

3

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.

1

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 
0

Try using "here document"

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

That should work.

YoMismo
  • 101
  • 1
0

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'
Oli
  • 293,335
0

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'
    
Louis Matthijssen
  • 11,885
  • 6
  • 44
  • 50