0

Why can't I pass a command group to systemd-run like this?

$ systemd-run --on-active=1 { cp file1 file2 && echo hi; }
Failed to find executable {: No such file or directory

I tried various combinations of quoting, braces, and subshells but to no avail. I feel like this might be a problem with the shell-linux divide.

I looked into https://www.freedesktop.org/software/systemd/man/systemd.service.html#Command%20lines but couldn't find a solution there either.

I could of course put the commands into a script, but I really want to know what I am missing here. Any ideas?


Context, because maybe there is a different solution entirely:

I am trying to create a solution to reset network settings after a certain countdown by copying back a previously created backup file, unless another command deletes it first (when the new network settings are successful).

I am attempting a solution with a transient timer with systemd-run that executes two commands, where the restart is only executed when cp succeeds:

systemd-run --on-active=300 { cp /etc/systemd/network/10-wired.network.backup /etc/systemd/network/10-wired.network && systemctl restart systemd-networkd; }

ckattmann
  • 3
  • 3

1 Answers1

0

You can start a shell and pass your commands in the command string:

systemd-run --on-active=300 sh -c '
    cp /etc/systemd/network/10-wired.network.backup /etc/systemd/network/10-wired.network &&
        systemctl restart systemd-networkd.service'
Freddy
  • 25,172
  • 1
  • 21
  • 60
  • That works, thank you! Any ideas why the grouping solution with `{ }` doesn't work? – ckattmann Mar 01 '23 at 19:25
  • Because `systemd-run` accepts one command with optional arguments. You can't expect it to understand arbitrary shell code. – Freddy Mar 01 '23 at 19:32