# 開機自動啟動程式 ## Startup 1. 開啟Startup Applications Preferences GUI ```shell gnome-session-properties ``` 2. 點擊Add按鈕 3. 依序輸入Name、Command、Comment後點擊Add按鈕 ```shell sh -c "sleep 5s; home/[USER]/[FOLDER]/main.sh" ``` 4. 重新開機 ## Systemd ### 設定步驟 1. 建立service檔案 ```shell # 以root權限執行 sudo gedit /etc/systemd/system/[NAME].service # 以user權限執行 sudo gedit ~/.config/systemd/user/[NAME].service ``` 2. 編輯service檔案 ``` # 使用root權限執行一次 [Unit] Description=Run one time [Service] Type=oneshot ExecStart=/usr/bin/python3 [PATH]/main.py RemainAfterExit=yes [Install] WantedBy=multi-user.target ``` ``` # 使用root權限常駐執行 [Unit] Description=Run forever After=[name].service [Service] Type=simple WorkingDirectory=/home/[USER]/ User=[USER] ExecStart=/bin/bash [PATH]/main.sh Environment=DISPLAY=:0.0 Restart=always RestartSec=3s [Install] WantedBy=multi-user.target ``` ``` # 使用user權限常駐執行(有先執行bashrc) [Unit] Description=Run forever After=[name].service [Service] Type=simple WorkingDirectory=%h ExecStart=/bin/bash -i [PATH]/main.sh Restart=always RestartSec=3s [Install] WantedBy=default.target ``` 4. 設定檔案權限 ```shell sudo chmod 644 /etc/systemd/system/[NAME].service ``` 5. 重新載入設定 ```shell # 以root權限執行 sudo systemctl daemon-reload # 以user權限執行 systemctl --user daemon-reload ``` ### 操作指令 - 啟動服務 ```shell # 以root權限執行 sudo systemctl start [name].service # 以user權限執行 systemctl --user start [name].service ``` - 停止服務 ```shell # 以root權限執行 sudo systemctl stop [name].service # 以user權限執行 systemctl --user stop [name].service ``` - 查看服務狀態 ```shell # 以root權限執行 sudo systemctl status [name].service # 以user權限執行 systemctl --user status [name].service ``` - 開機自動啟動服務 ```shell # 以root權限執行 sudo systemctl enable [name].service # 以user權限執行 systemctl --user enable [name].service ``` - 取消開機自動啟動服務 ```shell # 以root權限執行 sudo systemctl disable [name].service # 以user權限執行 systemctl --user disable [name].service ``` - 列出所有服務 ```shell # 以root權限執行 sudo systemctl list-units --type=service --all # 以user權限執行 systemctl --user list-units --type=service --all ```