home *** CD-ROM | disk | FTP | other *** search
/ ftp.ncftp.com / ftp.ncftp.com.zip / ftp.ncftp.com / ncftpd / end_of_life / ncftpd-2.8.7-linux-x86-glibc2.1-export.tar.gz / ncftpd-2.8.7-linux-x86-glibc2.1-export.tar / ncftpd-2.8.7 / extra / ncftpd.init < prev    next >
Text File  |  2011-01-17  |  8KB  |  330 lines

  1. #!/bin/sh
  2.  
  3. #############################################################################
  4. #
  5. # Choose "verbose mode" by setting NCFTPD_VERBOSITY to "-v" while testing,
  6. # and then set it NCFTPD_VERBOSITY to "" when testing is complete.
  7. #
  8. # NCFTPD_VERBOSITY="-v"
  9. NCFTPD_VERBOSITY=""
  10.  
  11.  
  12. #############################################################################
  13. #
  14. # Location of NcFTPd's configuration files.
  15. #
  16. NCFTPD_GENERAL_CF="/usr/local/etc/ncftpd/general.cf"
  17. NCFTPD_DOMAIN_CF="/usr/local/etc/ncftpd/domain.cf"
  18.  
  19.  
  20. #############################################################################
  21. #
  22. # Location of the startup log.  This log is used before the logger
  23. # process has been bootstrapped, so the startup log serves as a
  24. # location to log errors that occur before the logger has been
  25. # started.
  26. #
  27. NCFTPD_STARTUP_ERROR_LOG="/usr/local/etc/ncftpd/startup_errors"
  28.  
  29.  
  30. #############################################################################
  31. #
  32. # Rather than running $NCFTPD_PROG directly, we set the
  33. # PATH and use a relative path.  This makes it easy to
  34. # spot the "ncftpd" processes in "ps" or "top".
  35. #
  36. # This also implies that you must properly set the variable
  37. # $NCFTPD_PROG_PATH so it contains the directory containing
  38. # $NCFTPD_PROG.
  39. #
  40. NCFTPD_PROG_PATH="/usr/local/sbin:/usr/local/bin:$PATH"
  41. NCFTPD_PROG="/usr/local/sbin/ncftpd"
  42.  
  43.  
  44. #############################################################################
  45. # No more user-configurable options below this point.
  46. #############################################################################
  47.  
  48. PGREP="/usr/local/bin/pgrep.pl"
  49. PKILL="/usr/local/bin/pkill.pl"
  50.  
  51. #SOURCE-RC
  52. if [ "$rc_done" = "" ] ; then
  53.     rc_done="OK"
  54.     rc_failed="FAILED"
  55. fi
  56.  
  57. if [ "$#" -eq 0 ] ; then arg="start" ; else arg="$1" ; fi
  58.  
  59. # Return values acc. to LSB for all commands _except_ status:
  60. # 0       - success
  61. # 1       - generic or unspecified error
  62. # 2       - invalid or excess argument(s)
  63. # 3       - unimplemented feature (e.g. "reload")
  64. # 4       - user had insufficient privileges
  65. # 5       - program is not installed
  66. # 6       - program is not configured
  67. # 7       - program is not running
  68. # 8--199  - reserved (8--99 LSB, 100--149 distrib, 150--199 appl)
  69. es=5
  70.  
  71.  
  72. #
  73. # The install script may create aptly named symbolic links
  74. # to this script.
  75. #
  76. case "$0" in
  77.     *restart*)
  78.         arg="restart"
  79.         ;;
  80.     *start*)
  81.         arg="start"
  82.         ;;
  83.     *stop*)
  84.         arg="stop"
  85.         ;;
  86.     *reload*)
  87.         arg="reload"
  88.         ;;
  89.     *status*)
  90.         arg="status"
  91.         ;;
  92. esac
  93.  
  94. case "$arg" in
  95. 'start')
  96.     # Only proceed if everything is properly installed
  97.     if [ -f "${NCFTPD_PROG}" ] && [ -f "${NCFTPD_GENERAL_CF}" ] && [ -f "${NCFTPD_DOMAIN_CF}" ] ; then
  98.         es=1
  99.         echo -n 'Starting NcFTPd: '    # ECHO-N
  100.         oPATH="$PATH"
  101.         PATH="${NCFTPD_PROG_PATH}"
  102.         export PATH
  103.         ncftpd -d ${NCFTPD_VERBOSITY} "${NCFTPD_GENERAL_CF}" "${NCFTPD_DOMAIN_CF}" > "$NCFTPD_STARTUP_ERROR_LOG" 2>&1
  104.         es="$?"
  105.         PATH="$oPATH"
  106.         if [ "$es" -eq 0 ] ; then
  107.             # Successfully backgrounded
  108.             echo -e "$rc_done"    # ECHO-E
  109.         else
  110.             es=7
  111.             echo -e "$rc_failed"    # ECHO-E
  112.             if [ -s "$NCFTPD_STARTUP_ERROR_LOG" ] ; then
  113.                 echo "Contents of $NCFTPD_STARTUP_ERROR_LOG:"
  114.                 sed 's/^/    /;' < "$NCFTPD_STARTUP_ERROR_LOG"
  115.             fi
  116.         fi
  117.     elif [ -f "${NCFTPD_PROG}" ] ; then
  118.         es=6
  119.     fi
  120.     ;;
  121.  
  122. 'stop')
  123.     # Only proceed if everything is properly installed
  124.     if [ -f "${NCFTPD_PROG}" ] && [ -f "${NCFTPD_GENERAL_CF}" ] ; then
  125.         #
  126.         # This tries to figure out what you set "pid-file" to.
  127.         #
  128.         es=7
  129.         echo -n 'Stopping NcFTPd: '    # ECHO-N
  130.         NCFTPD_STOP_SCRIPT=`grep '^pid-file' "${NCFTPD_GENERAL_CF}" | cut -d= -f2`
  131.         if [ -x "$NCFTPD_STOP_SCRIPT" ] ; then
  132.             /bin/sh "$NCFTPD_STOP_SCRIPT"
  133.             es=0
  134.         fi
  135.         if [ -x "$PKILL" ] ; then
  136.             es=1
  137.             sig="-15"
  138.             for tries in 1 2 3 ; do
  139.                 case "$PKILL" in
  140.                     *.pl)
  141.                         "$PKILL" "$sig" -X $$ -x ncftpd
  142.                         ;;
  143.                     *)
  144.                         "$PKILL" "$sig" -x ncftpd
  145.                         ;;
  146.                 esac
  147.                 if [ $? -eq 1 ] ; then
  148.                     # No processes remaining, done
  149.                     es=0
  150.                     break
  151.                 fi
  152.                 sleep 3
  153.                 sig="-9"
  154.             done
  155.         fi
  156.         if [ "$es" -eq 0 ] ; then
  157.             echo -e "$rc_done"    # ECHO-E
  158.         else
  159.             echo -e "$rc_failed"    # ECHO-E
  160.         fi
  161.     else
  162.         es=6
  163.         if [ -x "$PKILL" ] ; then
  164.             es=1
  165.             sig="-15"
  166.             for tries in 1 2 3 ; do
  167.                 case "$PKILL" in
  168.                     *.pl)
  169.                         "$PKILL" "$sig" -X $$ -x ncftpd
  170.                         ;;
  171.                     *)
  172.                         "$PKILL" "$sig" -x ncftpd
  173.                         ;;
  174.                 esac
  175.                 if [ $? -eq 1 ] ; then
  176.                     # No processes remaining, done
  177.                     es=0
  178.                     break
  179.                 fi
  180.                 sleep 3
  181.                 sig="-9"
  182.             done
  183.         fi
  184.         if [ "$es" -eq 0 ] ; then
  185.             echo -e "$rc_done"    # ECHO-E
  186.         else
  187.             echo -e "$rc_failed"    # ECHO-E
  188.         fi
  189.     fi
  190.     ;;
  191.  
  192. 'restart'|'force-reload')
  193.     if [ -f "${NCFTPD_PROG}" ] && [ -f "${NCFTPD_GENERAL_CF}" ] && [ -f "${NCFTPD_DOMAIN_CF}" ] ; then
  194.         es=1
  195.         echo -n 'Restarting NcFTPd: '    # ECHO-N
  196.         # First stop...
  197.         stopped="yes"
  198.         NCFTPD_STOP_SCRIPT=`grep '^pid-file' "${NCFTPD_GENERAL_CF}" | cut -d= -f2`
  199.         if [ -x "$NCFTPD_STOP_SCRIPT" ] ; then
  200.             /bin/sh "$NCFTPD_STOP_SCRIPT"
  201.         fi
  202.         if [ -x "$PKILL" ] ; then
  203.             sig="-15"
  204.             stopped="no"
  205.             for tries in 1 2 3 ; do
  206.                 case "$PKILL" in
  207.                     *.pl)
  208.                         "$PKILL" "$sig" -X $$ -x ncftpd
  209.                         ;;
  210.                     *)
  211.                         "$PKILL" "$sig" -x ncftpd
  212.                         ;;
  213.                 esac
  214.                 if [ $? -eq 1 ] ; then
  215.                     # No processes remaining, done
  216.                     stopped="yes"
  217.                     break
  218.                 fi
  219.                 sleep 3
  220.                 sig="-9"
  221.             done
  222.         fi
  223.  
  224.         # Then start.
  225.         if [ "$stopped" = yes ] ; then
  226.             oPATH="$PATH"
  227.             PATH="${NCFTPD_PROG_PATH}"
  228.             export PATH
  229.             ncftpd -d ${NCFTPD_VERBOSITY} "${NCFTPD_GENERAL_CF}" "${NCFTPD_DOMAIN_CF}" > "$NCFTPD_STARTUP_ERROR_LOG" 2>&1
  230.             es="$?"
  231.             PATH="$oPATH"
  232.         else
  233.             echo "Could not stop previous NcFTPd instance." 1>&2
  234.             es=1
  235.         fi
  236.         if [ "$es" -eq 0 ] ; then
  237.             # Successfully backgrounded
  238.             echo -e "$rc_done"    # ECHO-E
  239.         else
  240.             es=7
  241.             echo -e "$rc_failed"    # ECHO-E
  242.             if [ -s "$NCFTPD_STARTUP_ERROR_LOG" ] ; then
  243.                 echo "Contents of $NCFTPD_STARTUP_ERROR_LOG:"
  244.                 sed 's/^/    /;' < "$NCFTPD_STARTUP_ERROR_LOG"
  245.             fi
  246.         fi
  247.     elif [ -f "${NCFTPD_PROG}" ] ; then
  248.         es=6
  249.     fi
  250.     ;;
  251.  
  252. 'reload')
  253.     if [ -f "${NCFTPD_PROG}" ] && [ -f "${NCFTPD_GENERAL_CF}" ] && [ -f "${NCFTPD_DOMAIN_CF}" ] ; then
  254.         es=1
  255.         #
  256.         # NOTE: Currently only the domain.cf can be reloaded.
  257.         # If you change the general.cf, you have to do a full restart.
  258.         #
  259.         echo -n 'Reloading NcFTPd domains: '    # ECHO-N
  260.         NCFTPD_STOP_SCRIPT=`grep '^pid-file' "${NCFTPD_GENERAL_CF}" | cut -d= -f2`
  261.         if [ -x "$NCFTPD_STOP_SCRIPT" ] ; then
  262.             sed -n '/main/{s/-15/-1/;p;q;}' "$NCFTPD_STOP_SCRIPT" | /bin/sh
  263.             es=0
  264.         fi
  265.         if [ "$es" -eq 0 ] ; then
  266.             echo -e "$rc_done"    # ECHO-E
  267.         else
  268.             echo -e "$rc_failed"    # ECHO-E
  269.         fi
  270.     elif [ -f "${NCFTPD_PROG}" ] ; then
  271.         es=6
  272.     fi
  273.     ;;
  274. 'status')
  275.     # If the status action is requested, the init script will return the following exit status codes.
  276.     # 
  277.     # 0    program is running or service is OK
  278.     # 1    program is dead and /var/run pid file exists
  279.     # 2    program is dead and /var/lock lock file exists
  280.     # 3    program is not running
  281.     # 4    program or service status is unknown
  282.     # 5-99    reserved for future LSB use
  283.     # 100-149    reserved for distribution use
  284.     # 150-199    reserved for application use
  285.     # 200-254    reserved
  286.     if [ ! -x /usr/bin/ftp ] && [ ! -x /bin/ftp ] && [ ! -x /usr/local/bin/ftp ] ; then
  287.         echo "/usr/bin/ftp is not installed, which is required for checking NcFTPd status." 1>&2
  288.         echo -e "$rc_failed"    # ECHO-E
  289.         es=4
  290.     elif [ -f "${NCFTPD_PROG}" ] && [ -f "${NCFTPD_GENERAL_CF}" ] && [ -f "${NCFTPD_DOMAIN_CF}" ] ; then
  291.         #
  292.         # Invoke /usr/bin/ftp and immediately send a QUIT command without logging in.
  293.         # The FTP server should simply display its banner message, followed by the
  294.         # response to the QUIT command as a 221 message.  If we see the 221 message
  295.         # we conclude the server is running.
  296.         #
  297.         # FTP_PORT_OPT="-P 21"
  298.         FTP_PORT_OPT=`sed -n '/^port=/{s/^port=21$//;s/^port=/-P /;p;q;}' "${NCFTPD_GENERAL_CF}" 2>/dev/null`
  299.         ncftpd_status=`echo "quit" | ftp -v -n $FTP_PORT_OPT localhost 2>&1 | sed -n '/.onnection..efused/{s/^.*$/NOT RUNNING/;p;};/221/{s/^.*$/A_OK/;p;q;}' | sort | sed -n 's/^A_//;1,1p'`
  300.         if [ -z "$ncftpd_status" ] ; then ncftpd_status="ERROR" ; fi
  301.         case "$ncftpd_status" in
  302.             'OK')
  303.                 es=0
  304.                 echo -e "$rc_done"    # ECHO-E
  305.                 ;;
  306.             'NOT RUNNING')
  307.                 echo "NcFTPd status: $ncftpd_status"
  308.                 echo -e "$rc_failed"    # ECHO-E
  309.                 es=3
  310.                 ;;
  311.             'ERROR'|*)
  312.                 echo "NcFTPd status: $ncftpd_status"
  313.                 es=1
  314.                 echo -e "$rc_failed"    # ECHO-E
  315.                 ;;
  316.         esac
  317.     elif [ -f "${NCFTPD_PROG}" ] ; then
  318.         es=6
  319.     fi
  320.     ;;
  321. #EXTRA
  322.  
  323. *)
  324.     echo "Usage: $0 { start | stop | restart | reload | status }"
  325.     es=3
  326.     ;;
  327. esac
  328.  
  329. exit "$es"                    # EXIT
  330.