簡體   English   中英

systemctl 在啟動時啟用和禁用自定義服務

[英]systemctl enable and disable a custom service at bootup

我有一個應用程序,我為它編寫了一個 myapp.service 文件並在/etc/systemd/system/為它創建了一個符號鏈接。

myapp.service 文件是這樣的:

[Unit]
Description=My Application
After=network.target

[Service]
Type=simple
Restart=always
RestartSec=1
StartLimitInterval=0
User=myuser
ExecStart=/var/opt/myapp/myapp

[Install]
WantedBy=multi-user.target

我可以使用systemctl start myapp , systemctl stop myapp , systemctl status myapp來啟動、停止和查看服務的狀態,效果很好。 我希望我也可以使用systemctl enable myappsystemctl disable myappsystemctl is-enabled myapp來控制系統啟動時是否自動啟動 myapp。 當我運行systemctl is-enabled myapp ,它顯示為輸出linked 所以我嘗試systemctl disable myapp並刪除了指向/etc/systemd/system/myapp.service的符號鏈接(輸出為: Removed symlink /etc/systemd/system/myapp.service. )。 之后我無法運行systemctl enable myapp ,它只是給出了這個輸出: Unit myapp.service could not be found.

創建服務以便可以使用systemctl啟用和禁用它的正確方法是什么? 我什至嘗試使用sshd執行此操作,但在禁用它后無法啟用。

$ systemctl is-enabled sshd
enabled

$ systemctl disable sshd
Removed /etc/systemd/system/multi-user.target.wants/ssh.service.
Removed /etc/systemd/system/sshd.service.

$ systemctl is-enabled sshd
Failed to get unit file state for sshd.service: No such file or directory

$ systemctl enable sshd
Failed to enable unit: Unit file sshd.service does not exist.

最終我只需要確保應用程序不會在啟動時啟動,但仍然可以通過systemctl start myappsystemctl stop myappsystemctl status myapp systemctl is-enabled myapplinked狀態systemctl is-enabled myapp意味着它不會在啟動時啟動? 我嘗試檢查 systemctl 的手冊頁,但找不到該狀態。

我無法在 debian(版本 244.3-1)上重現

我創建了/etc/systemd/system/test.service

me ~ $ sudo systemctl cat test.service
# /etc/systemd/system/test.service
[Unit]
Description=Test

[Service]
Type=oneshot
ExecStart=/bin/true

[Install]
WantedBy=multi-user.target

me ~ $ sudo systemctl enable test.service
Created symlink /etc/systemd/system/multi-user.target.wants/test.service → /etc/systemd/system/test.service.

me ~ $ sudo systemctl disable test.service
Removed /etc/systemd/system/multi-user.target.wants/test.service

正如預期的那樣, enable / disable/etc/systemd/system/multi-user.target.wants/創建/刪除指向您的服務的符號鏈接。 它不涉及/etc/systemd/system/*.service

我還看到我的控制台消息略有不同。 您使用的是哪個發行版/版本?

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM