You can check the target you need to be active in order for your script to run. For example, in order for the network to be accessible, you need to check the network-online.target
(systemctl check
will return 0 if the checked unit is active)
The target systemd tries to reach is its "default" target, which can be queried by systemctl get-default
. If that target is online, the system has finished booting (so you can issue systemctl status $(systemctl get-default)
to check whether the boot process has finished)
Edit:
If you want to monitor only the current state of the system, and you aren't interested in targets (i.e. you want to run your command only when everything is up and running), it is better to use the systemctl is-system-running
command. It will output running
is everything is fine, or degraded
if the system is up but some of the services have failed (which may or may not indicate an error). The other states would indicate that your script shouldn't run. As a side note, it's usually better to check return values instead of parsing the text output, but the manpage doesn't specify return values, only says that anything other than "running" results of a nonzero exit code, so it might be safer to parse the output text.
See the manpage of systemctl
for the possible output values.
After=network-online.target
and/orWants=network-online.target
to start it after the network.