简体   繁体   中英

systemctl enable and disable a custom service at bootup

I have an application for which I've written a myapp.service file and created a symlink for it in /etc/systemd/system/ .

The myapp.service file is like this:

[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

I can use systemctl start myapp , systemctl stop myapp , systemctl status myapp to start, stop, and view the status of the service, and it works very well. I was hoping I could also use systemctl enable myapp , systemctl disable myapp , and systemctl is-enabled myapp to control whether myapp is automatically launched when the system is booted up. When I ran systemctl is-enabled myapp , it showed linked as the output. So I tried systemctl disable myapp and it deleted the symlink to /etc/systemd/system/myapp.service (the output was: Removed symlink /etc/systemd/system/myapp.service. ). After that I couldn't run systemctl enable myapp , it just gave this output: Unit myapp.service could not be found.

What is the correct way to create a service such that it can be enabled and disabled with systemctl ? I even tried doing it with sshd and was not able to enable after disabling it.

$ 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.

Ultimately I just need to ensure that the application does not start at bootup, but can still be controlled with systemctl start myapp , systemctl stop myapp , systemctl status myapp . Does the linked status from systemctl is-enabled myapp mean it will not start at bootup? I tried checking the man page of systemctl, but couldn't find that state.

I can't reproduce on debian (version 244.3-1)

I created /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

As expected, enable / disable creates/deletes a symbolic link to your service in /etc/systemd/system/multi-user.target.wants/ . It does not touch /etc/systemd/system/*.service .

I also see that my console messages are slightly different. Which distro/version are you using?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM