Execute a call to an API before reboot and after boot

Hi

I want to call an api with curl before reboot or shutdown -r now and after boot for maintenance propose, only when I call reboot manually and came out with this.

It works, but it's calling the api on shutdown now/halt too.

The ideia it to call this api to disable monitoring on reboot and enable it on boot.

Any suggestions?

[Unit]
Description = Maintenance mode
Before = reboot.target
After = network-online.target boot-complete.target
Requires = network-online.target
Conflicts = halt.target poweroff.target

[Service]
User = root
Group = root
Type = oneshot
RemainAfterExit = yes
ExecStart = "/usr/local/bin/maintenance-on.sh"
ExecStop = "/usr/local/bin/maintenance-off.sh"

[Install]
WantedBy = multi-user.target

Thanks

Comments

  • Cron job for on reboot.

    Can't think of a good way to do on shutdown though. Most stuff gets killed pretty fast on shutdown command being issued. If you have a 2nd machine you could just watch till the machine goes down I guess via ping

  • Maybe another approach could be defining your script as alias for shutdown, trigger your API call there and then hand off the further processing to the "real binary"?

  • My guess would be the ExecStop is getting called when network-online goes away. If you must use systemd, I would suggest you have one unit file with ExecStart=maintenance-on.sh before reboot.target, and a second unit file with ExecStart=maintenance-off.sh after network-online.target.

    However, I would also recommend that before reboot, you make an API call that disables monitoring for 5 minutes instead of turning it off entirely. Otherwise you will end up with a situation where the instance fails to come back up properly after a reboot and your monitoring happily ignores it because the startup script never ran to turn monitoring back on.

    Thanked by (1)yoursunny
  • I’d just create an alias for the shutdown script, and a cron entry for boot up script as suggested before.

  • Yes, a cron @reboot entry and a function/alias should be an easier solution

    Thanks for you feedback

Sign In or Register to comment.

This Site is currently in maintenance mode.
Please check back here later.

→ Site Settings