繁体   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