Home > Software engineering >  How to use the ">" operator in a systemd file?
How to use the ">" operator in a systemd file?

Time:11-19

This is my systemd service file:

[Unit]
Description=Foo

[Service]
WorkingDirectory=/tmp/test
ExecStartPre=/usr/bin/yq -o json config.yaml > config.json
ExecStart=/usr/bin/foo run -c config.json

[Install]
WantedBy=multi-user.target

Everything working if I run ExecStartPre and ExecStart in the shell. But got error when I run systemctl start foo.service:

Error: open >: no such file or directory

Seems the ">" operator doesn't work as shell in the systemd file.

CodePudding user response:

ExecStartPre doesn't use the shell to execute the command, so you can't use shell operators like redirection.

You'll need to invoke the shell explicitly.

ExecStartPre=bash -c '/usr/bin/yq -o json config.yaml > config.json'

CodePudding user response:

Try:

ExecStartPre=/usr/bin/yq -o json config.yaml | tee -a config.json

or:

ExecStartPre=/bin/bash -c '/usr/bin/yq -o json config.yaml > config.json'
  • Related