9

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.

12

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.

  • 1
    note that systemctl enable will also do systemctl link, so no need to type the 2 commands ;-) – Julien May 2 '17 at 11:45
  • @Julien Oh, where we you when I wrote this last year :-) I think I finally realized that last month! – Auspex May 3 '17 at 16:16
8

When enabling a service from another path than the default paths you should use the full path. Enable will also create the link for you:

systemctl enable /root/myservice.service

Once enabled you can start/stop/status with the service name

systemctl start myservice

A few caveats here:

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.