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

推薦使用建議
- 系統(tǒng)服務(wù):使用Systemd(現(xiàn)代標(biāo)準(zhǔn))
- 簡單個(gè)人任務(wù):使用cron @reboot或rc.local
- GUI應(yīng)用:使用桌面環(huán)境自啟動(dòng)
- 兼容性考慮:rc.local在大多數(shù)系統(tǒng)仍可用
選擇哪種方式取決于你的具體需求、系統(tǒng)版本和使用場景。
以上就是Linux中實(shí)現(xiàn)開機(jī)自啟動(dòng)的幾種常見方式及區(qū)別詳解的詳細(xì)內(nèi)容,更多關(guān)于Linux開機(jī)自啟動(dòng)方式的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Linux drm_syncobj機(jī)制原理與應(yīng)用方式
DRM_syncobj是Linux內(nèi)核中用于GPU同步的抽象,支持二元與時(shí)間線語義,提供高效用戶接口,實(shí)現(xiàn)跨進(jìn)程/驅(qū)動(dòng)同步,滿足現(xiàn)代圖形API如Vulkan的復(fù)雜需求2025-09-09
linux中install命令和cp命令的使用與區(qū)別
相信大家都知道linux中的命令I(lǐng)nstall和cp類似,都可以將文件/目錄拷貝到指定的地點(diǎn)。下面這篇文章就詳細(xì)介紹了linux中install命令和cp命令的介紹與區(qū)別。有需要的朋友們可以參考借鑒,下面來一起看看吧。2017-01-01
CentOS 7下部署php7.1和開啟MySQL擴(kuò)展的方法教程
這篇文章主要給大家介紹了關(guān)于CentOS 7下部署php7.1和開啟MySQL擴(kuò)展的方法教程,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起看看吧。2017-07-07
CentOS7安裝nodejs18和yarn操作實(shí)踐
CentOS7因glibc版本低不支持Node.js18,需手動(dòng)下載非官方構(gòu)建版本并配置環(huán)境變量,安裝Yarn3需啟用Corepack,若依賴glibc的包建議改用Docker或更換系統(tǒng)2025-10-10
Linux環(huán)境使用g++編譯C++方法總結(jié)
本篇文章給大家分享了在Linux環(huán)境中用g++編譯C++的方法以及相關(guān)實(shí)例代碼分享,有興趣的朋友學(xué)習(xí)下。2018-03-03

