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

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

原创 2016年10月14日 22:48:36

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

chkconfig --list
  • 1

这里写图片描述

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

2.设置开机启动:

chkconfig httpd on
  • 1

再次查看结果:

这里写图片描述

3.如果在列表里找不到要启动的服务,则手动添加上去:

chkconfig --add sveserve
  • 1

4.取消开机启动:

chkconfig httpd off
  • 1

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

/etc/rc.d/init.d/mysqld start
...
  • 1
  • 2

设置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 $? 
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81

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

posted @ 2017-11-03 11:49  詹老师  阅读(214)  评论(0编辑  收藏  举报