一、rc-local服務(wù)簡介
Linux中的rc-local服務(wù)是一個(gè)開機(jī)自動(dòng)啟動(dòng)的,調(diào)用開發(fā)人員或系統(tǒng)管理員編寫的可執(zhí)行腳本或命令的,它的啟動(dòng)順序是在系統(tǒng)所有服務(wù)加載完成之后執(zhí)行。
ubuntu20.04系統(tǒng)已經(jīng)默認(rèn)安裝了rc-local.service服務(wù)璧针,不過需要手動(dòng)設(shè)置一下才可以使用坦袍。
二十厢、配置
2.1、創(chuàng)建rc-local.service文件
systemctl默認(rèn)讀取/etc/systemd/system/下的配置文件捂齐。我們可以創(chuàng)建一個(gè)軟連接到/lib/systemd/system/rc-local.service蛮放。
root@rshine:/etc/systemd/system# ln -s /lib/systemd/system/rc-local.service rc-local.service
root@rshine:/etc/systemd/system# ls rc-local.service
rc-local.service
root@rshine:/etc/systemd/system#
2.2、創(chuàng)建/etc/rc.local
開機(jī)自啟動(dòng)腳本奠宜,我們一般都放在這個(gè)文件中包颁。但是Ubuntu20.04中默認(rèn)沒有這個(gè)文件,需要我們自己創(chuàng)建压真。
root@rshine:/etc/systemd/system# touch /etc/rc.local
root@rshine:/etc/systemd/system# chmod 755 /etc/rc.local
root@rshine:~# echo '#!/bin/bash' >> /etc/rc.local
2.3娩嚼、設(shè)置rc-local.service開機(jī)自啟
將rc-local.service服務(wù)設(shè)置為開機(jī)自啟動(dòng)。當(dāng)然有可能你會(huì)遇到下面的報(bào)錯(cuò)滴肿。
root@rshine:/etc/systemd/system# systemctl enable rc-local.service
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.
root@rshine:/etc/systemd/system#
報(bào)錯(cuò)解決方法:
在/etc/systemd/system/rc-local.service文件末尾添加最后三行岳悟,如果有的話就不用添加。
root@rshine:~# cat /etc/systemd/system/rc-local.service
# 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
Alias=rc-local.service
root@rshine:~#
再次設(shè)置開機(jī)自啟就不會(huì)報(bào)錯(cuò)了泼差。
root@rshine:/etc/systemd/system# systemctl enable rc-local.service
Created symlink /etc/systemd/system/multi-user.target.wants/rc-local.service → /lib/systemd/system/rc-local.service.
root@rshine:/etc/systemd/system#