用于autossh的Bash init脚本

时间:2012-02-15 15:57:28

标签: bash ssh init

我正在尝试创建一个脚本,它将在启动时通过init脚本启动autossh,但我无法让它工作。我希望看到它记录了它的所有动作,因为它没有正常工作,但是这不是那么好的以太。由于我不是bash的专业人士,我希望我的代码不会令人非常尴尬。

#!/bin/sh
#
# by Patrick van der Leer <pat.vdleer@gmail.com>
# released under GPL, version 2 or later

PATH=/sbin:/bin:/usr/sbin:/usr/bin
DAEMON="/usr/bin/autossh"
DESC="Autossh job"
PIDFOLDER="/var/run/autossh"
PIDFOLDERSSH="$PIDFOLDER/ssh"
REMOTE_USER=""
REMOTE_ADDR=""
LOGFILE="/var/log/autossh.log"

if [ ! -d $PIDFOLDER ] ; then
    mkdir -p $PIDFOLDER
fi

if [ ! -d $PIDFOLDERSSH ] ; then
    mkdir -p $PIDFOLDERSSH
fi

test -f $DAEMON || exit 0

. /lib/lsb/init-functions

PIDFILE="$PIDFOLDER/$REMOTE_USER-$REMOTE_ADDR.pid"
PIDFILESSH="$PIDFOLDERSSH/$REMOTE_USER-$REMOTE_ADDR.pid"

is_running() {
    if [ -f $PIDFILE ]; then
        PID=`cat $PIDFILE`
        if [ -n "$PID" ]; then
            return 0
        else
            return 1
        fi
    else
        return 1
    fi
}

start_autossh() {
    if ! is_running; then
        echo "Starting $DESC"
        export AUTOSSH_FIRST_POLL=10
        export AUTOSSH_POLL=60
        export AUTOSSH_PIDFILE=$PIDFILESSH
        start-stop-daemon --start --make-pidfile --pidfile $PIDFILE --exec $DAEMON -- -M 29000 -i /root/.ssh/id_rsa -X -C -R 2222:localhost:22 $REMOTE_USER@$REMOTE_ADDR >> $LOGFILE 2>&1 &
        sleep 1;
        if ! is_running; then
            echo "$DESC: running @ pid $PID"
        else
            echo 'Something went wrong';
        fi
    else
        echo "$DESC: already running (pid $PID)"
    fi
}

stop_autossh() {
    if is_running; then
        echo "Stopping $DESC"
        start-stop-daemon --stop --pidfile $PIDFILE --signal 15
        if [ -f $PIDSSHFILE ]; then
            PIDSSH=`cat $PIDFILESSH`
            kill $PIDSSH
            rm -f $PIDFILESSH
        fi
    else
        echo "$DESC: not running"
    fi
    [ -f $PIDFILE ] && rm -f $PIDFILE
}

case "$1" in
    start)
        start_autossh
    ;;
    stop)
        stop_autossh
    ;;
    force-reload|restart)
        stop_autossh
        start_autossh
    ;;
    status)
        if is_running; then
            echo "$DESC: running (pid $PID)"
            exit 0
        else
            echo "$DESC: not running"
            [ -f $PIDFILE ] && exit 1 || exit 3
        fi
    ;;
    log)
        if [ -f $LOGIFLE ]; then
            tail $LOGFILE
        else
            echo "log file '$LOGFILE' does't exist"
        fi
    ;;
    *)
        echo "Usage: $0 {start|stop|restart|force-reload|status|log}"
        exit 3
    ;;
esac

exit 0

2 个答案:

答案 0 :(得分:2)

如果设置x选项,Shell将在执行它们之前打印所有语句。之一:

  • 将脚本作为/bin/sh -x script运行。
  • #!行更改为#!/bin/sh -x
  • 在脚本开头set -x

显然,第一个将为一次运行设置它,另外两个将为每次运行设置它。


检查start-stop-daemon(8)的手册页,好像你错了。您应该背景start-stop-daemon&,您应该使用start-stop-daemon选项告诉--background后台执行的流程。 --make-pidfile的文档甚至表示除了--background选项外,它不应该起作用。

您还应该重定向start-stop-dameon的输出,它只是您需要重定向的进程的输出。 start-stop-daemon会将其重定向到/dev/null IIRC,但我没有办法覆盖它。您可能必须创建一个包装器,exec使用适当的重定向并在--startas选项中使用它,除非您设法告诉autossh直接登录文件。

答案 1 :(得分:0)

下面的脚本在调用时只运行一个autossh进程。

#!/bin/sh
### BEGIN INIT INFO
# Provides:          skeleton
# Required-Start:    $network $remote_fs $syslog
# Required-Stop:     $network $remote_fs $syslog
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: autosshtun
# Description:       Used to launch SSH tunnel with autossh
### END INIT INFO

# Author: Laurent HUBERT <lau.hub@gmail.com>
#
# Do NOT "set -e"

# PATH should only include /usr/* if it runs after the mountnfs.sh script
PATH=/sbin:/usr/sbin:/bin:/usr/bin
DESC="AUTOSSH Tunnel"
NAME=autosshtun
DAEMON=/usr/lib/autossh/autossh
DAEMON_ARGS=""
PIDFILE=/var/run/$NAME.pid
SCRIPTNAME=/etc/init.d/$NAME

# Exit if the package is not installed
[ -x "$DAEMON" ] || exit 0

# Read configuration variable file if it is present
[ -r /etc/default/$NAME ] && . /etc/default/$NAME

# Load the VERBOSE setting and other rcS variables
. /lib/init/vars.sh

# Define LSB log_* functions.
# Depend on lsb-base (>= 3.2-14) to ensure that this file is present
# and status_of_proc is working.
. /lib/lsb/init-functions

CONN_PORT=22
SSH_TUNNEL_KEY_PATH=/root/.ssh/id-rsa
SSH_USERNAME=user
SSH_SERVER=server.domain.tld
ENTRY_PORT=1122
EXIT_PORT=22

ENV_COMMAND=/usr/bin/env 
ENV_OPTIONS="AUTOSSH_PIDFILE=$PIDFILE"
SSH_COMMAND_OPTIONS="-p $CONN_PORT -i $SSH_TUNNEL_KEY_PATH $SSH_USERNAME@$SSH_SERVER"
OPEN_TUNNEL_OPTIONS="-T -N -R $ENTRY_PORT:localhost:$EXIT_PORT"

AUTOSSH_OPTION="-M 0" 

DAEMON_ARGS="$AUTOSSH_OPTION $SSH_COMMAND_OPTIONS $OPEN_TUNNEL_OPTIONS"

is_running() {
    if [ -f $PIDFILE ]; then
        PID=`cat $PIDFILE`
        if [ -n "$PID" ]; then
            return 0
        else
            return 1
        fi
    else
        return 1
    fi
}
#
# Function that starts the daemon/service
#
do_start()
{
    QUIET="--quiet"
    if ! is_running; then
        start-stop-daemon --background --start $QUIET --name $NAME \
            --exec $ENV_COMMAND -- $ENV_OPTIONS $DAEMON $DAEMON_ARGS
        retval=$?
        sleep 1

        if [ $retval -gt 0 ]; then
            return $retval
        else
            sleep 1
            start-stop-daemon --stop $QUIET --pidfile $PIDFILE \
            --test --exec $DAEMON > /dev/null || return 2
        fi
    else
        return 1
    fi
}

#
# Function that stops the daemon/service
#
do_stop()
{
    # Return
    #   0 if daemon has been stopped
    #   1 if daemon was already stopped
    #   2 if daemon could not be stopped
    #   other if a failure occurred
    start-stop-daemon --stop --quiet --retry=TERM/10/KILL/5 --pidfile $PIDFILE --name $NAME
    RETVAL="$?"
    [ "$RETVAL" = 2 ] && return 2
    # Wait for children to finish too if this is a daemon that forks
    # and if the daemon is only ever run from this initscript.
    # If the above conditions are not satisfied then add some other code
    # that waits for the process to drop all resources that could be
    # needed by services started subsequently.  A last resort is to
    # sleep for some time.
    start-stop-daemon --stop --quiet --oknodo --retry=TERM/10/KILL/5 --exec $DAEMON
    [ "$?" = 2 ] && return 2
    # Many daemons don't delete their pidfiles when they exit.
    rm -f $PIDFILE
    return "$RETVAL"
}


case "$1" in
  start)
    [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
    do_start
    case "$?" in
        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
    esac
    ;;
  stop)
    [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
    do_stop
    case "$?" in
        0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
        2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
    esac
    ;;
  status)
    status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
    ;;
  restart)
    log_daemon_msg "Restarting $DESC" "$NAME"
    do_stop
    case "$?" in
      0|1)
        do_start
        case "$?" in
            0) log_end_msg 0 ;;
            1) log_end_msg 1 ;; # Old process is still running
            *) log_end_msg 1 ;; # Failed to start
        esac
        ;;
      *)
        # Failed to stop
        log_end_msg 1
        ;;
    esac
    ;;
  *)
    echo "Usage: $SCRIPTNAME {start|stop|status|restart}" >&2
    exit 3
    ;;
esac