问题
I have a systemd unit with spaces in an argument
ExecStart=command --argument="text text"
It seems that systemd does not recognize the double or single quotes and it splits up the argument into two arguments. Any idea how I can prevent that? I am using systemd v218 within CoreOS.
回答1:
This is actually surprisingly difficult to do, unfortunately. I stole this info from this answer. The only way to do it is to put your arguments in an environment file and then use them as variables as such (like in /etc/.progconfig):
ARG1=text
ARG2=text
Then import the environment file before running your command:
EnvironmentFile=/etc/.progconf
ExecStart = command $ARG1 $ARG2
回答2:
systemd only seems to recognize quotes which fully wrap arguments; i.e.
ExecStart=command "--argument=text text"
works but
ExecStart=command --argument="text text"
does not. I just ran into this issue and filed #624 about it.
回答3:
As Nico suggested, you can create an EvironmentFile in which you can specify an argument with spaces.
SPACEYARG="i love spaces"
In your unit file however, you'll need to wrap that argument in curly brackets in order for the spaces to be passed properly.
EnvironmentFile=/etc/.progconf
ExecStart = command ${SPACEYARG}
回答4:
I think recent versions of systemd have started accepting quotes in the middle of arguments, closer to what bash accepts. However, @Tgr's answer is still correct, and it's worth elaborating on. Quoting the entire argument, including the flag name, works here. If you do this:
ExecStart=command "--argument=text text"
Then systemd will understand --argument=text text
as a single positional argument. You don't have to worry about any more splitting happening on that space. You can see the same behavior in bash:
$ echo "--silly-flag=spaces are preserved here"
--silly-flag=spaces are preserved here
回答5:
Systemd service file supports this
Environment="TEST=one word"
Environment="TEST2=second word"
ExecStartPre=-/bin/echo start pre
ExecStartPre=/bin/echo start pre mandatory
ExecStart=/bin/echo started : ${TEST} $TEST2
ExecStartPost=-/bin/echo start post
ExecStartPost=/bin/echo start post mandatory
ExecStop=/bin/echo stop
ExecStopPost=-/bin/echo stop post
ExecStopPost=/bin/echo stop post mandatory
ExecReload=/bin/echo reload
log :
Mar 09 21:39:47 gitlab-runner-1 echo[30286]: start pre
Mar 09 21:39:47 gitlab-runner-1 echo[30288]: start pre mandatory
Mar 09 21:39:47 gitlab-runner-1 echo[30295]: started : one word second word
Mar 09 21:39:47 gitlab-runner-1 echo[30296]: start post
Mar 09 21:39:47 gitlab-runner-1 echo[30297]: start post mandatory
Mar 09 21:39:47 gitlab-runner-1 echo[30298]: stop
Mar 09 21:39:47 gitlab-runner-1 echo[30299]: stop post
Mar 09 21:39:47 gitlab-runner-1 echo[30300]: stop post mandatory
But you may actually want to set this if the app needs to read the whole string as 2 arguments, each argument between "" (not tested)
ExecStart=command "$ARG1" "$ARG2"
回答6:
Environment is a way to do it.
You can also use \s
as space, so ExecStart will be:
ExecStart=command --argument="text=\stext"
ref: https://www.freedesktop.org/software/systemd/man/systemd.service.html#Command%20lines
回答7:
Try escaping the space, like so:
ExecStart=command --argument="text\ text"
(The quotes may or may not be necessary.)
来源:https://stackoverflow.com/questions/28881758/how-can-i-use-spaces-in-systemd-command-line-arguments