PHP-FPM启动脚本

编写初衷:

因为某些原因会经常重启php。但每次 都会提示端口被暂用。

[root@localhost ~]# /opt/php-7.1.11/sbin/php-fpm 
[29-Aug-2018 15:11:29] ERROR: unable to bind listening socket for address '127.0.0.1:9000': Address already in use (98)
[29-Aug-2018 15:11:29] ERROR: FPM initialization failed

只能每次kill掉进程,然后再启动,这样操作很麻烦。

 

 

创建/etc/init.d/php-fpm,并赋权

[root@localhost ~]# cd /etc/init.d/
[root@localhost init.d]# touch php-fpm
[root@localhost init.d]# chmod +x php-fpm

/etc/init.d/php-fpm文件内容

#! /bin/sh

### BEGIN INIT INFO
# Provides:          php-fpm
# Required-Start:    $remote_fs $network
# Required-Stop:     $remote_fs $network
# Default-Start:     2 3 4 5
# Default-Stop:      0 1 6
# Short-Description: starts php-fpm
# Description:       starts the PHP FastCGI Process Manager daemon
### END INIT INFO

prefix=/opt/php-7.1.11
exec_prefix=${prefix}

php_fpm_BIN=${exec_prefix}/sbin/php-fpm
php_fpm_CONF=${prefix}/etc/php-fpm.conf
php_fpm_PID=${prefix}/var/run/php-fpm.pid


php_opts="--fpm-config $php_fpm_CONF --pid $php_fpm_PID"


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

		echo -n .
		try=`expr $try + 1`
		sleep 1

	done

}

case "$1" in
	start)
		echo -n "Starting php-fpm "

		$php_fpm_BIN --daemonize $php_opts

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

		wait_for_pid created $php_fpm_PID

		if [ -n "$try" ] ; then
			echo " failed"
			exit 1
		else
			echo " done"
		fi
	;;

	stop)
		echo -n "Gracefully shutting down php-fpm "

		if [ ! -r $php_fpm_PID ] ; then
			echo "warning, no pid file found - php-fpm is not running ?"
			exit 1
		fi

		kill -QUIT `cat $php_fpm_PID`

		wait_for_pid removed $php_fpm_PID

		if [ -n "$try" ] ; then
			echo " failed. Use force-quit"
			exit 1
		else
			echo " done"
		fi
	;;

	status)
		if [ ! -r $php_fpm_PID ] ; then
			echo "php-fpm is stopped"
			exit 0
		fi

		PID=`cat $php_fpm_PID`
		if ps -p $PID | grep -q $PID; then
			echo "php-fpm (pid $PID) is running..."
		else
			echo "php-fpm dead but pid file exists"
		fi
	;;

	force-quit)
		echo -n "Terminating php-fpm "

		if [ ! -r $php_fpm_PID ] ; then
			echo "warning, no pid file found - php-fpm is not running ?"
			exit 1
		fi

		kill -TERM `cat $php_fpm_PID`

		wait_for_pid removed $php_fpm_PID

		if [ -n "$try" ] ; then
			echo " failed"
			exit 1
		else
			echo " done"
		fi
	;;

	restart)
		$0 stop
		$0 start
	;;

	reload)

		echo -n "Reload service php-fpm "

		if [ ! -r $php_fpm_PID ] ; then
			echo "warning, no pid file found - php-fpm is not running ?"
			exit 1
		fi

		kill -USR2 `cat $php_fpm_PID`

		echo " done"
	;;

	configtest)
		$php_fpm_BIN -t
	;;

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

esac

 

[root@localhost ~]# chkconfig --add php-fpm
[root@localhost ~]# /etc/init.d/php-fpm status
php-fpm (pid 11946) is running...

试试重启功能

[root@localhost ~]# ps -ef | grep php
root     11946     1  0 13:49 ?        00:00:00 php-fpm: master process (/opt/php-7.1.11/etc/php-fpm.conf)
www      19035 11946  3 14:54 ?        00:00:11 php-fpm: pool www           
www      19121 11946  3 14:55 ?        00:00:09 php-fpm: pool www           
www      19271 11946  3 14:57 ?        00:00:07 php-fpm: pool www           
root     19685 18349  0 15:00 pts/7    00:00:00 grep php
[root@localhost ~]# /etc/init.d/php-fpm restart
Gracefully shutting down php-fpm . done
Starting php-fpm  done
[root@localhost ~]# 
[root@localhost ~]# 
[root@localhost ~]# ps -ef | grep php
root     19693     1  0 15:00 ?        00:00:00 php-fpm: master process (/opt/php-7.1.11/etc/php-fpm.conf)                                                                      
www      19694 19693  3 15:00 ?        00:00:00 php-fpm: pool www                                                                                                               
www      19695 19693  3 15:00 ?        00:00:00 php-fpm: pool www                                                                                                               
root     19697 18349  0 15:00 pts/7    00:00:00 grep php

重启成功

你可能感兴趣的:(PHP)