Ubuntu-18.04设置开机启动脚本

时间:2023-03-09 03:48:13
Ubuntu-18.04设置开机启动脚本

  参考:https://www.cnblogs.com/defifind/p/9285456.html

       http://www.cnblogs.com/airdot/p/9688530.html

  systemd 默认读取 /etc/systemd/system 下的配置文件,该目录下的文件会链接/lib/systemd/system/下的文件

  打开脚本/lib/systemd/system/rc.local.service  添加末尾几行

[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
elias=rc-local.service

  PS:实际测试无需添加最后三行也能实现

  最后增加字段[Install]否则在enable的时候可能会报一下错误,虽然报一下错误后开机自启程序还是正常的

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.

Ubuntu-18.04设置开机启动脚本

  创建启动文件/etc/rc.local(默认无该文件)

#!/bin/sh -e
touch /tmp/test

  增加执行权限

chmod +x /etc/rc.local

  PS:需要#!/bin/sh -e否则可能不生效

  设置软连接

ln -s /lib/systemd/system/rc.local.service /etc/systemd/system/

  重启如果在/tmp目录下面创建了test文件则代表设置开启启动成功

  启动服务并设置开启自启动

systemctl start rc-local
systemctl enable rc-local

  重启服务器测试,可以通过命令查看自启动状态以及自启动执行时候的报错等信息

systemctl status rc-local

  如果启动rc-local报错

journalctl -xe

  

单元已失败

Ubuntu-18.04设置开机启动脚本

  检查/etc/rc.local文件配置,是否有非英文状态输入的字符