centos把http、mysql等加入开机启动

有时因为各种原因,需要重启服务器,重启后发现网站打不开了,八成是服务没有开启,整理了下把服务加入开机启动的命令,如下:
1.查看开机启动项:

chkconfig --list

这里写图片描述

这里看到httpd和mysqld未设置开机自动启动

2.设置开机启动:

chkconfig httpd 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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值