centos通过系统服务设置http、mysql等开机启动

在我们操作服务器时,经常因为各种原因,需要重启服务器,重启后发现网站不能正常打开,这是,经常是因为某些服务没有跟着服务器重启而重启,整理了下把服务加入开机启动的命令,如下:
1.查看开机启动项:

chkconfig --list

冯奎博客
这里看到httpd和mysqld未设置开机自动启动
2.设置开机启动:

chkconfig httpd on
chkconfig mysqld on

再次查看结果:
冯奎博客
3.如果在列表里找不到要启动的服务,则手动添加上去:

chkconfig --add sveserve

4.取消开机启动:

chkconfig httpd off

还有一种方式,直接修改/etc/rc.d/rc.local 文件,添加命令:

/etc/rc.d/init.d/mysqld start
...

设置memcached为开机启动项:
在/etc/rc.d/init.d/下新建脚本文件,命名为memcached,把以下代码copy进去保存

#! /bin/bash  
#  
# memcached     start/stop the memcached daemon  
#  
# chkconfig: 35 80 70  
# description: memcached is a memory cache server.  
#  

prog="memcached"  
exec=/usr/local/memcached/bin/memcached  
lockfile=/var/lock/subsys/memcached  

# source function library.  
. /etc/rc.d/init.d/functions  

start() {  
    if [ $UID -ne 0 ]; then  
        echo "User has insufficient privilege."  
        exit 4  
    fi  
    [ -x $exec ] || exit 5  
    echo -n $"starting $prog: "  
    daemon $exec -u root -d -P /var/run/memcached.pid  
    retval=$?  
    echo  
    [ $retval -eq 0 ] && touch $lockfile  
}  

stop() {  
    if [ $UID -ne 0 ]; then  
        echo "User has insufficient privilege."  
        exit 4  
    fi  
    echo -n $"Stopping $prog: "  
        if [ -n "`pidfileofproc $exec`" ]; then  
                killproc $exec  

        else  
                failure $"stopping $prog"  
        fi  
    retval=$?  
    echo  
    [ $retval -eq 0 ] && rm -f $lockfile  
}  

restart() {  
    stop  
    start  
}  

rh_status() {  
    # run checks to determine if the service is running or use generic status  
    status $prog  
}  

rh_status_q() {  
    rh_status >/dev/null 2>&1  
}  

case "$1" in  
    "start")  
        rh_status_q && exit 0  
        $1  
        ;;  
    "stop")  
        rh_status_q || exit 0  
        $1  
        ;;  
    "restart")  
        rh_status_q || exit 7  
        $1  
        ;;  
    "status")  
        rh_status  
        ;;  
    *)  
        echo $"Usage: $0 {start|stop|status|restart}"  
        exit 2  
        ;;  
esac  
exit $? 

添加到开机启动项:#chkconfig --add memcached
查看启动项:#chkconfig --list memcached

冯奎博客
请先登录后发表评论
  • latest comments
  • 总共0条评论