init-multi-mode 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. #!/bin/sh
  2. function init_multi_mode()
  3. {
  4. ACTION=$1
  5. CONFIG=$2
  6. ERROR=0
  7. if [ -z $ACTION ]
  8. then
  9. echo "Usage: $0 {start|stop|status|restart} [CONFIGURATION_NAME]"
  10. exit 3
  11. fi
  12. if [ -z $CONFIG ]
  13. then
  14. for CONFIG_FOLDER in $( ls -d /etc/beegfs/*.d )
  15. do
  16. if [ -r ${CONFIG_FOLDER}/${SERVICE_NAME}.conf ]
  17. then
  18. CONFIG=$( basename $CONFIG_FOLDER .d )
  19. init_multi_mode_single_configuration $ACTION $CONFIG
  20. if [ $? -ne 0 ]
  21. then
  22. ERROR=1
  23. fi
  24. fi
  25. done
  26. else
  27. if [ -e "/etc/beegfs/${CONFIG}.d" ]
  28. then
  29. init_multi_mode_single_configuration $ACTION $CONFIG
  30. ERROR=$?
  31. else
  32. echo "Configuration folder /etc/beegfs/${CONFIG}.d doesn't exist."
  33. ERROR=1
  34. fi
  35. fi
  36. return $ERROR
  37. }
  38. function init_multi_mode_single_configuration()
  39. {
  40. ACTION=$1
  41. CONFIG=$2
  42. if [ -z $ACTION ]
  43. then
  44. echo "Usage: $0 {start|stop|status|restart} [CONFIGURATION_NAME]"
  45. exit 3
  46. fi
  47. # Check for existence of needed config file and read it
  48. APP_CONFIG=/etc/beegfs/${CONFIG}.d/${SERVICE_NAME}.conf
  49. test -r $APP_CONFIG || { echo "$APP_CONFIG not existing";
  50. if [ "$1" = "stop" ]; then exit 0;
  51. else exit 6; fi; }
  52. PIDFILE=/var/run/${SERVICE_NAME}-${CONFIG}.pid
  53. DAEMON_ARGS="cfgFile=${APP_CONFIG} pidFile=$PIDFILE"
  54. RETVAL=0
  55. # Return values acc. to LSB for all commands but status:
  56. # 0 - success
  57. # 1 - generic or unspecified error
  58. # 2 - invalid or excess argument(s)
  59. # 3 - unimplemented feature (e.g. "reload")
  60. # 4 - user had insufficient privileges
  61. # 5 - program is not installed
  62. # 6 - program is not configured
  63. # 7 - program is not running
  64. # 8--199 - reserved (8--99 LSB, 100--149 distrib, 150--199 appl)
  65. #
  66. # Note that starting an already running service, stopping
  67. # or restarting a not-running service as well as the restart
  68. # with force-reload (in case signaling is not supported) are
  69. # considered a success.
  70. case "$ACTION" in
  71. start)
  72. if [ -f "${SYSCONFIG_FILE}" ]; then
  73. if [ "${START_SERVICE}" = "NO" -o "${START_SERVICE}" = "no" ]; then
  74. echo "${APP_NAME} not set to be started"
  75. exit 0
  76. fi
  77. fi
  78. echo -n "Starting ${APP_NAME} (${CONFIG}): "
  79. ## create subfolder for lock files, on Debian systems needed
  80. mkdir -p /var/lock/subsys
  81. ## Start daemon with startproc(8). If this fails
  82. ## the return value is set appropriately by startproc.
  83. daemon --pidfile $PIDFILE $APP_BIN $DAEMON_ARGS \
  84. && touch /var/lock/subsys/${SERVICE_NAME}-${CONFIG}
  85. RETVAL=$?
  86. if [ ! -e /var/lock/subsys/${SERVICE_NAME} ]
  87. then
  88. touch /var/lock/subsys/${SERVICE_NAME}
  89. fi
  90. echo
  91. ;;
  92. stop)
  93. echo -n "Shutting down ${APP_NAME} (${CONFIG}): "
  94. ## Stop daemon with killproc(8) and if this fails
  95. ## killproc sets the return value according to LSB.
  96. killproc -p $PIDFILE $APP_BIN && rm -f /var/lock/subsys/${SERVICE_NAME}-${CONFIG}
  97. RETVAL=$?
  98. count=$(find /var/lock/subsys/ -name ${SERVICE_NAME}-* | wc -l)
  99. if [ $count -eq 0 ]
  100. then
  101. rm -f /var/lock/subsys/${SERVICE_NAME}
  102. fi
  103. echo
  104. ;;
  105. restart)
  106. ## Stop the service and regardless of whether it was
  107. ## running or not, start it again.
  108. $0 stop $CONFIG
  109. $0 start $CONFIG
  110. RETVAL=$?
  111. # Remember status and be quiet
  112. ;;
  113. status)
  114. echo -n "Checking for service ${APP_NAME} (${CONFIG}): "
  115. ## Check status with checkproc(8), if process is running
  116. ## checkproc will return with exit status 0.
  117. # Return value is slightly different for the status command:
  118. # 0 - service up and running
  119. # 1 - service dead, but /var/run/ pid file exists
  120. # 2 - service dead, but /var/lock/ lock file exists
  121. # 3 - service not running (unused)
  122. # 4 - service status unknown :-(
  123. # 5--199 reserved (5--99 LSB, 100--149 distro, 150--199 appl.)
  124. status_multi -p $PIDFILE $APP_BIN
  125. RETVAL=$?
  126. echo
  127. ;;
  128. *)
  129. echo "Usage: $0 {start|stop|status|restart} [CONFIGURATION_NAME]"
  130. exit 3
  131. ;;
  132. esac
  133. return $RETVAL
  134. }