1

I always shutdown my Linuxes with Ctrl-Alt-Del from a virtual console (outside any window mangager - KDE). Now I want that the system still shutdowns with Ctrl-Alt-Del, but only if certain CONDITION is satisfied. (That condition is the existence of a file.)

Something like this:

#!/bin/bash

if [[ ! CONDITION ]]; then

    echo
    echo "  *************************"
    echo "  *************************"
    echo "  **                     **"
    echo "  **   WRONG CONDITION   **"
    echo "  **                     **"
    echo "  *************************"
    echo "  *************************"
    echo
else
    sudo shutdown -h now
fi

I guess that I can do a service/target thing, but I have failed miserably....

EDIT: I do not need this to be through anything special, in particular, not with a service/target. This was just the thing I tried. I just want to Ctrl-Alt-Del from a virtual console after logout, and the system to execute the script above instead of the system poweroff. Maybe by recreating a symlink pointing to my script instead of poweroff, or whatever.

20
  • this can be very easy, or involve a lot of "working around": it's easy if you can write a program that will exit the moment that CONDITION is fulfilled, and you have a way of noticing when CONDITION stops being fulfilled. What is that CONDITION? As in, logically/technically, not in terms of bash scripting Commented Oct 29, 2024 at 12:09
  • I want to shutdown with Ctrl-Alt-Del as always, but only if CONDITION is satisfied. The actual CONDITION is not relevant. Commented Oct 29, 2024 at 12:10
  • it really is relevant, as I try to explain… your approach doesn't work, but there's easy ways to do something that works, but they depend on the nature of your condition. You will need to help us help you by actually answering questions! Commented Oct 29, 2024 at 12:11
  • Existence of a file. Commented Oct 29, 2024 at 12:11
  • 1
    @LuisA.Florit please edit your question and clarify exactly what you need. Do you really need this to work from a non-GUI environment? Do you need it to be done via systemd? Would you be open to just using a different shortcut that maps to running a simple script? As you can see, we are having trouble understanding what you need. Commented Oct 29, 2024 at 17:24

1 Answer 1

0

Two general ways to approach this (sadly, yours is neither!):

  1. (easy) you write a program, shell script, anything, that exits as soon as CONDITION becomes fulfilled. You call that program using systemd-inhibit --what=shutdown myblockingprogram. Because you don't want to figure out whether it's been started before, and you wouldn't want to have a loop polling the CONDITION, ideally, this execution would automatically be triggered by some action that causes a notification.
    In your case, where CONDITION is "existence of a file", that's a bit complicated in a shell script. But sure, either you have a loop doing the above invocation after checking existence of the file, with short sleep in between, so that you don't burn a CPU core just on checking. More elegantly:
  2. (more general) you write your own shutdown inhibitor. That's explained in detail here, in the official documentation. You write a program (shell programming is not sufficient here; but python, C++, Go, Rust or C would totally do, for example) that calls the Inhibit() functionality, which returns a file descriptor. The moment you close that file descriptor, shutdowns begin to be allowed again.
    So, in this scenario, you would use inotify or fanotify to get notified when the target file gets created. When it gets created, you call the Inhibit functionality, get a file descriptor. You use inotify/fanotify to get notified about the file getting deleted. Once that file disappears, you close the file descriptor. You start this program automatically as systemd user or system service.
6
  • Does systemd-inhibit show an explanation what was inhibited, like in my script? My file is in a pen drive. I can also check if the pen drive is plugged in (not just mounted)? Commented Oct 29, 2024 at 12:40
  • where would you show that, even? That makes no sense; your script could only show that message on the console it's being run from, and people don't leave a console open to watch for such messages. But yes, some desktop environments will inform you that, and some even which, a program has inhibited shutdown. Commented Oct 29, 2024 at 13:39
  • I like to see the booting messages, so I have disabled the graphic boot. So, I always do Ctrl-Alt-Del in the virtual console, after logging out from KDE. Commented Oct 29, 2024 at 14:29
  • well, OK, that still wouldn't make much sense; your script would have to have access to the current console. Anyways, yeah, again, your script approach doesn't work, you'll have to do something different – which is why I wrote this answer. If you want, you can always write to whatever thing you want to write to make a message shown in your inhibitor as described above. That's up to you. Commented Oct 29, 2024 at 14:30
  • echo "..." > /dev/console works perfectly for me. Commented Oct 29, 2024 at 14:32

You must log in to answer this question.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.