autossh.init.d 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides: AutoSSH
  4. # Required-Start: $local_fs $network $remote_fs $syslog
  5. # Required-Stop: $local_fs $network $remote_fs $syslog
  6. # Default-Start: 2 3 4 5
  7. # Default-Stop: 0 1 6
  8. # Short-Description: start the autossh daemon
  9. # Description: start the autossh daemon
  10. ### END INIT INFO
  11. # Author: Antoine Beaupré <anarcat@koumbit.org>
  12. # Do NOT "set -e"
  13. # PATH should only include /usr/* if it runs after the mountnfs.sh script
  14. PATH=/sbin:/usr/sbin:/bin:/usr/bin
  15. DESC="autossh"
  16. NAME=autossh
  17. USER=$NAME
  18. DAEMON=/usr/bin/autossh
  19. DAEMON_ARGS="-f"
  20. PIDFILE=/var/run/$NAME.pid
  21. SCRIPTNAME=/etc/init.d/$NAME
  22. # Read configuration variable file if it is present
  23. [ -r /etc/default/$NAME ] && . /etc/default/$NAME
  24. AUTOSSH_PIDFILE=$PIDFILE
  25. export AUTOSSH_PIDFILE
  26. # Exit if the package is not installed
  27. [ -x "$DAEMON" ] || exit 0
  28. # Load the VERBOSE setting and other rcS variables
  29. . /lib/init/vars.sh
  30. # Define LSB log_* functions.
  31. # Depend on lsb-base (>= 3.2-14) to ensure that this file is present
  32. # and status_of_proc is working.
  33. . /lib/lsb/init-functions
  34. #
  35. # Function that starts the daemon/service
  36. #
  37. do_start()
  38. {
  39. # Return
  40. # 0 if daemon has been started
  41. # 1 if daemon was already running
  42. # 2 if daemon could not be started
  43. start-stop-daemon --start --quiet --user $USER --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
  44. || return 1
  45. start-stop-daemon --start --quiet --user $USER --chuid $USER --pidfile $PIDFILE --exec $DAEMON -- \
  46. $DAEMON_ARGS \
  47. || return 2
  48. # The above code will not work for interpreted scripts, use the next
  49. # six lines below instead (Ref: #643337, start-stop-daemon(8) )
  50. #start-stop-daemon --start --quiet --pidfile $PIDFILE --startas $DAEMON \
  51. # --name $NAME --test > /dev/null \
  52. # || return 1
  53. #start-stop-daemon --start --quiet --pidfile $PIDFILE --startas $DAEMON \
  54. # --name $NAME -- $DAEMON_ARGS \
  55. # || return 2
  56. # Add code here, if necessary, that waits for the process to be ready
  57. # to handle requests from services started subsequently which depend
  58. # on this one. As a last resort, sleep for some time.
  59. }
  60. #
  61. # Function that stops the daemon/service
  62. #
  63. do_stop()
  64. {
  65. # Return
  66. # 0 if daemon has been stopped
  67. # 1 if daemon was already stopped
  68. # 2 if daemon could not be stopped
  69. # other if a failure occurred
  70. start-stop-daemon --stop --quiet --retry=TERM/30/KILL/5 --pidfile $PIDFILE --user $USER --name $NAME
  71. RETVAL="$?"
  72. [ "$RETVAL" = 2 ] && return 2
  73. # Wait for children to finish too if this is a daemon that forks
  74. # and if the daemon is only ever run from this initscript.
  75. # If the above conditions are not satisfied then add some other code
  76. # that waits for the process to drop all resources that could be
  77. # needed by services started subsequently. A last resort is to
  78. # sleep for some time.
  79. start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --user $USER --exec $DAEMON
  80. [ "$?" = 2 ] && return 2
  81. # Many daemons don't delete their pidfiles when they exit.
  82. rm -f $PIDFILE
  83. return "$RETVAL"
  84. }
  85. #
  86. # Function that sends a SIGHUP to the daemon/service
  87. #
  88. do_reload() {
  89. #
  90. # If the daemon can reload its configuration without
  91. # restarting (for example, when it is sent a SIGHUP),
  92. # then implement that here.
  93. #
  94. start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
  95. return 0
  96. }
  97. case "$1" in
  98. start)
  99. [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
  100. do_start
  101. case "$?" in
  102. 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  103. 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  104. esac
  105. ;;
  106. stop)
  107. [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
  108. do_stop
  109. case "$?" in
  110. 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
  111. 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
  112. esac
  113. ;;
  114. status)
  115. status_of_proc -p "$PIDFILE" "$DAEMON" "$NAME" && exit 0 || exit $?
  116. ;;
  117. reload|force-reload)
  118. log_daemon_msg "Reloading $DESC" "$NAME"
  119. do_reload
  120. log_end_msg $?
  121. ;;
  122. restart)
  123. #
  124. # If the "reload" option is implemented then remove the
  125. # 'force-reload' alias
  126. #
  127. log_daemon_msg "Restarting $DESC" "$NAME"
  128. do_stop
  129. case "$?" in
  130. 0|1)
  131. do_start
  132. case "$?" in
  133. 0) log_end_msg 0 ;;
  134. 1) log_end_msg 1 ;; # Old process is still running
  135. *) log_end_msg 1 ;; # Failed to start
  136. esac
  137. ;;
  138. *)
  139. # Failed to stop
  140. log_end_msg 1
  141. ;;
  142. esac
  143. ;;
  144. *)
  145. #echo "Usage: $SCRIPTNAME {start|stop|restart|reload|force-reload}" >&2
  146. echo "Usage: $SCRIPTNAME {start|stop|status|restart|force-reload}" >&2
  147. exit 3
  148. ;;
  149. esac
  150. :