Linux系統使用init解決Nginx無法自啟動的辦法

最近遇到服務器重啟后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?是串行啟動,前一個進程啟動完才會啟動下一個進程。如果太過復雜的服務,就會導致開機啟動變慢。

贊(4)
版權聲明:本文采用知識共享 署名4.0國際許可協議 [BY-NC-SA] 進行授權, 轉載請注明出處。
文章名稱:《Linux系統使用init解決Nginx無法自啟動的辦法》
文章鏈接:http://www.lrracing.com/37324.html
【聲明】:國外主機測評僅分享信息,不參與任何交易,也非中介,所有內容僅代表個人觀點,均不作直接、間接、法定、約定的保證,讀者購買風險自擔。一旦您訪問國外主機測評,即表示您已經知曉并接受了此聲明通告。
【關于安全】:任何 IDC商家都有倒閉和跑路的可能,備份永遠是最佳選擇,服務器也是機器,不勤備份是對自己極不負責的表現,請保持良好的備份習慣。