Systemd: why isn’t it possible to use %i in the first argument of ExecStart=

systemd

To quote http://www.freedesktop.org/software/systemd/man/systemd.service.html#Command%lines:

The command line accepts "%" specifiers as described in systemd.unit(5). Note that the first argument of the command line (i.e. the program to execute) may not include specifiers.

I just ran into a use-case where I wanted to do this.

Why is this not possible? Is there a suggested workaround?

Best Answer

According to the author himself (poettering) in this thread : it is forbiden because the executable name might be needed in advance (e.g.: SELinux needs this).

But according to other in the same thread and in that one, is still works sometime-ish.

Given that most specifiers can be determined statically in advance (template, machine, etc.) it should get supported eventually.

In the meantime, one solution is to launch a shell, as noted in the question comments and in the first thread:

ExecStart=/bin/sh -c "exec opt/%i/bin/service --args"

Another solution would be to manually invoke the ELF interpreter:

ExecStop=/lib64/ld-linux-x86-64.so.2 /usr/local/bin/%i-cli stop

(Basically the same idea as manually running /usr/bin/perl script.pl instead of trusting the she-bang of the script)