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
Leave a Reply