summaryrefslogtreecommitdiff
path: root/testing/php/rc.d.php-fpm
blob: 54bcf4d5b321d0d7be6df4f2d2b97e7cd1c3a72e (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
#!/bin/bash

. /etc/rc.conf
. /etc/rc.d/functions


wait_for_pid () {
	try=0
	while test $try -lt 35 ; do
		case "$1" in
			'created')
			if [ -f "$2" ] ; then
				try=''
				break
			fi
			;;
			'removed')
			if [ ! -f "$2" ] ; then
				try=''
				break
			fi
			;;
		esac

		stat_append '.'
		try=`expr $try + 1`
		sleep 1
	done
}

test_config() {
	stat_busy 'Checking configuration'
	if [ $(id -u) -ne 0 ]; then
		stat_append '(This script must be run as root)'
		stat_die
	fi

	if [ ! -r /etc/php/php-fpm.conf ]; then
		stat_append '(/etc/php/php-fpm.conf not found)'
		stat_die
	fi

	local test=$(/usr/sbin/php-fpm -t 2>&1)
	if [ $? -gt 0 ]; then
		stat_append '(error in /etc/php/php-fpm.conf)'
		stat_die
	elif echo $test | grep -qi 'error'; then
		stat_append '(error in /etc/php/php.ini)'
		stat_die
	fi

	[ -d /var/run/php-fpm ] || install -d -m755 /var/run/php-fpm

	stat_done
}

case "$1" in
	start)
		test_config
		stat_busy 'Starting php-fpm'

		/usr/sbin/php-fpm

		if [ "$?" != 0 ] ; then
			stat_fail
			exit 1
		fi

		wait_for_pid created /var/run/php-fpm/php-fpm.pid

		if [ -n "$try" ] ; then
			stat_fail
			exit 1
		else
			add_daemon php-fpm
			stat_done
		fi
	;;

	stop)
		test_config
		stat_busy 'Gracefully shutting down php-fpm'

		if [ ! -r /var/run/php-fpm/php-fpm.pid ] ; then
			stat_fail
			exit 1
		fi

		kill -QUIT `cat /var/run/php-fpm/php-fpm.pid`

		wait_for_pid removed /var/run/php-fpm.pid

		if [ -n "$try" ] ; then
			stat_fail
			exit 1
		else
			rm_daemon php-fpm
			stat_done
		fi
	;;

	force-quit)
		stat_busy 'Terminating php-fpm'

		if [ ! -r /var/run/php-fpm/php-fpm.pid ] ; then
			stat_fail
			exit 1
		fi

		kill -TERM `cat /var/run/php-fpm/php-fpm.pid`

		wait_for_pid removed /var/run/php-fpm/php-fpm.pid

		if [ -n "$try" ] ; then
			stat_fail
			exit 1
		else
			rm_daemon php-fpm
			stat_done
		fi
	;;

	restart)
		$0 stop
		$0 start
	;;

	reload)
		test_config
		stat_busy 'Reload service php-fpm'

		if [ ! -r /var/run/php-fpm/php-fpm.pid ] ; then
			stat_fail
			exit 1
		fi

		kill -USR2 `cat /var/run/php-fpm/php-fpm.pid`
		stat_done
	;;

	logrotate)
		stat_busy 'Reopen php-fpm log'

		if [ ! -r /var/run/php-fpm/php-fpm.pid ] ; then
			stat_fail
			exit 1
		fi

		kill -USR1 `cat /var/run/php-fpm/php-fpm.pid`
		stat_done
	;;

	*)
		echo "usage: $0 {start|stop|force-quit|restart|reload|logrotate}"
		exit 1
	;;

esac