systemd系统开机运行rc.local

原文出处: https://xugaoxiang.com/2019/12/05/systemd-rc-local-on-boot/

软硬件环境

  • ubuntu 18.04 64bit
  • anaconda with python 3.6

问题描述

在使用较新版本的ubuntu系统中发现,原来写入/etc/rc.local文件中的开机命令不执行了?经过一番研究,发现原来ubuntu16.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字段的相关信息,如WantedByRequiredByAlsoAlias。如果不写呢,系统就不认为它是一个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中的脚本已经被正确执行了。

参考资料

  • 9
    点赞
  • 44
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论
开机启动是指在系统启动时自动运行特定的脚本或程序。在 Linux 系统中,有多种方法可以实现开机启动,其中包括使用 rc.local 和编写开机启动脚本。 1. rc.local 方法: rc.local 是一个在系统启动阶段自动执行的脚本文件,可以用来配置开机启动项。以下是使用 rc.local 实现开机启动的步骤: 1. 打开终端,使用文本编辑器(如 vi 或 nano)以管理员权限编辑 rc.local 文件: ``` sudo nano /etc/rc.local ``` 2. 在文件中添加需要在系统启动时执行的命令或脚本,例如: ``` #!/bin/bash # 启动脚本示例 /path/to/your/script.sh ``` 注意:确保添加的命令或脚本的执行权限正确设置(如使用 chmod 命令设置为可执行)。 3. 保存并关闭文件。 4. 确保 rc.local 文件具有可执行权限: ``` sudo chmod +x /etc/rc.local ``` 5. 重新启动系统,验证开机启动是否成功。 2. 开机启动脚本方法: 另一种常见的方法是编写一个专门的开机启动脚本,然后将其添加到系统的启动项中。以下是使用开机启动脚本方法的步骤: 1. 创建一个新的启动脚本文件,例如 `myscript.sh`: ``` #!/bin/bash # 启动脚本示例 /path/to/your/script.sh ``` 2. 保存并关闭文件。 3. 将脚本移动到 `/etc/init.d/` 目录中: ``` sudo mv myscript.sh /etc/init.d/ ``` 4. 为脚本设置执行权限: ``` sudo chmod +x /etc/init.d/myscript.sh ``` 5. 将脚本添加到系统启动项中: ``` sudo update-rc.d myscript.sh defaults ``` 6. 重新启动系统,验证开机启动是否成功。 请注意,具体步骤可能因不同的 Linux 发行版而有所差异。在某些发行版中,也可能使用 systemd 或其他工具来管理开机启动项。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

迷途小书童的Note

请博主喝矿泉书!

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值