1 #!/bin/sh
2 ### BEGIN INIT INFO
3 # Provides: php-fpm php5-fpm
4 # Required-Start: $remote_fs $network
5 # Required-Stop: $remote_fs $network
6 # Default-Start: 2 3 4 5
7 # Default-Stop:
8 # Short-Description: starts php5-fpm
9 # Description: Starts PHP5 FastCGI Process Manager Daemon
10 ### END INIT INFO
12 # Author: Ondrej Sury <ondrej@debian.org>
14 PATH=/sbin:/usr/sbin:/bin:/usr/bin
15 DESC="PHP5 FastCGI Process Manager"
16 NAME=php5-fpm
17 DAEMON=/usr/sbin/$NAME
18 DAEMON_ARGS="--fpm-config /etc/php5/fpm/php-fpm.conf"
19 PIDFILE=/var/run/php5-fpm.pid
20 TIMEOUT=30
21 SCRIPTNAME=/etc/init.d/$NAME
23 # Exit if the package is not installed
24 [ -x "$DAEMON" ] || exit 0
26 # Read configuration variable file if it is present
27 [ -r /etc/default/$NAME ] && . /etc/default/$NAME
29 # Load the VERBOSE setting and other rcS variables
30 . /lib/init/vars.sh
32 # Define LSB log_* functions.
33 # Depend on lsb-base (>= 3.0-6) to ensure that this file is present.
34 . /lib/lsb/init-functions
36 #
37 # Function to check the correctness of the config file
38 #
39 do_check()
40 {
41 [ "$1" != "no" ] && $DAEMON $DAEMON_ARGS -t 2>&1 | grep -v "\[ERROR\]"
42 FPM_ERROR=$($DAEMON $DAEMON_ARGS -t 2>&1 | grep "\[ERROR\]")
44 if [ -n "${FPM_ERROR}" ]; then
45 echo "Please fix your configuration file..."
46 $DAEMON $DAEMON_ARGS -t 2>&1 | grep "\[ERROR\]"
47 return 1
48 fi
49 return 0
50 }
52 #
53 # Function that starts the daemon/service
54 #
55 do_start()
56 {
57 # Return
58 # 0 if daemon has been started
59 # 1 if daemon was already running
60 # 2 if daemon could not be started
61 start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON --test > /dev/null \
62 || return 1
63 start-stop-daemon --start --quiet --pidfile $PIDFILE --exec $DAEMON -- \
64 $DAEMON_ARGS 2>/dev/null \
65 || return 2
66 # Add code here, if necessary, that waits for the process to be ready
67 # to handle requests from services started subsequently which depend
68 # on this one. As a last resort, sleep for some time.
69 }
71 #
72 # Function that stops the daemon/service
73 #
74 do_stop()
75 {
76 # Return
77 # 0 if daemon has been stopped
78 # 1 if daemon was already stopped
79 # 2 if daemon could not be stopped
80 # other if a failure occurred
81 start-stop-daemon --stop --quiet --retry=TERM/$TIMEOUT/KILL/5 --pidfile $PIDFILE --name $NAME
82 RETVAL="$?"
83 [ "$RETVAL" = 2 ] && return 2
84 # Wait for children to finish too if this is a daemon that forks
85 # and if the daemon is only ever run from this initscript.
86 # If the above conditions are not satisfied then add some other code
87 # that waits for the process to drop all resources that could be
88 # needed by services started subsequently. A last resort is to
89 # sleep for some time.
90 start-stop-daemon --stop --quiet --oknodo --retry=0/30/KILL/5 --exec $DAEMON
91 [ "$?" = 2 ] && return 2
92 # Many daemons don't delete their pidfiles when they exit.
93 rm -f $PIDFILE
94 return "$RETVAL"
95 }
97 #
98 # Function that sends a SIGHUP to the daemon/service
99 #
100 do_reload() {
101 #
102 # If the daemon can reload its configuration without
103 # restarting (for example, when it is sent a SIGHUP),
104 # then implement that here.
105 #
106 start-stop-daemon --stop --signal 1 --quiet --pidfile $PIDFILE --name $NAME
107 return 0
108 }
110 case "$1" in
111 start)
112 [ "$VERBOSE" != no ] && log_daemon_msg "Starting $DESC" "$NAME"
113 do_check $VERBOSE
114 case "$?" in
115 0)
116 do_start
117 case "$?" in
118 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
119 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
120 esac
121 ;;
122 1) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
123 esac
124 ;;
125 stop)
126 [ "$VERBOSE" != no ] && log_daemon_msg "Stopping $DESC" "$NAME"
127 do_stop
128 case "$?" in
129 0|1) [ "$VERBOSE" != no ] && log_end_msg 0 ;;
130 2) [ "$VERBOSE" != no ] && log_end_msg 1 ;;
131 esac
132 ;;
133 status)
134 status_of_proc "$DAEMON" "$NAME" && exit 0 || exit $?
135 ;;
136 check)
137 do_check yes
138 ;;
139 reload|force-reload)
140 log_daemon_msg "Reloading $DESC" "$NAME"
141 do_reload
142 log_end_msg $?
143 ;;
144 restart)
145 log_daemon_msg "Restarting $DESC" "$NAME"
146 do_stop
147 case "$?" in
148 0|1)
149 do_start
150 case "$?" in
151 0) log_end_msg 0 ;;
152 1) log_end_msg 1 ;; # Old process is still running
153 *) log_end_msg 1 ;; # Failed to start
154 esac
155 ;;
156 *)
157 # Failed to stop
158 log_end_msg 1
159 ;;
160 esac
161 ;;
162 *)
163 echo "Usage: $SCRIPTNAME {start|stop|status|restart|reload|force-reload}" >&2
164 exit 1
165 ;;
166 esac
168 :
