【问题标题】:systemctl enable and disable a custom service at bootupsystemctl 在启动时启用和禁用自定义服务
【发布时间】: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 myappsystemctl stop myappsystemctl 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 myapp 中的 linked 状态是否意味着它不会在启动时启动?我尝试检查 systemctl 的手册页,但找不到该状态。

【问题讨论】:

    标签: linux service systemd systemctl


    【解决方案1】:

    我无法在 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

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

    【讨论】:

      猜你喜欢
      • 2021-08-31
      • 1970-01-01
      • 1970-01-01
      • 2018-03-29
      • 1970-01-01
      • 1970-01-01
      • 2018-08-27
      • 2021-07-31
      • 1970-01-01
      相关资源
      最近更新 更多