欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

Linux使用Systemd設(shè)置程序開(kāi)機(jī)自啟動(dòng)的具體教程

 更新時(shí)間:2025年05月28日 10:20:50   作者:嵌入式學(xué)習(xí)和實(shí)踐  
Systemd是Linux系統(tǒng)中的初始化系統(tǒng),旨在提高啟動(dòng)速度和效率,它通過(guò)并行啟動(dòng)服務(wù)、按需加載、cgroups管理進(jìn)程、自動(dòng)掛載管理等方式優(yōu)化啟動(dòng)過(guò)程,本文給大家介紹了Linux系統(tǒng)中使用Systemd設(shè)置程序開(kāi)機(jī)自啟動(dòng)教程,需要的朋友可以參考下

使用Systemd設(shè)置程序的開(kāi)機(jī)自啟動(dòng)的方法。

1. 創(chuàng)建服務(wù)文件

在 /etc/systemd/system/ 目錄下創(chuàng)建一個(gè) .service 文件(需 sudo 權(quán)限):

sudo nano /etc/systemd/system/myprogram.service
或者
sudo vi /etc/systemd/system/myprogram.service
或者
sudo gedit /etc/systemd/system/myprogram.service

2. 編寫(xiě)服務(wù)配置

將以下模板寫(xiě)入文件(按需修改參數(shù)):

[Unit]
Description=My Custom Program   # 服務(wù)描述
After=network.target           # 指定依賴(lài)的啟動(dòng)目標(biāo)(如網(wǎng)絡(luò)就緒后啟動(dòng))

[Service]
ExecStart=/usr/bin/python3 /path/to/your_script.py  # 程序啟動(dòng)命令(絕對(duì)路徑)
WorkingDirectory=/path/to/working_dir  # 可選:程序工作目錄
User=myuser                         # 運(yùn)行程序的用戶(如 root 或其他用戶)
Group=mygroup                       # 用戶組
Restart=always                      # 崩潰后自動(dòng)重啟
RestartSec=5                        # 重啟間隔(秒)

[Install]
WantedBy=multi-user.target          # 關(guān)聯(lián)到系統(tǒng)啟動(dòng)目標(biāo)

測(cè)試:

[Unit]
Description=test2   
After=network.target           

[Service]
ExecStart=/mnt/hgfs/shareData/demo/2/1  
WorkingDirectory=/mnt/hgfs/shareData/demo/2  
User=root                                              
Restart=always                     
RestartSec=5                        

[Install]
WantedBy=multi-user.target          

3. 啟用并啟動(dòng)服務(wù)

  • 重新加載Systemd配置(修改服務(wù)文件后必執(zhí)行):
sudo systemctl daemon-reload
  • 啟用開(kāi)機(jī)自啟動(dòng)
sudo systemctl enable myprogram.service
  • 立即啟動(dòng)服務(wù)(無(wú)需重啟):
sudo systemctl start myprogram.service

4. 管理服務(wù)

  • 查看服務(wù)狀態(tài)
systemctl status myprogram.service
  • 停止服務(wù)
sudo systemctl stop myprogram.service
  • 禁用開(kāi)機(jī)自啟動(dòng)
sudo systemctl disable myprogram.service
  • 查看日志
journalctl -u myprogram.service -e  # 查看最新日志
journalctl -u myprogram.service -f  # 實(shí)時(shí)追蹤日志

測(cè)試:

常見(jiàn)問(wèn)題

  1. 路徑錯(cuò)誤

    • 確保 ExecStart 中的命令使用絕對(duì)路徑。
    • 檢查腳本是否有執(zhí)行權(quán)限:chmod +x /path/to/script
  2. 權(quán)限問(wèn)題

    • 若程序需要特定用戶權(quán)限,在 [Service] 中設(shè)置 User 和 Group。
  3. 依賴(lài)未就緒

    • 在 [Unit] 中使用 After=network.target mysql.target 等指定依賴(lài)服務(wù)。

到此這篇關(guān)于Linux使用Systemd設(shè)置程序開(kāi)機(jī)自啟動(dòng)的具體教程的文章就介紹到這了,更多相關(guān)Linux Systemd程序開(kāi)機(jī)自啟動(dòng)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

最新評(píng)論