Linux – Enabling ‘linked’ unit files in Systemd

linuxsystemd

I am still getting to grips with systemd and have run into something. It's not so much a problem, but I would like to learn more about the way this is. I could not find any reference to this elsewhere.

First off, I understand that custom unit files for services should go in /etc/systemd/system. However, it would be nice for management of our servers if the unit files could be located elsewhere.

In the documentation, I saw that you may 'link' unit files like so:

systemctl link /path/to/servicename.service

This will create a link to the above in /etc/systemd/system. You are now able to start/stop this service. On the surface, this seemed like a good way for us to manage our services.

However, trying to enable such a 'linked' unit file results in failure:

root@test1:/etc/systemd/system# systemctl link /root/myservice.service 
Created symlink from /etc/systemd/system/myservice.service to /root/myservice.service.

root@test1:/etc/systemd/system# systemctl status myservice.service 
 * myservice.service - My Test Service
     Loaded: loaded (/root/myservice.service; linked; vendor preset: enabled)

root@test1:/etc/systemd/system# systemctl enable myservice.service
Failed to execute operation: No such file or directory

Using the exact same unit file, but copied in to /etc/systemd/system instead of linked in, you get:

root@test1:/etc/systemd/system# cp -p /root/myservice.service .

root@test1:/etc/systemd/system# systemctl daemon-reload 

root@test1:/etc/systemd/system# systemctl status myservice.service 
 * myservice.service - My Test Service
     Loaded: loaded (/etc/systemd/system/myservice.service; disabled; vendor preset: enabled)

root@test1:/etc/systemd/system# systemctl enable myservice.service
Created symlink from /etc/systemd/system/multi-user.target.wants/myservice.service to /etc/systemd/system/myservice.service.

From this, it seems that it is not possible to enable linked in unit files to be called at system startup.

If this is the case, what is the point of the 'link' functionality? From the docs, it says:

link FILENAME

Link a unit file that is not in the unit file search paths into the unit file search path. This requires an absolute path to a unit file. The effect of this can be undone with disable. The effect of this command is that a unit file is available for start and other commands although it is not installed directly in the unit search path.

Best Answer

The man page is misleading.

systemctl link /root/myservice.service

systemctl enable /root/myservice.service

The first makes it possible for you to do systemctl start myservice. The second makes it possible for myservice to be started automatically (which, as @Julien pointed out, automatically adds the link).

I think... I've been trying to wrap my head around this all day.