无忧启动论坛

 找回密码
 注册
搜索
系统gho:最纯净好用系统下载站投放广告、加入VIP会员,请联系 微信:wuyouceo
查看: 1775|回复: 8
打印 上一主题 下一主题

systemd 之下启用 rc.local

  [复制链接]
跳转到指定楼层
1#
发表于 2022-7-23 19:48:25 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
本帖最后由 不点 于 2022-7-24 07:49 编辑

从英文网站搜到的,也懒得翻译了。相关知识点整理如下:

1、 rc.local 文件的位置:

/etc/rc.local (通常是这个)
/etc/rc.d/rc.local (在 Fedora、CentOS 之下可能是这个)

应确保 rc.local 是可执行文件。

2、检查 rc-local 服务是否已启用:

sudo systemctl is-enabled rc-local.service

3、检查 rc-local 服务的状态:

sudo systemctl status rc-local.service

4、启用 rc-local 服务:

sudo systemctl enable rc-local.service

5、启用 rc-local 服务之后,只要重启操作系统,就可以自动运行 rc.local 文件了。如果现在不想重启操作系统,那也可以执行如下命令来运行 rc.local 文件:

sudo systemctl start rc-local.service

6、systemd 自带了一个工具(可执行文件),但它没有在 PATH 的路径搜索中:

/lib/systemd/system-generators/systemd-rc-local-generator

猜测这可能只是让系统开发者内部使用的,而不是让终端用户使用的。它有一个 man page:

https://manpages.debian.org/bullseye/systemd/systemd-rc-local-generator.8.en.html




【补充】来自:https://blog.wijman.net/enable-rc-local-in-debian-bullseye/

Enable rc.local in Debian Bullseye
By Stephan Wijman   2021-12-03

With Debian 11 (Bullseye) there is no default rc.local file anymore. But this doesn't mean it has been completely removed.

The rc-local service is available but it will need a default config file.


Create a file /etc/rc.local with the following content:

  1. #!/bin/sh -e
  2. #
  3. # rc.local
  4. #
  5. # This script is executed at the end of each multiuser runlevel.
  6. # Make sure that the script will "exit 0" on success or any other
  7. # value on error.
  8. #
  9. # In order to enable or disable this script just change the execution
  10. # bits.
  11. #
  12. # By default this script does nothing.

  13. exit 0
复制代码

We now have to make the file executable with the following command:

chmod  +x  /etc/rc.local

Then we need to reload the systemd manager configuration:

systemctl  daemon-reload

Then we start the rc-local daemon:

systemctl  start  rc-local

And then we check the status of rc-local to confirm it ran OK:

systemctl  status  rc-local

You should then get an output like below stating that it is active and exited normally:

  1. ● rc-local.service - /etc/rc.local Compatibility
  2.      Loaded: loaded (/lib/systemd/system/rc-local.service; enabled-runtime; vendor preset: enabled)
  3.     Drop-In: /usr/lib/systemd/system/rc-local.service.d
  4.              └─debian.conf     Active: active (exited)
  5.        Docs: man:systemd-rc-local-generator(8)
  6.     Process: 58756 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)
复制代码

2#
发表于 2022-7-23 19:54:21 来自手机 | 只看该作者
各大serivce文件在/etc/systemd/system/目录和/usr/lib/systemd/system目录
回复

使用道具 举报

3#
发表于 2022-7-23 20:00:41 来自手机 | 只看该作者
前者有优先于后者。这些个文件大部分都是文本文件。用文本编辑器打开看看,里边有这个service内容,这些依赖那些模,在那些之后或者之前运行,运行命令,与那些命令冲突。initsysv是串行运行,systemd是并行运行的,。可以这样理解,systemd默认以default.target为目标,这个文件也是文本文件,看看它一来那些target,必须在那些target之后运行,找到这些一直往上逆向推回去,就知道启动了哪些脚本命令。
回复

使用道具 举报

4#
发表于 2022-7-23 20:12:30 | 只看该作者

点评

谢谢分享  详情 回复 发表于 2022-7-23 20:29
回复

使用道具 举报

5#
发表于 2022-7-23 20:29:00 | 只看该作者
回复

使用道具 举报

6#
发表于 2022-7-23 20:29:15 | 只看该作者
谢谢分享
回复

使用道具 举报

7#
发表于 2022-7-23 22:38:24 | 只看该作者
谢谢分享
回复

使用道具 举报

8#
 楼主| 发表于 2022-7-24 06:33:13 | 只看该作者
本帖最后由 不点 于 2022-7-24 06:36 编辑

转贴一篇中文的文章            https://blogs.vicsdf.com/article/44516

Debian 11 Bullseye 解决 rc.local 开机启动问题
2022-01-20  by  烧饼          关注作者

本文同样适用于 Debian 10 Buster 和 Debian 9 Strech。

由于某些软件并没有增加开启启动的服务,很多时候需要手工添加,一般我们都是推荐使用 systemd 写个系统服务,但是对于一些简单的脚本或者懒人来说,添加命令到 /etc/rc.local 文件更方便,但是自从 Debian 9 开始,Debian 默认不带 /etc/rc.local 文件,而 rc.local 服务却还是自带的:

  1. root@Debian ~ # cat /lib/systemd/system/rc-local.service
  2. #  SPDX-License-Identifier: LGPL-2.1-or-later##  This file is part of systemd.
  3. #
  4. #  systemd is free software; you can redistribute it and/or modify it
  5. #  under the terms of the GNU Lesser General Public License as published by
  6. #  the Free Software Foundation; either version 2.1 of the License, or
  7. #  (at your option) any later version.

  8. # This unit gets pulled automatically into multi-user.target by
  9. # systemd-rc-local-generator if /etc/rc.local is executable.
  10. [Unit]
  11. Description=/etc/rc.local Compatibility
  12. Documentation=man:systemd-rc-local-generator(8)
  13. ConditionFileIsExecutable=/etc/rc.local
  14. After=network.target

  15. [Service]
  16. Type=forking
  17. ExecStart=/etc/rc.local start
  18. TimeoutSec=0
  19. RemainAfterExit=yes
  20. GuessMainPID=no
复制代码

并且默认情况下这个服务还是关闭的状态:

  1. root@debian ~ # systemctl status rc-local
  2. ● rc-local.service - /etc/rc.local Compatibility
  3.      Loaded: loaded (/lib/systemd/system/rc-local.service; static)
  4.     Drop-In: /usr/lib/systemd/system/rc-local.service.d
  5.              └─debian.conf
  6.      Active: inactive (dead)
  7.        Docs: man:systemd-rc-local-generator(8)
复制代码

为了解决这个问题,我们需要手工添加一个 /etc/rc.local 文件:

  1. cat <<EOF >/etc/rc.local
  2. #!/bin/sh -e
  3. #
  4. # rc.local
  5. #
  6. # This script is executed at the end of each multiuser runlevel.
  7. # Make sure that the script will "exit 0" on success or any other# value on error.
  8. #
  9. # In order to enable or disable this script just change the execution
  10. # bits.
  11. #
  12. # By default this script does nothing.

  13. exit 0
  14. EOF
复制代码

然后赋予权限:

  1. chmod  +x  /etc/rc.local
复制代码

接着启动 rc-local 服务:

  1. systemctl  start  rc-local
  2. systemctl  enable  rc-local
复制代码

此时可能会弹出警告:

  1. The unit files have no installation config (WantedBy=, RequiredBy=, Also=,
  2. Alias= settings in the [Install] section, and DefaultInstance= for template
  3. units). This means they are not meant to be enabled using systemctl.

  4. Possible reasons for having this kind of units are:
  5. • A unit may be statically enabled by being symlinked from another unit's
  6.   .wants/ or .requires/ directory.
  7. • A unit's purpose may be to act as a helper for some other unit which has
  8.   a requirement dependency on it.
  9. • A unit may be started when needed via activation (socket, path, timer,
  10.   D-Bus, udev, scripted systemctl call, ...).
  11. • In case of template units, the unit is meant to be enabled with some
  12.   instance name specified.
复制代码

无视警告,因为这个服务没有任何依赖的系统服务,只是开机启动 /etc/rc.local 脚本而已。再次查看状态:
  1. root@debian ~ # systemctl status rc-local.service
  2. ● rc-local.service - /etc/rc.local Compatibility
  3.      Loaded: loaded (/lib/systemd/system/rc-local.service; enabled-runtime; vendor preset: enabled)
  4.     Drop-In: /usr/lib/systemd/system/rc-local.service.d
  5.              └─debian.conf
  6.      Active: active (exited) since Thu 2022-01-27 18:52:43 UTC; 10s ago
  7.        Docs: man:systemd-rc-local-generator(8)
  8.     Process: 541 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)
  9.         CPU: 3ms

  10. Jan 27 18:52:43 debian systemd[1]: Starting /etc/rc.local Compatibility...
  11. Jan 27 18:52:43 debian systemd[1]: Started /etc/rc.local Compatibility.
复制代码

然后你就可以把需要开机启动的命令添加到 /etc/rc.local 文件,丢在 exit 0 前面即可,重启以后试试是否生效了。

作者:烧饼
文章来源: https://u.sb/debian-rc-local/
回复

使用道具 举报

9#
发表于 2022-7-24 22:14:46 | 只看该作者
感谢分享
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 注册

本版积分规则

小黑屋|手机版|Archiver|捐助支持|无忧启动 ( 闽ICP备05002490号-1 )

闽公网安备 35020302032614号

GMT+8, 2025-12-10 21:43

Powered by Discuz! X3.3

© 2001-2017 Comsenz Inc.

快速回复 返回顶部 返回列表