問題描述
我在/etc/rc.local
腳本中有一個命令,它應該在啟動期間啟動Tiny Tiny RSS的更新守護程序,但在啟動期間不會執行該腳本。為什麽?
整個/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.
/sbin/start-stop-daemon -b -c www-data:www-data -S -x /usr/bin/php /var/www/ttrss/update_daemon2.php -- -quiet
exit 0
/etc/rc.local
是可執行的:
# ls -l /etc/rc.local
-rwxr-xr-x 1 root root 342 May 25 16:14 /etc/rc.local
/etc/init.d/rc.local
存在且可執行:
# ls -l /etc/init.d/rc.local
-rwxr-xr-x 1 root root 801 Jul 27 2012 /etc/init.d/rc.local
/etc/init.d/rc.local
應該在啟動時為此運行級別執行:
# runlevel
N 2
# ls -l /etc/rc2.d/S99rc.local
lrwxrwxrwx 1 root root 18 Sep 22 2012 /etc/rc2.d/S99rc.local -> ../init.d/rc.local
如果我從命令行手動調用/etc/rc.local,則update_daemon會加載…
# /etc/rc.local
# ps ax | grep update_daemon2.php
2233 ? S 0:00 /usr/bin/php /media/sda5/www/news/update_daemon2.php -quiet
2234 ? S 0:00 /usr/bin/php /media/sda5/www/news/update_daemon2.php -quiet
…每次我的服務器重新啟動時都必須記住這個問題,直到修複此問題為止。
Similar questions already存在,但到目前為止,我無法將信息應用於我的具體問題。
為什麽啟動期間rc.local中的命令沒有執行?
最佳解決方法
如果在執行任何命令時發生任何錯誤,rc.local
腳本將退出(請參閱#!/bin/sh -e
中的-e
標誌)。
當您嘗試在執行rc.local
時運行命令時,可能無法滿足某些先決條件,因此命令執行失敗。
我在手動設置cpu調控器時遇到了同樣的事情,而在rc.local
中沒有這樣做。這是我的自定義解決方法,它使用update-rc.d
使您的命令在啟動時運行:
-
在目錄
/etc/init.d
中創建文件myscript.sh
,標題為:#!/bin/sh
-
將自定義命令作為內容
-
使其可執行:
sudo chmod +x /etc/init.d/myscript.sh
-
為各種運行級別的腳本創建符號鏈接:
sudo update-rc.d myscript.sh defaults
此外,您可以檢查/etc/network/if-up.d
腳本,看看是否可以在網絡啟動時觸發命令。
次佳解決方法
嘗試sudo sysv-rc-conf
並檢查rc.local
是否已啟用
rc.local [ ] [x] [x] [x] [x] [ ] [ ] [ ]
第三種解決方法
我在rc.local中有一些類似的問題沒有在啟動時執行
sshades給了我以下答案:
Ubuntu is now using systemd, and rc.local is now considered a service which is turned “off” by default. You can turn rc.local “on” by entering the following command and rebooting:
sudo systemctl enable rc-local.service
雖然我還沒有測試過他的解決方案,但我認為這聽起來合乎邏輯且有效。但是:
我還找到了一個解決方案,即添加腳本到./.config/autostart-scripts/就可以了
第四種方法
我們在加載FW規則的某些托管服務器上遇到此問題。
在這些盒子上,他們非常快速地重啟,我們發現隻需在rc.local中放入一個“睡眠1”,然後加載語句就可以解決問題。我想在加載FW規則之前,它為接口提供了一點時間。
第五種方法
確保rc.local腳本是可執行的:
sudo chmod +x /etc/rc.local
然後,啟用它:
sudo systemctl enable rc-local.service
通過運行以下命令重新啟動係統或手動啟動腳本:
sudo systemctl start rc-local.service
運行以下命令可以顯示服務狀態:
$ sudo systemctl status rc-local.service
● rc-local.service - /etc/rc.local Compatibility
Loaded: loaded (/lib/systemd/system/rc-local.service; static; vendor preset: enabled)
Drop-In: /lib/systemd/system/rc-local.service.d
└─debian.conf
Active: active (running) since Mon 2018-04-02 10:39:44 -03; 1s ago
Process: 2044 ExecStart=/etc/rc.local start (code=exited, status=0/SUCCESS)
Main PID: 2049 (svscanboot)
Tasks: 3
Memory: 556.0K
CPU: 10ms
CGroup: /system.slice/rc-local.service