tl;dr: Use this shell command in my crontab:
@reboot sh -c "while ! systemctl status network-online.target > /dev/null; do sleep 1; done; exec /foo/bar.sh"
The problem I've encountered is, sometimes /foo/bar.sh
would failed at boot. After digging into details, I found it's because the network was not ready, therefore the script could not get network interface information.
So the solution is quite simple, just make sure it's run after our network is ready. In systemd your can just use Wants=
and After=
, but we want to use this information in the command line. Fortunely, systemctl's status
provides this information via the return value, so that's it: use a loop and wait until our network environment is ready.
Of course, this requires your network is managed by systemd, but if it's managed by other tool, I guess the same idea still works.