简单的service编写 nginx php-fpm


nginx


几个方法

start 直接启动 查看返回值是否是0 如果0则success 非0 则failure

stop 直接调用killproc

reload 调用nginx -s reload

restart stop,start

test 调用nginx -t

status 直接调用status


几个方法killproc, status, success, failure 来自/etc/init.d/functions   详细方法: /etc/init.d/functions说明

#!/bin/bash
#/etc/init.d/nginx

# Source function library.
if [ -f /etc/init.d/functions ] ; then
  . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
  . /etc/rc.d/init.d/functions
else
  exit 1
fi

#config
service='/opt/Soft/nginx/sbin/nginx'
service_name='nginx'

#functions
start()
{
        echo 'start service'
        echo "${service}"
        ${service}
        if [ "$?" -eq 0 ] ; then
                success
        else
                failure
        fi
}
stop()
{
        echo 'stop service'
        echo "killproc ${service_name}"
        killproc ${service_name}
}
reload()
{
        echo 'reload service'
        echo "${service} -s reload"
        ${service} -s reload
        if [ "$?" -eq 0 ] ; then
                success
        else
                failure
        fi
}
test()
{
        echo 'test service'
        echo "${service} -t"
        ${service} -t
}


case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
reload)
    reload
    ;;
restart)
    stop
    start
    ;;
test)
    test
    ;;
status)
    status ${service_name}
    ;;
*)
    echo "usage: $0 start|stop|reload|restart|test|status"
    exit 0;
esac




php-fpm

start , stop ,restart|reload, status

#!/bin/bash

# Source function library.
if [ -f /etc/init.d/functions ] ; then
  . /etc/init.d/functions
elif [ -f /etc/rc.d/init.d/functions ] ; then
  . /etc/rc.d/init.d/functions
else
  exit 1
fi

#config
service='/opt/Soft/php/sbin/php-fpm'
service_name='php-fpm'

#functions
start()
{
        echo 'start service'
        echo "${service}"
        ${service}
        if [ "$?" -eq 0 ] ; then
                success
        else
                failure
        fi
}
stop()
{
        echo 'stop service'
        echo "killproc ${service_name}"
        killproc ${service_name}
}


case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
reload|restart)
    stop
    start
    ;;
status)
    status ${service_name}
    ;;
*)
    echo "usage: $0 start|stop|reload|restart|status"
    exit 0;
esac

根据正在运行的服务进行添加


#!/bin/bash
#author zhouwei <http://blog.csdn.net/sunvince>
#replace service path and move to /etc/init.d
#need template nginxservice,php-fpmservice,apacheservice

nginx=$(lsof | grep txt | grep nginx | awk '{print $NF}' | sort | uniq | grep nginx);
echo $nginx
if [ ! -z $nginx ] ; then
        #addslashes  / => \/
        nginx=$(echo ${nginx} | sed "s/\//\\\\\//g");
        sed "s/NGINXSERVICE/${nginx}/" nginxservice > nginx
        chmod +x nginx
        mv nginx /etc/init.d/
fi

phpfpm=$(lsof |grep txt | grep php-fpm | awk '{print $NF}' | sort |uniq | grep php-fpm);
echo $phpfpm
if [ ! -z $phpfpm ] ; then
        #addslashes  / => \/
        phpfpm=$(echo ${phpfpm} | sed "s/\//\\\\\//g");
        sed "s/PHPFPMSERVICE/${phpfpm}/" php-fpmservice > php-fpm
        chmod +x php-fpm
        mv php-fpm /etc/init.d/
fi

apache=$(lsof |grep txt | grep httpd | awk '{print $NF}' | sort |uniq | grep httpd);
if [ ! -z $apache ] ; then
        apache=$(echo $apache | awk -F"/bin/httpd" '{print $1}')/bin/apachectl
fi
echo $apache
if [ ! -z $apache ] ; then
        #addslashes  / => \/
        apache=$(echo ${apache} | sed "s/\//\\\\\//g");
        sed "s/APACHESERVICE/${apache}/" apacheservice > apache
        chmod +x apache
        mv apache /etc/init.d/
fi


todo:等有时间写个基于pid 和 conf的... 这样就能支持多php-fpm 了,这玩意儿先凑合用着


  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值