原文出处: https://xugaoxiang.com/2019/12/05/systemd-rc-local-on-boot/
软硬件环境
- ubuntu 18.04 64bit
- anaconda with python 3.6
问题描述
在使用较新版本的ubuntu
系统中发现,原来写入/etc/rc.local
文件中的开机命令不执行了?经过一番研究,发现原来ubuntu
自16.04
版本后就不再使用initd
管理系统,而改用systemd
分析问题
通过systemctl
命令查看rc-local
的状态
sudo systemctl status rc-local
输出错误信息
● rc-local.service - /etc/rc.local Compatibility
Loaded: loaded (/lib/systemd/system/rc-local.service; enabled-runtime; vendor preset: enabled)
Drop-In: /lib/systemd/system/rc-local.service.d
└─debian.conf
Active: failed (Result: exit-code) since Thu 2018-11-01 10:56:36 CST; 1h 59min ago
Docs: man:systemd-rc-local-generator(8)
Process: 1961 ExecStart=/etc/rc.local start (code=exited, status=203/EXEC)
11月 01 10:56:36 ubuntu systemd[1]: Starting /etc/rc.local Compatibility...
11月 01 10:56:36 ubuntu systemd[1961]: rc-local.service: Failed to execute command: Exec format error
11月 01 10:56:36 ubuntu systemd[1961]: rc-local.service: Failed at step EXEC spawning /etc/rc.local: Exec format e
11月 01 10:56:36 ubuntu systemd[1]: rc-local.service: Control process exited, code=exited status=203
11月 01 10:56:36 ubuntu systemd[1]: rc-local.service: Failed with result 'exit-code'.
11月 01 10:56:36 ubuntu systemd[1]: Failed to start /etc/rc.local Compatibility.
在systemd
中使能rc-local
服务
sudo systemctl enable rc-local
输出错误信息
xugaoxiang@ubuntu:~$ sudo 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:
1) A unit may be statically enabled by being symlinked from another unit's
.wants/ or .requires/ directory.
2) A unit's purpose may be to act as a helper for some other unit which has
a requirement dependency on it.
3) A unit may be started when needed via activation (socket, path, timer,
D-Bus, udev, scripted systemctl call, ...).
4) In case of template units, the unit is meant to be enabled with some
instance name specified.
大意是说rc-local
服务文件中没有Install
字段的相关信息,如WantedBy
、RequiredBy
、Also
、Alias
。如果不写呢,系统就不认为它是一个systemd
服务。
修复问题
默认的service
文件都是存在与/etc/systemd/system
目录下,有点像某种服务的配置文件。注意到/lib/systemd/system
下也有个rc-local.service
,我们借用这个模板来进行修改,当然你也可以从头开始编写
sudo cp /lib/systemd/system/rc-local.service /etc/systemd/system/rc-local.service
修改内容如下,主要是添加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
其中Unit
字段主要描述服务的启动顺序以及依赖关系,Service
字段主要描述如何启动,Install
字段描述如何安装这个服务。ubuntu 18.04
系统默认已经将/etc/rc.local
文件移除了,因此,我们需要手动创建一个,并将需要开机执行的命令写入到文件中,如
#!/bin/bash
/usr/bin/sslocal -c /home/xugaoxiang/Tools/ss/ss.json &
同样的,别忘了,给/etc/rc.local
加上可执行的权限
sudo chmod a+x /etc/rc.local
然后执行
xugaoxiang@ubuntu:~$ sudo systemctl enable rc-local
Created symlink /etc/systemd/system/multi-user.target.wants/rc-local.service → /etc/systemd/system/rc-local.service.
接着启动这个服务并查看它的状态
sudo systemctl start rc-local.service
sudo systemctl status rc-local.service
命令输出如下
● rc-local.service - /etc/rc.local Compatibility
Loaded: loaded (/etc/systemd/system/rc-local.service; enabled; vendor preset: enabled)
Drop-In: /lib/systemd/system/rc-local.service.d
└─debian.conf
Active: active (running) since Thu 2018-11-01 13:17:08 CST; 2s ago
Docs: man:systemd-rc-local-generator(8)
Process: 10810 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)
Tasks: 1 (limit: 4915)
CGroup: /system.slice/rc-local.service
└─10811 /usr/bin/python /usr/bin/sslocal -c /home/xugaoxiang/Tools/ss/ss.json
11月 01 13:17:08 ubuntu systemd[1]: Starting /etc/rc.local Compatibility...
11月 01 13:17:08 ubuntu systemd[1]: Started /etc/rc.local Compatibility.
11月 01 13:17:08 ubuntu rc.local[10810]: INFO: loading config from /home/xugaoxiang/Tools/ss/ss.json
11月 01 13:17:08 ubuntu rc.local[10810]: 2018-11-01 13:17:08 INFO loading libcrypto from libcrypto.so.1.1
11月 01 13:17:08 ubuntu rc.local[10810]: 2018-11-01 13:17:08 INFO starting local at 127.0.0.1:1080
可以看到rc.local
中的脚本已经被正确执行了。