On the DRY principle, rather than make a bunch of slightly different systemd configurations, I'd rather use one, symlinked or hard linked to different names like myservice1.service, myservice2.service, ...

With SysV initscripts, I can use $0 to determine the name of the start script, with Upstart .conf files there's the $UPSTART_JOB automatic variable, but I've been searching the documentation for systemd for something similar with no success. Any ideas?

2 Answers 2


Systemd has templates, which let a single unit file accept an argument and start different instances. The classic example would be getty:

~ systemctl cat [email protected] | grep Exec
ExecStart=-/sbin/agetty --noclear %I $TERM

If you do systemctl start getty@tty7, the %I is replaced with tty7 before systemd starts the service.

It would be easier to use this in your myservice.service file. Remember to name it [email protected], and use %I or %i in the ExecStart command line accordingly.



%p (escaped) or %P (unescaped) is the equivalent of $UPSTART_JOB. From Understanding Systemd Units and Unit Files:

The following are some of the more common specifiers will be replaced when an instance unit is interpreted with the relevant information:

  • %n: Anywhere where this appears in a template file, the full resulting unit name will be inserted.
  • %N: This is the same as the above, but any escaping, such as those present in file path patterns, will be reversed.
  • %p: This references the unit name prefix. This is the portion of the unit name that comes before the @ symbol.
  • %P: This is the same as above, but with any escaping reversed.
  • %i: This references the instance name, which is the identifier following the @ in the instance unit. This is one of the most commonly used specifiers because it will be guaranteed to be dynamic. The use of this identifier encourages the use of configuration significant identifiers. For example, the port that the service will be run at can be used as the instance identifier and the template can use this specifier to set up the port specification.
  • %I: This specifier is the same as the above, but with any escaping reversed.
  • %f: This will be replaced with the unescaped instance name or the prefix name, prepended with a /.
  • %c: This will indicate the control group of the unit, with the standard parent hierarchy of /sys/fs/cgroup/systemd/ removed.
  • %u: The name of the user configured to run the unit.
  • %U: The same as above, but as a numeric UID instead of name.
  • %H: The host name of the system that is running the unit.
  • %%: This is used to insert a literal percentage sign.

By using the above identifiers in a template file, systemd will fill in the correct values when interpreting the template to create an instance unit.

Credit goes to muru for the 'template' reference which improved my search results.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .