当前位置 博文首页 > _NVIDIA Jetson xavier NX设置开机启动脚本_cumtchw:Ubuntu18.0

    _NVIDIA Jetson xavier NX设置开机启动脚本_cumtchw:Ubuntu18.0

    作者:[db:作者] 时间:2021-07-12 18:41

    目录

    1.修改/etc/systemd/system/rc-local.service,在后面增加install区块.

    2.创建并修改rc.local文件

    3.测试


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

    1.修改/etc/systemd/system/rc-local.service,在后面增加install区块.

    sudo vim /etc/systemd/system/rc-local.service

    打开文件如下:

    #  SPDX-License-Identifier: LGPL-2.1+
    #
    #  This file is part of systemd.
    #
    #  systemd is free software; you can redistribute it and/or modify it
    #  under the terms of the GNU Lesser General Public License as published by
    #  the Free Software Foundation; either version 2.1 of the License, or
    #  (at your option) any later version.
    
    # This unit gets pulled automatically into multi-user.target by
    # systemd-rc-local-generator if /etc/rc.local is executable.
    [Unit]
    Description=/etc/rc.local Compatibility
    Documentation=man:systemd-rc-local-generator(8)
    ConditionFileIsExecutable=/etc/rc.local
    After=network.target
    
    [Service]
    Type=forking
    ExecStart=/etc/rc.local start
    TimeoutSec=0
    RemainAfterExit=yes
    GuessMainPID=no
    

    配置文件解析(主要分成三个部分):

    [Unit] 段: 启动顺序与依赖关系
    [Service] 段: 启动行为,如何启动,启动类型
    [Install] 段: 定义如何安装这个配置文件,即怎样做到开机启动

    可以看出,/etc/rc.local 的启动顺序是在网络后面,但是显然它少了 Install 段,也就没有定义如何做到开机启动,所以显然这样配置是无效的。?因此我们就需要在后面帮他加上 [Install] 段:

    [Install]  
    WantedBy=multi-user.target  
    Alias=rc-local.service

    修改后的文件内容如下:

    #  SPDX-License-Identifier: LGPL-2.1+
    #
    #  This file is part of systemd.
    #
    #  systemd is free software; you can redistribute it and/or modify it
    #  under the terms of the GNU Lesser General Public License as published by
    #  the Free Software Foundation; either version 2.1 of the License, or
    #  (at your option) any later version.
    
    # This unit gets pulled automatically into multi-user.target by
    # systemd-rc-local-generator if /etc/rc.local is executable.
    [Unit]
    Description=/etc/rc.local Compatibility
    Documentation=man:systemd-rc-local-generator(8)
    ConditionFileIsExecutable=/etc/rc.local
    After=network.target
    
    [Service]
    Type=forking
    ExecStart=/etc/rc.local start
    TimeoutSec=0
    RemainAfterExit=yes
    GuessMainPID=no
    
    [Install]
    WantedBy=multi-user.target
    Alias=rc-local.service
    

    2.创建并修改rc.local文件

    sudo vim /etc/rc.local

    在里面添加如下内容,注意第一行要用bash,不要用sh:

    #!/bin/bash -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.
    
    
    
    #这里就是我自己添加的启动脚本
    bash /data/chw/temp/auto.sh
    
    
    
    exit 0
    

    加入系统程序中:

    sudo systemctl enable rc-local   #这条语句就是创建一个超链接,在系统启动服务程序中.

    给文件赋予可执行权限

    sudo chmod +x /etc/rc.local

    3.测试

    我的/data/chw/temp/auto.sh脚本里面的内容是:

    #!/bin/bash
    
    cd /data/chw/temp
    python main.py > ./log.txt

    然后我的main.py里面的内容是:

    # -*- coding:utf-8 -*-
    
    import sys
    while 1:
        print("this is a auto sh,")
        print("==================")

    如果想不重启,直接查看效果执行如下命令即可,启动服务并检查状态,如果修改了rc-local.service文件,则需要用sudo systemctl daemon-reload重新加载.:

    sudo systemctl daemon-reload     
    sudo systemctl stop   rc-local.service
    sudo systemctl start  rc-local.service
    sudo systemctl status rc-local.service

    如果想重启看下效果:

    sudo init 6         #重启命令
    sudo reboot -h now  #重启命令

    参考文献:

    ubuntu18.04中如何设置开机启动脚本,开机启动命令??https://blog.csdn.net/xiaoxiao_yingzi/article/details/100520171

    ubuntu18.04 设置开机启动脚本? https://www.jianshu.com/p/85039842d318

    cs