Shutdown command vs cron jobs

I have a number of computers and each computer is starting cron jobs left and right.

Those are both server computers and desktop computers.

On desktop computers it would be inconvenient if a user would shutdown the computer while a cron job is running.

I am thinking of mv-ing the shutdown command, and have users shutdown via a dedicated function, which would in turn, run the shutdown command, but only once all cron jobs have stopped running.

Am i mis thinking this somehow?

Or does the shutdown command already take cron jobs into consideration?

Asked By: john-jones

||

The shutdown command doesn’t know anything about cron jobs, but if you’re on a modern system then shutdown is actually handled by systemd, and systemd knows about inhibitors, which allow you to prevent certain activities — like system shutdown, suspend, etc — while a command is running.

You can use the systemd-inhibit command to run a command with an inhibitor lock. So if you have a cron job that does something that shouldn’t be interrupted — like making a system backup, or installing package upgrades — you can replace something like…

0 0 * * * /path/to/backup-script.sh

With:

0 0 * * * systemd-inhibit --what=shutdown /path/to/backup-script.sh
Answered By: larsks
Categories: Answers Tags: ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.