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?
Super User is a question and answer site for computer enthusiasts and power users. It only takes a minute to sign up.
Sign up to join this communityNo, 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.
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.
pkill
with no signal (= the default
) also doesn't terminate autossh
.
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
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.
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.
$(which autossh)
instead of simply autossh
?
Jun 21, 2019 at 7:36