How can I use spaces in systemd command line arguments?

前端 未结 7 1418
情话喂你
情话喂你 2021-01-17 11:52

I have a systemd unit with spaces in an argument

ExecStart=command --argument=\"text text\"

It seems that systemd does not recognize the do

相关标签:
7条回答
  • 2021-01-17 11:55

    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
    
    0 讨论(0)
  • 2021-01-17 12:05

    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

    0 讨论(0)
  • 2021-01-17 12:09

    Try escaping the space, like so:

    ExecStart=command --argument="text\ text"
    

    (The quotes may or may not be necessary.)

    0 讨论(0)
  • 2021-01-17 12:12

    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"
    
    0 讨论(0)
  • 2021-01-17 12:13

    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}
    
    0 讨论(0)
  • 2021-01-17 12:15

    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.

    0 讨论(0)
提交回复
热议问题