Debian 11 开机启动执行自定义脚本

前言

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

rc-local 服务启动

查看 rc-local 服务的 /lib/systemd/system/rc-local.service 配置文件,其内容如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
#  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

并且在默认情况下,rc-local 这个服务还是默认关闭的

1
2
3
4
5
6
● 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 文件,内容如下:

1
2
3
4
5
6
7
8
9
10
11
#!/bin/sh -e
# 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

然后赋予 /etc/rc.local 文件执行权限

1
$ sudo chmod +x /etc/rc.local

设置 rc-local 服务开机自自动,并立刻启动它

1
$ sudo systemctl enable --now rc-local

此时可能会弹出警告(如下),可以忽略警告信息,因为这个服务没有任何依赖的系统服务,只是开机执行 /etc/rc.local 脚本而已

1
2
3
4
5
6
7
8
9
10
11
12
13
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.

再次查看 rc-local 服务的运行状态

1
$ sudo systemctl status rc-local.service
1
2
3
4
5
6
7
8
9
10
11
● 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 文件里,添加需要开机自动执行自定义的 Shell 脚本内容了。特别注意,自定义的脚本内容,必须写在 exit 0 之前,否则不会生效

1
2
3
4
5
6
7
8
9
10
11
12
13
#!/bin/sh -e
# 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.

echo never > /sys/kernel/mm/transparent_hugepage/enabled

exit 0

rc-local 服务管理

1
2
3
4
5
6
7
8
9
10
11
# 开启自启动
sudo systemctl enable rc-local

# 启动服务
sudo systemctl start rc-local.service

# 关闭服务
sudo systemctl stop rc-local.service

# 查看状态
sudo systemctl status rc-local.service

参考资料