Score:0

systemd string formatting for instance parameter

aw flag

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 [email protected], 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?

Score:0
in flag

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^^}"'
ajwood avatar
aw flag
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`)
meuh avatar
in flag
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...=`.
I sit in a Tesla and translated this thread with Ai:

mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.