How can I make my cronjob friendly to reboot and other interruptions?

I have as part of my cronjob a variable random delay. This has good reasons to exist, it's to distribute the load of the automated process from spiking on various minute or hour boundaries.

I suspect (but am not certain) that sometimes when I reboot the machine, systemd hangs uncomfortably for a variable length of time "waiting for anacron jobs". This does not happen often enough to be worth deep-diving for source of truth, but given that I only ever set up one cronjob on my computers it seems a likely culprit.

I'm curious to know if there is an industry standard simple technique that can be used within a script that is configured as a cron job to make it interruptible. The idea here is that if I command my system to reboot, under no circumstance do I need this job (which has a variable random length sleep before it does its actual work) to complete and I want it to be interrupted and nuked from orbit when the system goes for reboot. Is there some way to do this? Will cron or the system send a signal to the configured job script? Must I resort to a custom incantation to check for jobs that are running first, terminate them and stop them from restarting, and then issue the reboot?


Instead of using cron, you could create a systemd service

When creating the service you can set the KillMode and KillSignal options in the service options.