• Ubuntu 18.04 设置开机启动脚本 rc.local systemd


    ubuntu18.04不再使用initd管理系统,改用systemd

    ubuntu-18.04不能像ubuntu14一样通过编辑rc.local来设置开机启动脚本,通过下列简单设置后,可以使rc.local重新发挥作用。

    1.建立rc-local.service文件

    $ sudo vi /etc/systemd/system/rc-local.service

    2.复制以下内容

    [Unit]
    Description=/etc/rc.local Compatibility
    ConditionPathExists=/etc/rc.local
     
    [Service]
    Type=forking
    ExecStart=/etc/rc.local start
    TimeoutSec=0
    StandardOutput=tty
    RemainAfterExit=yes
    SysVStartPriority=99
     
    [Install]
    WantedBy=multi-user.target
    1) [Unit] 区块:启动顺序与依赖关系。
    
    2) [Service] 区块:启动行为,如何启动,启动类型。
    
    3) [Install] 区块,定义如何安装这个配置文件,即怎样做到开机启动。
    有关说明

    3. 创建我们自定义的启动脚本(以后自己的脚本在这里面添加即可)

    $ sudo vi /etc/rc.local

    #!/bin/sh -e
    #
    # rc.local
    #
    # This script is executed at the end of each multiuser runlevel.
    # Make sure that the script will "exit 0" on success or any other
    # value on error.
    #
    # In order to enable or disable this script just change the execution
    # bits.
    #
    # By default this script does nothing.
    touch /home/xxx/`date +%Y-%m-%d`.log 
    echo "`df`" >  /home/xxx/`date +%Y-%m-%d`.log  
    echo "看到这行字,说明添加自启动脚本成功。" > /usr/local/test.log
    exit 0
    #!/bin/sh -e
    
    -e 参数可以让脚本执行遇到错误就不往下执行
    有关说明
    $ sudo chmod +x /etc/rc.local && sudo systemctl enable rc-local
    Created symlink /etc/systemd/system/multi-user.target.wants/rc-local.service → /etc/systemd/system/rc-local.service.
    

    启动服务,并检查服务(非必要)

    $ sudo systemctl start rc-local.service
    $ sudo systemctl status rc-local.service
    
     rc-local.service - /etc/rc.local Compatibility
       Loaded: loaded (/etc/systemd/system/rc-local.service; enabled; vendor preset:
      Drop-In: /lib/systemd/system/rc-local.service.d
               └─debian.conf
       Active: active (exited) since Fri 2019-01-25 09:31:07 CST; 9s ago
      Process: 17588 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)
    
    Jan 25 09:31:07 SCHIPS systemd[1]: Starting /etc/rc.local Compatibility...
    Jan 25 09:31:07 SCHIPS systemd[1]: Started /etc/rc.local Compatibility.
    



    参考:

    https://www.jianshu.com/p/79d24b4af4e5

    https://www.cnblogs.com/defifind/p/9285456.html

  • 相关阅读:
    从Linux内核中获取真随机数【转】
    linux下pthread_cancel无法取消线程的原因【转】
    LINUX-内核-中断分析-中断向量表(3)-arm【转】
    ARM中断向量表与响应流程【转】
    小任务与工作队列的区别【转】
    GNU Readline 库及编程简介【转】
    Linux 内核同步之自旋锁与信号量的异同【转】
    Professional Linux Kernel Architecture 笔记 —— 中断处理(Part 2)【转】
    Linux中断(interrupt)子系统之二:arch相关的硬件封装层【转】
    浅谈C语言中的强符号、弱符号、强引用和弱引用【转】
  • 原文地址:https://www.cnblogs.com/schips/p/10318078.html
Copyright © 2020-2023  润新知