32ew 发表于 2014-11-10 08:25:46

将编译安装的Apache添加到服务启动

安装Apache的时候,大家可能采用最多的就是编译安装,安装完成以后发现启动的时候总是需要通过完整路径启动,这里介绍一个方法,将所有类编译安装的软件添加服务启动
这里以Apache为例
正常情况下启动Apache命令为:
# /usr/local/apache2/bin/apachectl start
#
#
#
# ps -eaf|grep httpd
root   26857   10 20:08 ?      00:00:00 /usr/local/apache2//bin/httpd -k start
daemon   26859 268570 20:08 ?      00:00:00 /usr/local/apache2//bin/httpd -k start
daemon   26860 268570 20:08 ?      00:00:00 /usr/local/apache2//bin/httpd -k start
daemon   26861 268570 20:08 ?      00:00:00 /usr/local/apache2//bin/httpd -k start
daemon   26862 268570 20:08 ?      00:00:00 /usr/local/apache2//bin/httpd -k start
daemon   26863 268570 20:08 ?      00:00:00 /usr/local/apache2//bin/httpd -k start
root   26865 258530 20:08 pts/1    00:00:00 grep httpd
#
我这里已经起来了,关闭的时候使用
# /usr/local/apache2/bin/apachectl stop
但是不想每次都输入这么长一串命令启动怎么办呢?当然最简单就是通过service apached start|stop|restart来启动停止和重启服务了
具体步骤如下:
vi /etc/rc.d/init.d/apached 添加如下内容

#!/bin/bash
# chkconfig: 345 90 20
# description: Activates/Deactivates Apache Web Server
start()
{
    echo "start apached"
    /usr/local/apache2/bin/apachectl start &
    exit 0;
}
stop()
{
    ps -eaf|grep httpd|grep -v grep|awk '{print $2}'|xargs kill -9
    echo "stop apached"
}
case "$1" in
start)
    start
    ;;
stop)
    stop
    ;;
restart)
    stop
    start
    ;;
*)
    echo "usage: $0 start|stop|restart"
    exit 0;
esac
这一段代码即可轻松搞定,然后就是chmod 755 /etc/rc.d/init.d/apached为其添加执行权限
最好chkconfig --add apached添加到服务即可

测试:
# ps -eaf|grep httpd|grep -v grep|awk '{print $2}'   
26904
26906
26907
26908
26909
26910
#
# service apached stop
stop apached
# ps -eaf|grep httpd|grep -v grep|awk '{print $2}'
#
#
#
# service apached start
start apached
# ps -eaf|grep httpd|grep -v grep|awk '{print $2}'
26940
26942
26943
26944
26945
26946

页: [1]
查看完整版本: 将编译安装的Apache添加到服务启动