• 转:CentOS设置程序开机自启动的方法


    转自:http://www.centos.bz/2011/09/centos-setup-process-startup-boot/

    1、把启动程序的命令添加到/etc/rc.d/rc.local文件中,比如下面的是设置开机启动httpd。

    复制代码
    #!/bin/sh
    #
    # This script will be executed *after* all the other init scripts.
    # You can put your own initialization stuff in here if you don't
    # want to do the full Sys V style init stuff.
     
    touch /var/lock/subsys/local
    /usr/local/apache/bin/apachectl start
    复制代码

    2、把写好的启动脚本添加到目录/etc/rc.d/init.d/,然后使用命令chkconfig设置开机启动。

    /etc/rc.d/init.d与/etc/init.d关系

    /etc/init.d是跟软连接,指向/etc/rc.d/init.d,用ll可以看出来)

    chkconfig 功能说明:检查,设置系统的各种服务。

    语法:chkconfig [--add][--del][--list][系统服务] 或 chkconfig [--level <等级代号>][系统服务][on/off/reset]

    --add 添加服务

    --del 删除服务

    --list 查看各服务启动状态

    比如我们设置自启动mysql:

    复制代码
     1 #将mysql启动脚本放入所有脚本运行目录/etc/rc.d/init.d中
     2 cp /lamp/mysql-5.0.41/support-files/mysql.server /etc/rc.d/init.d/mysqld
     3 
     4 #改变权限
     5 chown root.root /etc/rc.d/init.d/mysqld
     6 
     7 #所有用户都可以执行,单只有root可以修改
     8 chmod 755 /etc/rc.d/init.d/mysqld
     9 
    10 #将mysqld 放入linux启动管理体系中
    11 chkconfig --add mysqld
    12 
    13 #查看全部服务在各运行级状态
    14 chkconfig --list mysqld
    15 
    16 #只要运行级别3启动,其他都关闭
    17 chkconfig --levels 245 mysqld off
    复制代码

    例如:我们把httpd的脚本写好后放进/etc/rc.d/init.d/目录,使用

    chkconfig --add httpd
    chkconfig httpd on

    命令即设置好了开机启动。

    3、把启动程序的命令添加到/etc/rc.d/rc.sysinit 文件中

    脚本/etc/rc.d/rc.sysinit,完成系统服务程序启动,如系统环境变量设置、设置系统时钟、加载字体、检查加载文件系统、生成系统启动信息日志文件等

    比如我们设置自启动apache:

    echo "/usr/local/apache2/bin/apachectl start" >> /etc/rc.d/rc.sysinit



    redis开机启动
    http://blog.csdn.net/mlks_2008/article/details/19001595
    # chkconfig:   2345 90 10
    
    # description:  Redis is a persistent key-value database
    
    PATH=/usr/local/bin:/sbin:/usr/bin:/bin
       
    REDISPORT=6399
    EXEC=/opt/redis/redis-server
    REDIS_CLI=/opt/redis/redis-cli
       
    PIDFILE=/var/run/redis.pid
    CONF="/etc/redisMy.conf"
       
    case "$1" in
        start)
            if [ -f $PIDFILE ]
            then
                    echo "$PIDFILE exists, process is already running or crashed"
            else
                    echo "Starting Redis server..."
                    $EXEC $CONF
            fi
            if [ "$?"="0" ] 
            then
                  echo "Redis is running..."
            fi
            ;;
        stop)
            if [ ! -f $PIDFILE ]
            then
                    echo "$PIDFILE does not exist, process is not running"
            else
                    PID=$(cat $PIDFILE)
                    echo "Stopping ..."
                    $REDIS_CLI -p $REDISPORT SHUTDOWN
                    while [ -x ${PIDFILE} ]
                   do
                        echo "Waiting for Redis to shutdown ..."
                        sleep 1
                    done
                    echo "Redis stopped"
            fi
            ;;
       restart|force-reload)
            ${0} stop
            ${0} start
            ;;
      *)
        echo "Usage: /etc/init.d/redis {start|stop|restart|force-reload}" >&2
            exit 1
    esac
    ##############################

    service redis does not support chkconfig的解决办法

    问题解决办法如下:

    必须把下面两行注释放在/etc/init.d/redis文件靠前的注释中:

    # chkconfig:   2345 90 10

    # description:  Redis is a persistent key-value database

    上面的注释的意思是,redis服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10。




    【centos7】添加开机启动服务/脚本

    一、添加开机自启服务

    在centos7中添加开机自启服务非常方便,只需要两条命令(以Jenkins为例):

    systemctl enable jenkins.service #设置jenkins服务为自启动服务
    sysstemctl start  jenkins.service #启动jenkins服务

    二、添加开机自启脚本

    在centos7中增加脚本有两种常用的方法,以脚本autostart.sh为例:

    #!/bin/bash
    #description:开机自启脚本
    /usr/local/tomcat/bin/startup.sh  #启动tomcat

    方法二

    1、将脚本移动到/etc/rc.d/init.d目录下

    mv  /opt/script/autostart.sh /etc/rc.d/init.d

    2、增加脚本的可执行权限

    chmod +x  /etc/rc.d/init.d/autostart.sh

    3、添加脚本到开机自动启动项目中

    cd /etc/rc.d/init.d
    chkconfig --add autostart.sh
    chkconfig autostart.sh on

    以上两种方法均已在centos7系统上验证过,如有疑问,欢迎大家评论留言

    提示

    service redis does not support chkconfig的解决办法

    问题解决办法如下:

    必须把下面两行注释放在/etc/init.d/redis文件靠前的注释中:

    # chkconfig:   2345 90 10

    # description:  Redis is a persistent key-value database

         

    上面的注释的意思是,redis服务必须在运行级2,3,4,5下被启动或关闭,启动的优先级是90,关闭的优先级是10。

    
    
     

    方法一

    1、赋予脚本可执行权限(/opt/script/autostart.sh是你的脚本路径)

    chmod +x /opt/script/autostart.sh 

    2、打开/etc/rc.d/rc/local文件,在末尾增加如下内容

    /opt/script/autostart.sh 

    3、在centos7中,/etc/rc.d/rc.local的权限被降低了,所以需要执行如下命令赋予其可执行权限

    chmod +x /etc/rc.d/rc.local


  • 相关阅读:
    Sqoop
    Mediawiki
    TextMate 通用快捷键
    Wind7外接显示器选择拓展模式后,鼠标只能往右移动才能切换到外接显示器上,不能修改切换方向
    用Nginx+Lua(OpenResty)开发高性能Web应用
    netty4.0.x源码分析—bootstrap
    mysql分组合并GROUP_CONCAT
    Only POT texture can be compressed to PVRTC format
    手机屏幕左下角显示Fastboot mode是什么情况?
    判断UNITY版本号
  • 原文地址:https://www.cnblogs.com/youxin/p/5044633.html
Copyright © 2020-2023  润新知