Advertisement
Guest User

Untitled

a guest
Jun 6th, 2016
107
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.29 KB | None | 0 0
  1. #!/bin/sh
  2. ### BEGIN INIT INFO
  3. # Provides: fcgiwrap
  4. # Required-Start: $remote_fs
  5. # Required-Stop: $remote_fs
  6. # Should-Start:
  7. # Should-Stop:
  8. # Default-Start: 2 3 4 5
  9. # Default-Stop: 0 1 6
  10. # Short-Description: FastCGI wrapper
  11. # Description: Simple server for running CGI applications over FastCGI
  12. ### END INIT INFO
  13.  
  14. PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin
  15.  
  16. SPAWN_FCGI="/usr/bin/spawn-fcgi"
  17. DAEMON="/usr/sbin/fcgiwrap"
  18. NAME="fcgiwrap"
  19. DESC="FastCGI wrapper"
  20.  
  21. PIDFILE="/var/run/$NAME.pid"
  22.  
  23. test -x $SPAWN_FCGI || exit 0
  24. test -x $DAEMON || exit 0
  25.  
  26. # FCGI_APP Variables
  27. FCGI_CHILDREN="1"
  28. FCGI_SOCKET="/var/run/$NAME.socket"
  29. FCGI_USER="www-data"
  30. FCGI_GROUP="www-data"
  31. # Socket owner/group (will default to FCGI_USER/FCGI_GROUP if not defined)
  32. FCGI_SOCKET_OWNER="www-data"
  33. FCGI_SOCKET_GROUP="www-data"
  34.  
  35. . /lib/lsb/init-functions
  36.  
  37. # Default options, these can be overriden by the information
  38. # at /etc/default/$NAME
  39. DAEMON_OPTS="-f" # By default we redirect STDERR output from executed
  40. # CGI through FastCGI, to disable this behaviour set
  41. # DAEMON_OPTS to an empty value in the default's file
  42.  
  43. ENV_VARS="PATH='$PATH'" # We reset the environ for spawn-fcgi, but we use the
  44. # contents of this variable as a prefix when calling it
  45. # to export some variables (currently just the PATH)
  46. DIETIME=10 # Time to wait for the server to die, in seconds
  47. # If this value is set too low you might not
  48. # let some servers to die gracefully and
  49. # 'restart' will not work
  50. QDIETIME=0.5 # The same as DIETIME, but a lot shorter for the
  51. # stop case.
  52.  
  53. #STARTTIME=2 # Time to wait for the server to start, in seconds
  54. # If this value is set each time the server is
  55. # started (on start or restart) the script will
  56. # stall to try to determine if it is running
  57. # If it is not set and the server takes time
  58. # to setup a pid file the log message might
  59. # be a false positive (says it did not start
  60. # when it actually did)
  61.  
  62. # Include defaults if available
  63. if [ -f /etc/default/$NAME ] ; then
  64. . /etc/default/$NAME
  65. fi
  66.  
  67. set -e
  68.  
  69. running_pid() {
  70. # Check if a given process pid's cmdline matches a given name
  71. pid=$1
  72. name=$2
  73. [ -z "$pid" ] && return 1
  74. [ ! -d /proc/$pid ] && return 1
  75. cmd="$(cat /proc/$pid/cmdline | tr "\000" "\n"|head -n 1 |cut -d : -f 1)"
  76. # Is this the expected server
  77. [ "$cmd" != "$name" ] && return 1
  78. return 0
  79. }
  80.  
  81. running() {
  82. # Check if the process is running looking at /proc
  83. # (works for all users)
  84. # No pidfile, probably no daemon present
  85. [ ! -f "$PIDFILE" ] && return 1
  86. PIDS="$(cat "$PIDFILE")"
  87. for pid in $PIDS; do
  88. if [ -n "$pid" ]; then
  89. running_pid $pid $DAEMON && return 0 || true
  90. fi
  91. done
  92. return 1
  93. }
  94.  
  95. start_server() {
  96. ARGS="-P $PIDFILE"
  97. # Adjust NUMBER of processes
  98. if [ -n "$FCGI_CHILDREN" ]; then
  99. ARGS="$ARGS -F '$FCGI_CHILDREN'"
  100. fi
  101. # Adjust SOCKET or PORT and ADDR
  102. if [ -n "$FCGI_SOCKET" ]; then
  103. ARGS="$ARGS -s '$FCGI_SOCKET'"
  104. elif [ -n "$FCGI_PORT" ]; then
  105. if [ -n "$FCGI_ADDR" ]; then
  106. ARGS="$ARGS -a '$FCGI_ADDR'"
  107. fi
  108. ARGS="$ARGS -p '$FCGI_PORT'"
  109. fi
  110. # Adjust user
  111. if [ -n "$FCGI_USER" ]; then
  112. ARGS="$ARGS -u '$FCGI_USER'"
  113. if [ -n "$FCGI_SOCKET" ]; then
  114. if [ -n "$FCGI_SOCKET_OWNER" ]; then
  115. ARGS="$ARGS -U '$FCGI_SOCKET_OWNER'"
  116. else
  117. ARGS="$ARGS -U '$FCGI_USER'"
  118. fi
  119. fi
  120. fi
  121. # Adjust group
  122. if [ -n "$FCGI_GROUP" ]; then
  123. ARGS="$ARGS -g '$FCGI_GROUP'"
  124. if [ -n "$FCGI_SOCKET" ]; then
  125. if [ -n "$FCGI_SOCKET_GROUP" ]; then
  126. ARGS="$ARGS -G '$FCGI_SOCKET_GROUP'"
  127. else
  128. ARGS="$ARGS -G '$FCGI_GROUP'"
  129. fi
  130. fi
  131. fi
  132. eval $(echo env -i $ENV_VARS $SPAWN_FCGI $ARGS -- $DAEMON $DAEMON_OPTS) \
  133. > /dev/null
  134. errcode="$?"
  135. return $errcode
  136. }
  137.  
  138. stop_server() {
  139. # Force the process to die killing it manually
  140. [ ! -e "$PIDFILE" ] && return
  141. PIDS="$(cat "$PIDFILE")"
  142. for pid in $PIDS; do
  143. if running_pid $pid $DAEMON; then
  144. kill -15 $pid
  145. # Is it really dead?
  146. sleep "$QDIETIME"s
  147. if running_pid $pid $DAEMON; then
  148. kill -9 $pid
  149. sleep "$QDIETIME"s
  150. if running_pid $pid $DAEMON; then
  151. echo "Cannot kill $NAME (pid=$pid)!"
  152. exit 1
  153. fi
  154. fi
  155. fi
  156. done
  157. rm -f "$PIDFILE"
  158. if [ -n "$FCGI_SOCKET" ]; then
  159. rm -f "$FCGI_SOCKET"
  160. fi
  161. }
  162.  
  163. case "$1" in
  164. start)
  165. log_daemon_msg "Starting $DESC" "$NAME"
  166. # Check if it's running first
  167. if running ; then
  168. log_progress_msg "apparently already running"
  169. log_end_msg 0
  170. exit 0
  171. fi
  172. if start_server ; then
  173. # NOTE: Some servers might die some time after they start,
  174. # this code will detect this issue if STARTTIME is set
  175. # to a reasonable value
  176. [ -n "$STARTTIME" ] && sleep $STARTTIME # Wait some time
  177. if running ; then
  178. # It's ok, the server started and is running
  179. log_end_msg 0
  180. else
  181. # It is not running after we did start
  182. log_end_msg 1
  183. fi
  184. else
  185. # Either we could not start it
  186. log_end_msg 1
  187. fi
  188. ;;
  189. stop|force-stop)
  190. log_daemon_msg "Stopping $DESC" "$NAME"
  191. if running ; then
  192. # Only stop the server if we see it running
  193. errcode=0
  194. stop_server || errcode=$?
  195. log_end_msg $errcode
  196. else
  197. # If it's not running don't do anything
  198. log_progress_msg "apparently not running"
  199. log_end_msg 0
  200. exit 0
  201. fi
  202. ;;
  203. restart|force-reload)
  204. log_daemon_msg "Restarting $DESC" "$NAME"
  205. errcode=0
  206. stop_server || errcode=$?
  207. # Wait some sensible amount, some server need this
  208. [ -n "$DIETIME" ] && sleep $DIETIME
  209. start_server || errcode=$?
  210. [ -n "$STARTTIME" ] && sleep $STARTTIME
  211. running || errcode=$?
  212. log_end_msg $errcode
  213. ;;
  214. status)
  215.  
  216. log_daemon_msg "Checking status of $DESC" "$NAME"
  217. if running ; then
  218. log_progress_msg "running"
  219. log_end_msg 0
  220. else
  221. log_progress_msg "apparently not running"
  222. log_end_msg 1
  223. exit 1
  224. fi
  225. ;;
  226. # Use this if the daemon cannot reload
  227. reload)
  228. log_warning_msg "Reloading $NAME daemon: not implemented, as the daemon"
  229. log_warning_msg "cannot re-read the config file (use restart)."
  230. ;;
  231. *)
  232. N=/etc/init.d/$NAME
  233. echo "Usage: $N {start|stop|force-stop|restart|force-reload|status}" >&2
  234. exit 1
  235. ;;
  236. esac
  237.  
  238. exit 0
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement