zoukankan      html  css  js  c++  java
  • Ubuntu18.04 开机自启动(需要 sudo 权限)

    前言

    ubuntu作为服务器使用时,常常需要在机器重启时能自动启动我们开发的服务。

    Ubuntu 18.04不再使用initd管理系统,改用systemd,包括用systemctl命令来替换了service和chkconfig的功能。

    systemd 默认读取 /etc/systemd/system 下的配置文件,该目录下的文件会链接/lib/systemd/system/下的文件。

    不同于以往的版本,ubuntu18.04默认不带/etc/rc.local文件,我们需要通过配置来让rc.local.service生效。

    然后我们就可以像以前那样,直接把启动脚本写入/etc/rc.local文件,这样机器启动时就会自动运行它。

    正文

    以下操作最好在 root 账号权限下操作

    1. 检查源文件 rc-local.service

    检查系统目录/lib/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
    
    

    这是我们需要添加【install】,添加后的内容为:

    #  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
    

    这里我们可以看到添加了这些内容

    ####  需要自己添加
    [Install]
    WantedBy=multi-user.target
    Alias=rc-local.service
    

    2. 检查 软链接 rc-local.service

    systemd 默认读取 /etc/systemd/system 下的配置文件,该目录下的文件会链接 /lib/systemd/system/下的文件。

    查看 /etc/systemd/system 目录下是否有 rc-local.service 文件,如果有,确认是否为 /lib/systemd/system/ 目录下的软链接

    如果没有则创建软连接

    ln -s /lib/systemd/system/rc-local.service /etc/systemd/system/rc-local.service
    

    3. 创建 /etc/rc.local 脚本文件

    rc.local 文件默认没有创建,我们直接创建并赋值最高权限

    sudo touch /etc/rc.local 
    chmod 777 /etc/rc.local 
    
    

    在 /etc/rc.local 里面我们就可以写入我们想让服务器开机时运行的命令,比如 echo "Hello Shuiche" > /usr/local/text.log

    4. 启用rc.local脚步

    这里需要注意,在启用脚步时,必须保证脚步有内容,并且是一个课执行脚步。不然会启用失败

    sudo systemctl enable rc-local
    

    5. 启动服务并查看状态

    sudo systemctl start rc-local.service
    sudo systemctl status rc-local.service
    

    6. 重启机器验证

    reboot
    
    博客中所涉及到的图片都有版权,请谨慎使用
  • 相关阅读:
    c++实验9 图及图的操作实验
    Html/CSS 示例演练 图书馆后台界面
    c++实验8 哈夫曼编码-译码器
    Html/CSS 初步介绍html和css部分重要标签
    c++实验7 二叉树
    JavaWeb -学生信息管理实践(JDBC+web+三层架构+DBUtil构造思路)
    java 实验6 图形用户界面设计试验(2)
    操作表与操作表数据行
    数据表操作练习
    数据库的介绍:
  • 原文地址:https://www.cnblogs.com/shuiche/p/15481924.html
Copyright © 2011-2022 走看看