16

Now that I have a autossh tunnel, what is correct way to stop it?

There seems to be no easy way to do this. Documentation is unclear on this part.

The easy way seems to be to reboot the machine? Is this correct?

20

No, the proper way to kill autossh is simply to kill the process autossh, nothing else.

The reason is

# file $(which autossh)
/usr/bin/autossh: POSIX shell script, ASCII text executable

that autossh is simply a shell script, not a service. It starts a new program, in its very last line,

exec /usr/lib/autossh/autossh "$@"

again not a service. As for exec (you can double-check it in the wiki of the Bash hackers), it is a shell-builtin command which replaces the current shell with the following command (/usr/lib/autossh/autossh "$@" in this case) without starting a new process. So, the only way to stop autossh is to kill the calling script, for instance

pkill -3 autossh

(thanks to dviljoen for pointing out the importance of using the -3 flag, see below). Incidentally, killing the ssh connection will not work, because the calling command (i.e., the one above) will simply start a new connection as soon as it realizes the old one has been dropped.

  • 1
    Actually you should kill autossh without the -9. If a connection has already been made through the tunnel, it will have spawned a child sshd process to handle it. Killing the parent autossh with -9 will leave the sshd process behind. The tunnel will still operate until the sshd process times out. If you use the default signal (-3) on the kill it will gracefully shutdown and take the spawned sshd process with it. – dviljoen Mar 24 '17 at 14:17
  • hi as the -3 is the default kill signal, you don't actually need to explicitly pass -3, you can omit it – Andrés Alcarraz Aug 1 '18 at 12:03
  • @AndrésAlcarraz You really sure about it? SIGTERM is the default, and that one is 15. SIGQUIT is 3, see superuser.com/questions/352147/what-does-kill-3-mean and en.wikipedia.org/wiki/Kill_(command) -- Also, FWIW, I just checked and -15 doesn't shut it down, so SIGTERM cannot be used. – Daniel F Sep 19 '18 at 20:55
  • And using pkill with no signal (= the default) also doesn't terminate autossh. – Daniel F Sep 19 '18 at 21:01
  • @DanielF you're right, I was confused y the "default signal (-3)" from dvijoen comment – Andrés Alcarraz Sep 19 '18 at 21:03
4

Search for the process:

ps aux | grep ssh

The secon column is the PIDnumber

Kill process by PID :)

kill -9 PIDnumber

Use sudo if you dont have root privileges.

4

run auto ssh with:

AUTOSSH_PIDFILE=/var/run/tunnel.pid autossh

kill it with:

kill pid

BTW

pkill -9 autossh is a wrong

-9 makes sure the process not exiting gracefully, so ssh process is still there when the autossh process is killed

without -9 is still bad, if you have multiple tunnels running, pkill will kill them all

correct way is to set AUTOSSH_PIDFILE env var then kill that pid only

  • Welcome to Super User. It appears your final paragraph is your actual answer, while the remainder of your question is a comment on another answer. Given the Q&A nature of this site, It would helpful to add enough detail to your answer so it can stand on its own. Thanks for contributing and don't forget to check out the tour. – Twisty Impersonator Dec 16 '17 at 2:25
1

I know this has been answered, but contrary to the comments above, using pkill -3 autossh does NOT kill the sshd child processes for me.

I use this function in my .bashrc file.
Bascially, it's like adding a --kill argument to autossh.

  if [ "$1" = "--kill" ]; then
    ps aux | 
    grep -P "(/usr/bin/ssh|/usr/lib/autossh/autossh)\s.*$2" |
    awk '{print $2}' |
    xargs -r kill
  else
    $(which autossh) "$@"
    echo "" # prevents line wrapping when you kill the ssh process
  fi

You can run which ssh and which autossh to check the paths on your system.

As long as the first argument isn't --kill, it just passes the args along to autossh.

This script kills the autossh & ssh instances. This is important if you are using port forwarding because, killing ONLY the autossh instance doesn't kill the tunnel, it just prevents it from reconnecting if/when it finally does disconnect.

You can also specify a search term (host name) to kill only specific tunnels.

autossh --kill dbserver1 kills just the connections to dbserver1
autossh --kill dbserver will kill dbserver1, dbserver2, etc.
autossh --kill dbserver will kill ALL autossh connections

To clarify, it SHOULD kill only the SSH sessions started by autossh.

If you run ps aux | grep ssh while you have both autossh and ssh sessions running, you will see that the ones started by autossh use the full path (/usr/bin/ssh and /usr/lib/autossh/autossh).
This script only matches results to processes started with the speicifc path. I did this because I (and I assume most people) usually type ssh and not the full path, which keeps it from killing my normal ssh sessions.

Hope this help others.

  • any particular reason for $(which autossh) instead of simply autossh? – MestreLion Jun 21 at 7:36

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.