nginx php-fpm memcached 均為編譯安裝,将腳本統一放置在/etc/init.d/ 下,形成啟動服務的标準化。
一、編譯安裝後nginx啟動、停止有些麻煩,将下面内容添加到/etc/init.d/nginx中,作為nginx的服務啟動。
#!/bin/bash
#chkconfig: 345 85 15
#description:It is used to serve
PATH=/usr/local/nginx/sbin:$PATH
export PATH
# check if root account
if [ $(id -u) != "0" ];then
printf "Error:You must be root account!\n"
exit 1
fi
#Define environment variables
NGINX_PID=/usr/local/nginx/logs/nginx.pid
NGINX_DAEMON=/usr/local/nginx/sbin/nginx
#Function define
fun_start()
{
printf "Starting nginx ...\n"
if [ -f $NGINX_PID ];then
printf "Nginx is running!\n"
exit 1
else
$NGINX_DAEMON
printf "Nginx start successfully!\n"
fi
}
fun_stop()
printf "Stoping Nginx...\n"
kill $(cat $NGINX_PID)
printf "Nginx is stopped!\n"
else
printf "Nginx is not running!\n"
fun_reload()
printf "Reloading Nginx...\n"
if [ -f $NGINX_PID ];then
$NGINX_DAEMON -s reload
else
printf "Nginx is not running!\n"
fi
fun_restart()
printf "Restarting Nginx..."
kill $(cat $NGINX_PID)
$NGINX_DAEMON
fun_status()
printf "Nginx is running!\n"
printf "Nginx is stop!\n"
case "$1" in
start)
fun_start
;;
stop)
fun_stop
restart)
reload)
fun_reload
status)
fun_status
*)
printf "Usage:Only {start|stop|restart|reload|status}"
esac
exit
------------- < nginx script END> --------------
說明:腳本中紅色字型部分為将nginx添加為linux系統服務必須添加的語句,否則無法添加成功。
紅色字型的意思為:啟動級别 | 啟動優先級 | 停止優先級
1、将nginx賦予執行權限,放置在/etc/init.d/下
2、chkconfig --add nginx
二、将php-fpm的啟動腳本放置在/etc/init.d下
ln -s /usr/local/php5/sbin/php-fpm /etc/init.d/php-fpm
三、memcached啟動腳本
路徑:/etc/init.d/memcached
賦予執行權限
#!/bin/bash
memcache_prog=$(ps -ef | grep memcached | grep -v grep | wc -l)
memcache_pid=$(ps -ef | grep memcached | grep -v grep | awk '{print $2}')
mem=50
user=root
port=12000
if [ $memcache_prog != "0" ];then
printf "memcached is running !\n"
memcached -d -m $mem -u $user -p $port
printf "memcached is started!\n"
if [ $memcache_prog = "0" ];then
printf "memcached is not running !\n"
kill $memcache_pid
printf "memcached is stopped\n"
printf "memcached is stopped!\n"
start)
;;
stop)
restart)
status)
fun_status
;;
*)
printf "Usage:Only {start | stop | restart | status }\n"
exit
------------- <memcached script END> ---------------------