最近遇到服務器重啟后Nginx無法自啟,必須開機后手動啟動,也懶得排查問題在哪,直接添加開機自啟動服務。
雖然?Systemd?用來啟動守護進程,已經成為現在Linux系統的標準配置。但是為了這么一句Nginx的啟動命令就寫一個系統服務實在有點麻煩。這時?init?就顯得更容易了。
對于現在發行的Linux系統已經不再有 /etc/rc.local 文件,但 rc.local 服務卻還是自帶的:
root@debian ~ # cat /lib/systemd/system/rc-local.service
# 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
默認情況下這個服務還是關閉的狀態:
root@debian ~ # systemctl status rc-local
● 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 文件:
cat <<EOF >/etc/rc.local
#!/bin/sh -e
#
# rc.local
#
# 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
EOF
然后賦予權限:
chmod +x /etc/rc.local
接著啟動 rc-local 服務:
systemctl enable --now 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: ? 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.
無視警告,因為這個服務沒有任何依賴的系統服務,只是開機啟動?/etc/rc.local?腳本而已。
再次查看狀態:
root@debian ~ # systemctl status rc-local
● 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)
然后我們把下面的Nginx開機啟動命令添加到 /etc/rc.local 文件中 exit 0 前面即可。
/etc/init.d/nginx start
如果你有別的需要開機啟動的命令、腳本,都可以添加在這里。之后嘗試重啟就會發現已經生效了。
如果是參數較多或是較為復雜的需要開機啟動的服務,這里還是建議你使用?Systemd?。因為?init?是串行啟動,前一個進程啟動完才會啟動下一個進程。如果太過復雜的服務,就會導致開機啟動變慢。




















