Is there a way of setting a service name dynamically in a service file?

I am running several instances of a service, where all services uses different input arguments. For debugging this is not optimal as all services are called the same and it can be tedious finding out which service is actually complaining. So what I want is to give the service a unique name for a given input. Systemds service files support this by changing the arg[0] in ExecStart by setting the prefix "@" where the second argument will be the arg[0] value.

Information about ExecStart command and the @ prefix: https://www.freedesktop.org/software/systemd/man/systemd.service.html

So this is what I have so far in my service file:

ExecStart=@/usr/bin/foo foo-%I %I

The input argument is a file, so it will be called something like bar-123.json. This works and the service name will be foo-bar-123.json when I use top, ps or the QA tools we are using. The input files are created by another service that also starts the "foo" service instance using systemctl dbus api.

However having the service called foo-bar-123.json is not the best and instead I would like to call is foo-bar.

I have tried running, as a test:

**ExecStartPre=/bin/sh -c 'export NAME=bar'**
**ExecStart=@/usr/bin/foo foo-${NAME} %I**

But then the name will be "foo-". Am guessing this is to be expected as the service file is not a shell script, however that was the only solution I could think of.

Other solution to the problem are also welcomed. I know that PR_SET_NAME in the prctl function should also change the name of the process but I am not sure who they relates to each other. The reason I don't want to use the prctl function is that is says that the name only can be 16 bytes long including the null byte, this is a limitation that I would be nice not to have.

  • Have you looked into Service Templates (aka instantiated services)? ibug.io/blog/2019/07/systemd-service-template Apr 12, 2021 at 20:08
  • You mean to only pass in the "bar" part and then letting service at runtime add the "123.json" part to find the file? That could work... not sure about if I like the service to have the logic for getting the "123" part. Apr 12, 2021 at 20:54

1 Answer 1


Usually there are two ways to work with systemd service unit instances. Either use files in /etc/sysconfig per instance which override eg. default OPTIONS variable (EnvironmentFile in service definition would load such file and populate OPTIONS, see for example systemctl show -p ExecStart sshd) or override more unit definitions via "drop-in" directory, eg. <service>@instance.service.d.

BTW, instance MUST have '@', otherwise it won't be an instance.

See how is it used in getty service units.

$ ls -l /usr/lib/systemd/system/getty@.service
-rw-r--r-- 1 root root 1958 Apr  9 00:22 /usr/lib/systemd/system/getty@.service
$ grep ^ExecStart /usr/lib/systemd/system/getty@.service
ExecStart=-/sbin/agetty -o '-p -- \\u' --noclear %I $TERM
$ systemctl -t service --state running list-units | grep getty
getty@tty1.service       loaded active running Getty on tty1 

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.