zoukankan      html  css  js  c++  java
  • Ubuntu 开机启动程序

    Ubuntu 的开机启动项都放在 /etc/init 下的 *.conf 文件

    以 /etc/init/nginx.conf 为例子

    description "nginx - small, powerful, scalable web/proxy server"
    
    start on filesystem and static-network-up      ## 在文件系统和网络启动后启动 nginx
    stop on runlevel [016]               ## 在 level 0, 1, 6 的时候停止 nginx
    
    expect fork     ## 监控 fork 一次后的 pid
                    ## expect daemonize 则是监控 fork 两次后的 pid (有的 daemon 程序会 fork 两次)
    
    respawn         ## 如果 nginx 失败了会自动重启
    
    pre-start script
            ## pre-start 步骤,检查 nginx 是否存在,通过 -q -t 检查 nginx 配置
            [ -x /usr/sbin/nginx ] || { stop; exit 0; }
            /usr/sbin/nginx -q -t -g 'daemon on; master_process on;' || { stop; exit 0; }
    end script
    
    ## 启动 nginx 的命令,可以有多个步骤放在 script...end script 内
    exec /usr/sbin/nginx -g 'daemon on; master_process on;'
    
    ## 停止 nginx 的命令
    pre-stop exec /usr/sbin/nginx -s quit
    
    

    其中有一个特殊的文件 rc.conf
    这是为了兼容早期的版本,早期的启动配置放在了 /etc/rc[runlevel].d 或者 /etc/rc.d/rc[runlevel].d

    # rc - System V runlevel compatibility
    #
    # This task runs the old System V-style rc script when changing between
    # runlevels.
    
    description     "System V runlevel compatibility"
    author          "Scott James Remnant <scott@netsplit.com>"
    
    emits deconfiguring-networking
    emits unmounted-remote-filesystems
    
    start on runlevel [0123456]
    stop on runlevel [!$RUNLEVEL]
    
    export RUNLEVEL
    export PREVLEVEL
    
    console output
    env INIT_VERBOSE
    
    task
    
    script
    if [ "$RUNLEVEL" = "0" -o "$RUNLEVEL" = "1" -o "$RUNLEVEL" = "6" ]; then
        status plymouth-shutdown 2>/dev/null >/dev/null && start wait-for-state WAITER=rc WAIT_FOR=plymouth-shutdown || :
    fi
    /etc/init.d/rc $RUNLEVEL
    end script
    
    

    可以看到实际上是启动了 /etc/init.d/rc RUNLEVEL 命令
    rc 命令会调用 /etc/rc$runlevel.d/ 下的程序,这些程序以 [S|K][level][name] 的规则命名
    其中 S 代表启动,K 代表停止,level 代表执行顺序,比如

    /etc/rc2.d/S02ssh
    /etc/rc2.d/S03docker
    

    这表示当 RUN LEVEL 是 2 的时候,先启动 ssh 再启动 docker

    进一步查看这些脚本发现都是链接

    S02ssh -> ../init.d/ssh
    S03docker -> ../init.d/docker
    

    实际上 rc 脚本的做法是遇到 S 脚本就传个 start,遇到 K 脚本就传个 stop,并按 level 的顺序执行

    相当于执行

    /etc/init.d/ssh start
    /etc/init.d/docker start
    

    不同系统的 run level 的含义不一样

    ubuntu 默认 run level 是 2,各 leve 含义是
    0 - Halt
    1 - Single
    2 - Full multi-user with display manager (GUI)
    3 - Full multi-user with display manager (GUI)
    4 - Full multi-user with display manager (GUI)
    5 - Full multi-user with display manager (GUI)
    6 - Reboot

    而 RedHat 的 run level 含义是
    0 - Halt
    1 - Single
    2 - Not used/User definable
    3 - Full multi-user NO display manager
    4 - Not used/User definable
    5 - Full multi-user with display manager (GUI)
    6 - Reboot

    还有一个 /etc/rcS.d 目录,应该是有一个 run level 是 S

    另外基本上每个 /etc/rc$runlevel.d/ 下都有像这样的命令

    S05rc.local -> ../init.d/rc.local
    

    而 /etc/init.d/rc.local 脚本会调用 /etc/rc.local 或 /etc/rc.d/rc.local

    而 /etc/rc.local 是空的,可以往里面添加命令,这样就可以在开机时执行

    添加开机启动项有以下方法

    • 直接添加到 rc.local
    • 建立 /etc/init.d/xxx 链接,然后再建立链接如 /etc/rc2.d/S99xxx -> ../init.d/xxx
    • 建立 /etc/init.d/xxx 链接,然后用命令 sudo update-rc.d xxx defaults 99 创建

    在 Ubuntu 18.04,开机启动的规则好像又不一样了



  • 相关阅读:
    mvc多级views目录
    JSP中文乱码总结
    sql查询指定表外键约束
    eclipse设置汉化
    c#序列化json字符串及处理
    获取需要登录认证的远程数据
    vs2013 括号自动配对样式设置
    c#中事物使用
    ListView遍历每个Item出现NullPointerException的异常
    gen目录无法更新,或者gen目录下的R.JAVA文件无法生成
  • 原文地址:https://www.cnblogs.com/moonlight-lin/p/13693999.html
Copyright © 2011-2022 走看看