Linux中實現開機自啟動的幾種常見方式及區(qū)別詳解
/etc/rc.local:
這是傳統(tǒng)的SysVinit系統(tǒng)中的自啟動腳本。在系統(tǒng)啟動到指定運行級別時,會執(zhí)行這個文件中的命令。
在使用systemd的系統(tǒng)中,為了兼容性,通常有一個rc-local.service來運行/etc/rc.local。但需要確保該服務被啟用。
使用rc.local比較簡單,只需將需要開機自啟動的命令寫入該文件即可。但注意,該文件默認可能沒有執(zhí)行權限,需要確保其有執(zhí)行權限。
具體操作方式
1. 創(chuàng)建或編輯rc.local文件
sudo vim /etc/rc.local
2. 添加內容
寫入自啟動腳本
#!/bin/bash sudo -u pharmacy /home/services/rcs/start_rcs.sh & sudo -u pharmacy /home/abc.sh & sudo -u pharmacy /home/edf.sh & # 添加日志以便調試 echo "$(date): rc.local executed successfully" >> /var/log/rc-local.log exit 0
3. 給文件添加執(zhí)行權限
sudo chmod +x /etc/rc.local
4. 啟用rc-local服務(如果尚未啟用)
啟用rc-local服務:、
sudo systemctl enable rc-local
啟動服務:
sudo systemctl start rc-local
在這里如果提示如下,說明rc-local沒有被systemctl管理到,那么需要新增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.
5.新增service 配置
sudo tee /etc/systemd/system/rc-local.service > /dev/null << 'EOF' [Unit] Description=/etc/rc.local Compatibility ConditionFileIsExecutable=/etc/rc.local After=network.target [Service] Type=forking ExecStart=/etc/rc.local start TimeoutSec=0 RemainAfterExit=yes [Install] WantedBy=multi-user.target EOF
6. 重新加載 systemd 并啟用服務
sudo systemctl daemon-reload sudo systemctl enable rc-local.service sudo systemctl start rc-local.service sudo systemctl status rc-local.service
7. 驗證rc-local服務是否啟用
sudo systemctl status rc-local.service
如果服務狀態(tài)顯示為active 則表示服務已經運行,下方會顯示腳本的執(zhí)行打印日志和時間
/etc/init.d/(SysVinit腳本):
在SysVinit系統(tǒng)中,每個運行級別都有對應的目錄(如/etc/rc0.d~/etc/rc6.d),這些目錄中的符號鏈接指向/etc/init.d/中的腳本。
可以通過update-rc.d(在Debian/Ubuntu中)或chkconfig(在RedHat/CentOS中)來管理這些鏈接,從而控制服務在哪個運行級別啟動或停止。
這種方式較為傳統(tǒng),現在逐漸被systemd取代。
systemd:
現代大多數Linux發(fā)行版使用systemd作為初始化系統(tǒng)。
用戶可以通過創(chuàng)建自定義的service單元文件(通常放在/etc/systemd/system/目錄下)來管理自啟動服務。
使用systemctl enable service_name來啟用自啟動,使用systemctl start service_name來立即啟動服務。
systemd提供了更強大的功能,如依賴管理、條件啟動、資源控制等。
使用方式
下方例子是啟動指定腳本,然后在腳本中執(zhí)行程序啟動,不監(jiān)視程序是否停止運行且不會重啟,目標只是為了實現簡單的自動啟動。另外修改service內容,可以支持自啟動和崩潰重啟。
rcs.service 文件內容(只做開啟自啟動,不做程序退出檢測和退出后的后的自啟動)
[Unit] Description=run jd rcs After=network.target [Service] Type=oneshot ExecStart=/home/services/rcs/start_rcs.sh User=iamuser Group=iamuser WorkingDirectory=/home/prod/rcs/build/ RemainAfterExit=yes [Install] WantedBy=multi-user.target
- Type=oneshot:適用于只執(zhí)行一次然后退出的服務
- 移除了 ExecStop:因為 oneshot 類型不需要停止腳本
- 保留 RemainAfterExit=yes:讓 systemd 知道服務已"激活",即使主進程已退出
cron:
使用@reboot選項,可以在系統(tǒng)啟動時運行指定的命令或腳本。
例如,在crontab中添加@reboot /path/to/script。
具體為:
crontab -e
添加以下行
@reboot /path/to/your/script.sh
這種方法簡單,但不適合需要復雜依賴關系或需要作為守護進程運行的服務。
/etc/rc.d/rc.local(在某些發(fā)行版中):
類似于/etc/rc.local,在一些傳統(tǒng)的發(fā)行版中(如RedHat/CentOS 6及以下)使用。
~/.config/autostart(桌面環(huán)境自啟動):
對于圖形界面環(huán)境,用戶可以在~/.config/autostart目錄下放置.desktop文件,以便在用戶登錄時自動啟動應用程序。
/etc/xdg/autostart(系統(tǒng)級別的桌面自啟動):
與用戶級別的自啟動類似,但是應用于所有用戶。
區(qū)別總結:
適用范圍:rc.local和SysVinit腳本通常用于系統(tǒng)級別的啟動,而cron和桌面自啟動可以用于用戶級別。
靈活性:systemd提供了最靈活和強大的管理方式,可以處理復雜的依賴和條件。
兼容性:rc.local和SysVinit腳本在較老的系統(tǒng)中常見,而systemd是現代系統(tǒng)的標準。
使用簡便性:對于簡單的啟動任務,rc.local和cron的@reboot可能更簡單。
在選擇自啟動方法時,需要考慮你的發(fā)行版使用的初始化系統(tǒng)以及你的具體需求(如是否需要在圖形界面啟動后運行、是否需要守護進程等)。
在這里插入圖片描述

推薦使用建議
- 系統(tǒng)服務:使用Systemd(現代標準)
- 簡單個人任務:使用cron @reboot或rc.local
- GUI應用:使用桌面環(huán)境自啟動
- 兼容性考慮:rc.local在大多數系統(tǒng)仍可用
選擇哪種方式取決于你的具體需求、系統(tǒng)版本和使用場景。
以上就是Linux中實現開機自啟動的幾種常見方式及區(qū)別詳解的詳細內容,更多關于Linux開機自啟動方式的資料請關注腳本之家其它相關文章!
相關文章
linux中install命令和cp命令的使用與區(qū)別
相信大家都知道linux中的命令Install和cp類似,都可以將文件/目錄拷貝到指定的地點。下面這篇文章就詳細介紹了linux中install命令和cp命令的介紹與區(qū)別。有需要的朋友們可以參考借鑒,下面來一起看看吧。2017-01-01
CentOS 7下部署php7.1和開啟MySQL擴展的方法教程
這篇文章主要給大家介紹了關于CentOS 7下部署php7.1和開啟MySQL擴展的方法教程,文中通過示例代碼介紹的非常詳細,對大家具有一定的參考學習價值,需要的朋友們下面來一起看看吧。2017-07-07

