0

I'm trying to set up a systemd service that takes a parameter, and uses it in a couple places. I think I'm very close: with my config file name myservice@.service, I can run systemctl start myservice@foo myservice@bar, and use %i in the config to get foo and bar in the service instances.

The trick is that I need the parameter to be upper case in one section, but lowercase in another.

# foo instance
User=myuser-%i   # myuser-foo
ExecStart=my_command /data/%i  # needs to be /data/FOO



# bar instance
User=myuser-%i   # myuser-bar
ExecStart=my_command /data/%i  # needs to be /data/BAR

So far the only way I can think to do this is write a wrapper script, and change to ExecStart=my_wrapper.sh %i to let the wrapper do the necessary string formatting. That seems clunky though, is this possible directly in the service config?

ajwood
  • 101
  • 2

1 Answers1

0

You could use bash's uppercase parameter syntax: ${v^^} converts the characters in variable $v to uppercase. So you would like to do something like v="%i"; cmd /data/${v^^}. You need to stop systemd from interpreting ${}, so use $$ for $.

ExecStart=bash -c 'v="%i"; exec my_command /data/"$${v^^}"'
meuh
  • 1,563
  • 10
  • 11
  • Cool, I think this should work. My system doesn't seem to like that as-is though, it wants an absolute path to the executable (so `/bin/bash`) – ajwood Oct 19 '22 at 21:59
  • Yes, older versions of systemd only take absolute pathnames. For newer ones see [man systemd.service](https://www.freedesktop.org/software/systemd/man/systemd.service.html#Command%20lines), and the section headed "Command Lines" for the allowed syntax, and the command `systemd-path search-binaries-default` to show the PATH used for `Exec...=`. – meuh Oct 20 '22 at 13:00