• ubuntu设置脚本开机自启动


    rc-local.service
    flexmi@td1:~$ cd /lib/systemd/system/
    flexmi@td1:/lib/systemd/system$ ls |grep rc-local.service
    rc-local.service
    rc-local.service.d
    flexmi@td1:/lib/systemd/system$ pwd
    /lib/systemd/system
    flexmi@td1:/lib/systemd/system$
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    确保有rc-local.service文件,没有手动添加,内容如下:

    #flexmi@td1:/lib/systemd/system$ cat ./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
    
    • 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
    rc.local

    找到rc.local文件,没有就创建在/etc目录下,内容为你需要执行的脚本内容或者你需要执行的脚本启动命令

    flexmi@td1:/lib/systemd/system$ cd /etc
    flexmi@td1:/etc$ ls |grep rc.local
    rc.local
    flexmi@td1:/etc$ cat ./rc.local
    #!/bin/bash
    cd /home/flexmi/cmss
    sudo ./start.sh
    exit 0
    flexmi@td1:/etc$
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9

    注意要给rc.local脚本文件添加执行权限

    sudo chmod +x ./rc.local
    
    • 1
    启动服务
    sudo systemctl enable rc-local
    
    • 1
    sudo systemctl start rc-local.service
    
    • 1
    sudo systemctl status rc-local.service
    
    • 1
    注意
    1. 如果服务启动失败,看看是不是rc.local的问题,脚本的开头一定要是 #!/bin/bash
    2. rc.local脚本的后面一定要加上exit 0

    下面是我在操作过程中的截图:
    启动失败:
    在这里插入图片描述
    启动成功
    在这里插入图片描述

  • 相关阅读:
    Vue组件之间的通信-父传子-子传父
    SpringBoot
    《计算机英语》 Unit 3 Software Engineering 软件工程
    成本高、落地难、见效慢,开源安全怎么办?
    HSIC Bottleneck的C++程序实现
    webpack output.library的16 种取值方法示例
    python数据结构 操作指南(列表、元组、字典、集合)
    5-if语句(选择结构)
    数据结构零基础入门篇(C语言实现)
    PING命令中的-r参数的原理分析
  • 原文地址:https://blog.csdn.net/m0_46636892/article/details/134414890