【发布时间】:2019-07-24 15:08:27
【问题描述】:
我有一个应用程序,为此我编写了一个 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 myapp、systemctl disable myapp 和systemctl 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 myapp、systemctl stop myapp、systemctl status myapp 进行控制。 systemctl is-enabled myapp 中的 linked 状态是否意味着它不会在启动时启动?我尝试检查 systemctl 的手册页,但找不到该状态。
【问题讨论】:
标签: linux service systemd systemctl