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; }

1 Answer 1

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'
2
  • That works, thank you! Any ideas why the grouping solution with { } doesn't work? Commented Mar 1, 2023 at 19:25
  • Because systemd-run accepts one command with optional arguments. You can't expect it to understand arbitrary shell code. Commented Mar 1, 2023 at 19:32

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.