The Cookie Machine - Click here to drag window

DUMMY TEXT - Real text set in assets/js/theCookieMachine.js

Views: 2,009     Votes:  2     ✅ Solution
Tags: 16.04   bash   shutdown  
Link: 🔍 See Original Answer on Ask Ubuntu 🔗

Title: Intercept shutdown call and run script to allow or prevent shutdown
ID: /2018/02/27/Intercept-shutdown-call-and-run-script-to-allow-or-prevent-shutdown
Created: February 27, 2018    Edited:  June 12, 2020
Upload: November 24, 2022    Layout:  post
TOC: false    Navigation:  false    Copy to clipboard:  false

Use systemd inhibitors

Original Post is in section below and details how to intercept /sbin/shutdown command. This section is based on systemd inhibitors.

From systemd inhibitor:


systemd-inhibit — Execute a program with an inhibition lock taken


systemd-inhibit [OPTIONS...] [COMMAND] [ARGUMENTS...]

systemd-inhibit [OPTIONS...] --list


systemd-inhibit may be used to execute a program with a shutdown, sleep, or idle inhibitor lock taken. The lock will be acquired before the specified command line is executed and released afterwards.

Inhibitor locks may be used to block or delay system sleep and shutdown requests from the user, as well as automatic idle handling of the OS. This is useful to avoid system suspends while an optical disc is being recorded, or similar operations that should not be interrupted.

For more information see the Inhibitor Lock Developer Documentation.


The following options are understood:






-h, --help


Exit status

Returns the exit status of the executed program.


# systemd-inhibit wodim foobar.iso

This burns the ISO image foobar.iso on a CD using wodim(1) Note: link is broken in systemd documentation, and inhibits system sleeping, shutdown and idle while doing so.

See Also

systemd(1), logind.conf(5)

Original Post

In concept it seems pretty easy. Simply find the command, rename it, and replace it with your own script that calls the renamed version:

$ type -a shutdown
shutdown is /sbin/shutdown
$ sudo mv /sbin/shutdown /sbin/shutdownoriginal

Then edit your own script in /sbin/shutdown containing at the very least:


Then mark your script as executable for everyone:

$ sudo chmod a+x /sbin/shutdown

Voila! Everything that calls shutdown now calls your script which then calls the original commmand.

In reality by the time your script gets called things may not be in the state you expect. For example I put in some commands to record the shutdown but they do not appear to work:

echo "/sbin/shutdown custom script calling /sbin/shutdownoriginal"
echo "Last shutdown: $shutdowntime" >> /home/rick/shutdownlog.txt

The first echo should have appeared in /var/log/syslog but it did not. The second echo should have appended a line to the log file but it did not. This tells me that by the time the /sbin/shutdown command is executed system logging is already turned off and the file I/O system is shutdown.

A better approach would be looking at systemd shutdown target and/or input inhibitors. I’ll leave this answer here though for others that might think it could/would/should work.

As always remember YMMVYour Mileage May Very.

⇧ Nautilus gives a warning when opened in the current directory from the terminal Automatic tabulation when writing code on Gedit?  ⇩