Autossh init.d script

In this init.d script for autossh in CentOS, I use puppet to replace the param “pAutoSSHParams”, which could be something like this:

“-M 0 -f -N -L 3307:127.0.0.1:3306 -p 22 user@host”

File: /etc/init.d/autossh

Run: service autossh start/stop

#!/bin/sh
#
# autossh – Startup script for autossh

# chkconfig: 2345 20 80
# description: Maintain a persistent SSH tunneling
# processname: autossh
# pidfile: /var/run/autossh.pid

# @since 2011-12-11 08:18:47
# @author Son Nguyen
# Managed by Puppet so do not modify manually

# Source function library
. /etc/rc.d/init.d/functions

prog=”autossh”
autossh=”/usr/bin/autossh”
RETVAL=0

start() {
echo -n $”Starting $prog: ”
# http://www.jbmurphy.com/2011/04/29/autossh-on-centos/
AUTOSSH_PIDFILE=/var/run/autossh.pid
if [ ! -e $AUTOSSH_PIDFILE ]; then
AUTOSSH_PIDFILE=$AUTOSSH_PIDFILE;export AUTOSSH_PIDFILE
daemon $autossh <%= pAutoSSHParams %>
RETVAL=$?
else
RETVAL=1
echo_failure
fi
echo
[ $RETVAL -eq 0 ] && touch /var/lock/subsys/$prog
return $RETVAL
}

stop() {
echo -n $”Stopping $prog: ”
killproc $autossh
RETVAL=$?
echo
[ $RETVAL -eq 0 ] && rm -f /var/lock/subsys/$prog
return $RETVAL
}

case “$1″ in
start)
start
;;
stop)
stop
;;
restart)
stop
start
;;
status)
status $autossh
RETVAL=$?
;;
*)
echo $”Usage: $0 {start|stop|restart|status}”
RETVAL=1
esac

exit $RETVAL


Posted

in

by

Tags:

Comments

2 responses to “Autossh init.d script”

  1. Roderick Avatar
    Roderick

    Hey, thanks for the post. Good to share the knowledge!

    I’m running into the a problem when using daemon $autossh. The tunnel is created. Only one proces is created:

    # ps -ax
    autossh -M 0 -q -N -o ServerAliveInterval 60 -o ServerAliveCountMax 3 -L 11111:localhost:5669 nagios@xxx.xxx.xxx.xxx

    When I do not use daemon but directly the autossh command then the tunnel is created.

    # ps -ax
    autossh -M 0 -q -N -o ServerAliveInterval 60 -o ServerAliveCountMax 3 -L 11111:localhost:5669 nagios@xxx.xxx.xxx.xxx
    /usr/bin/ssh -q -N -o ServerAliveInterval 60 -o ServerAliveCountMax 3 -L 11111:localhost:5669 nagios@xxx.xxx.xxx.xxx

    You had this issue too? Any suggestions?

    Grtz
    Roderick

  2. Matt Reid Avatar

    Roderick – I didn’t have the same issue but you might want to run the script via “bash -x auto-ssh” to enable debug so that you can see what step is failing.

    OP — great script, very useful!

Leave a Reply

Your email address will not be published. Required fields are marked *