The Cookie Machine - Click here to drag window

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

If you can read me, I'm broken!

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

URL: https://askubuntu.com/q/1010124
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: April 8, 2024    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:

Name

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

Synopsis

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

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

Description

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.

Options

The following options are understood:

--what=

--who=

--why=

--mode=

--list

-h, --help

--version

Exit status

Returns the exit status of the executed program.

Example

# 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:

#!/bin/bash
/sbin/shutdownoriginal

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"
shutdowntime=`date`
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?  ⇩