无忧启动论坛
标题: systemd 之下启用 rc.local [打印本页]
作者: 不点 时间: 2022-7-23 19:48
标题: systemd 之下启用 rc.local
本帖最后由 不点 于 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
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:
- #!/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.
- 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:
- ● rc-local.service - /etc/rc.local Compatibility
- Loaded: loaded (/lib/systemd/system/rc-local.service; enabled-runtime; vendor preset: enabled)
- Drop-In: /usr/lib/systemd/system/rc-local.service.d
- └─debian.conf Active: active (exited)
- Docs: man:systemd-rc-local-generator(8)
- Process: 58756 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)
复制代码
作者: my9823 时间: 2022-7-23 19:54
各大serivce文件在/etc/systemd/system/目录和/usr/lib/systemd/system目录
作者: my9823 时间: 2022-7-23 20:00
前者有优先于后者。这些个文件大部分都是文本文件。用文本编辑器打开看看,里边有这个service内容,这些依赖那些模,在那些之后或者之前运行,运行命令,与那些命令冲突。initsysv是串行运行,systemd是并行运行的,。可以这样理解,systemd默认以default.target为目标,这个文件也是文本文件,看看它一来那些target,必须在那些target之后运行,找到这些一直往上逆向推回去,就知道启动了哪些脚本命令。
作者: my9823 时间: 2022-7-23 20:12
作者: 9527sss 时间: 2022-7-23 20:29
谢谢分享
作者: 9527sss 时间: 2022-7-23 20:29
谢谢分享
作者: su45 时间: 2022-7-23 22:38
谢谢分享
作者: 不点 时间: 2022-7-24 06:33
本帖最后由 不点 于 2022-7-24 06:36 编辑
转贴一篇中文的文章 https://blogs.vicsdf.com/article/44516
Debian 11 Bullseye 解决 rc.local 开机启动问题
本文同样适用于 Debian 10 Buster 和 Debian 9 Strech。
由于某些软件并没有增加开启启动的服务,很多时候需要手工添加,一般我们都是推荐使用 systemd 写个系统服务,但是对于一些简单的脚本或者懒人来说,添加命令到 /etc/rc.local 文件更方便,但是自从 Debian 9 开始,Debian 默认不带 /etc/rc.local 文件,而 rc.local 服务却还是自带的:
- root@Debian ~ # cat /lib/systemd/system/rc-local.service
- # SPDX-License-Identifier: LGPL-2.1-or-later## 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
复制代码
并且默认情况下这个服务还是关闭的状态:
- root@debian ~ # systemctl status rc-local
- ● rc-local.service - /etc/rc.local Compatibility
- Loaded: loaded (/lib/systemd/system/rc-local.service; static)
- Drop-In: /usr/lib/systemd/system/rc-local.service.d
- └─debian.conf
- Active: inactive (dead)
- Docs: man:systemd-rc-local-generator(8)
复制代码
为了解决这个问题,我们需要手工添加一个 /etc/rc.local 文件:
- cat <<EOF >/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.
- exit 0
- EOF
复制代码
然后赋予权限:
接着启动 rc-local 服务:
- systemctl start rc-local
- systemctl enable rc-local
复制代码
此时可能会弹出警告:
- The unit files have no installation config (WantedBy=, RequiredBy=, Also=,
- Alias= settings in the [Install] section, and DefaultInstance= for template
- units). This means they are not meant to be enabled using systemctl.
- Possible reasons for having this kind of units are:
- • A unit may be statically enabled by being symlinked from another unit's
- .wants/ or .requires/ directory.
- • A unit's purpose may be to act as a helper for some other unit which has
- a requirement dependency on it.
- • A unit may be started when needed via activation (socket, path, timer,
- D-Bus, udev, scripted systemctl call, ...).
- • In case of template units, the unit is meant to be enabled with some
- instance name specified.
复制代码
无视警告,因为这个服务没有任何依赖的系统服务,只是开机启动 /etc/rc.local 脚本而已。再次查看状态:
- root@debian ~ # systemctl status rc-local.service
- ● rc-local.service - /etc/rc.local Compatibility
- Loaded: loaded (/lib/systemd/system/rc-local.service; enabled-runtime; vendor preset: enabled)
- Drop-In: /usr/lib/systemd/system/rc-local.service.d
- └─debian.conf
- Active: active (exited) since Thu 2022-01-27 18:52:43 UTC; 10s ago
- Docs: man:systemd-rc-local-generator(8)
- Process: 541 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)
- CPU: 3ms
- Jan 27 18:52:43 debian systemd[1]: Starting /etc/rc.local Compatibility...
- Jan 27 18:52:43 debian systemd[1]: Started /etc/rc.local Compatibility.
复制代码
然后你就可以把需要开机启动的命令添加到 /etc/rc.local 文件,丢在 exit 0 前面即可,重启以后试试是否生效了。
作者: lianmeng 时间: 2022-7-24 22:14
感谢分享
| 欢迎光临 无忧启动论坛 (http://bbs.wuyou.net/) |
Powered by Discuz! X3.3 |