If I run a systemctl command for a non-existant service (foo.service) (e.g. systemctl stop foo.service), systemd fails (exit code 5, & stderr: Failed to stop foo.service: Unit foo.service not loaded.).
Is it possible to detect, in my bash script, if a service (or .timer) would fail like that before running the systemctl command? systemd has systemctl is-active NAME.service. There is no systemctl is-loaded. What's the Best™ way to detect this? systemctl list-units? systemctl status?
I am writing a script which restarts (well try-restarts) a .timer or .service. If the timer doesn't exist, I don't want an error to happen. If it does exist, and the try-restart fails, I want to detect, and raise, that error.
systemd v245 etc on latest Ubuntu LTSs (e.g. 20.04)