2

When you shut down a macOS system, it asks applications to exit one by one, and the system will not shut down unless all applications agree to exit (aka. there is no unsaved work) or until they are manually forced to exit.

When you shut down a Windows system, it gives you a grace period when you can see which apps are refusing to close. If it reminds you that you didn't save something, you can stop the shutdown during this grace period.

However, when you shut down a Linux system through the window manager, whether KDE or Gnome, it instantly shows a black screen, killing every process without checking if they're saved.

A lot of my work has been lost because of the lack of saving prompts during shutdown. Can I configure something on Linux that will show me unsaved applications before shutting down?

3
  • 3
    Different philosophy, I guess. Linux is not designed to babysit you, it's designed to obey you. If you tell it to shut down (or remove a file, or…) and you have the right permissions then the system will shut down (or remove the file, or…). A responsible admin uses shutdown -h +5 rather than … now. Users get wall messages and/or desktop notifications and it's their responsibility to save their work. If you are the only user then you know when you want to shut the OS down, so save your work beforehand. OTOH an obeying OS should be able to act as you want it, the question makes sense. Commented Oct 23, 2022 at 22:05
  • Your work hasn't been lost because of the lack of a warning or prompt, you lost your work because you didn't check if it was saved before you shut down the machine and subsequently killed running processes. If you use any tool in a possible harmful way then you are responsible for the damage, not the tool. Commented Oct 24, 2022 at 1:38
  • Actually systemd has something that sort of provides such feature: freedesktop.org/software/systemd/man/systemd-inhibit.html
    – Tom Yan
    Commented Oct 25, 2022 at 5:06

1 Answer 1

2

Like the comments said, the shutdown script in Linux is designed to power off without checking for other running apps (and it has its benefits that way). If you really want to do some checks before powering off the machine, you need to write your own script to do it. For example, something like this:

#!/usr/bin/env bash

## Applications to check before shutting down
apps='(nvim.*|calcurse|aerc)' # add your apps here

ps -eo pid,cmd | grep -E "[0-9]+ $apps" | while read pid process; do
  echo -n "$process is still running. Kill it? [Y/n] "
  read -r yn </dev/tty
  if [[ $yn =~ ^[Yy]?$ ]]; then
    kill $pid
  else
    exit 1
  fi
done && systemctl poweroff

This script retrieves all the running processes, matches them against the apps defined by apps, then prompts you to kill them. If you enter no to any of them, it exits immediately. Otherwise, it shuts down the system.

Put this file in your $PATH and make an alias to execute it when you run your normal shutdown command. For example, instead of running shutdown, add this to your bash aliases:

alias shutdown='my_shutdown_script.sh'

You can do the same thing with reboot too. Just modify the script to take an argument/flag. You get the idea :)

Edit: If (neo)vim is the only app you'd like to check, I recommend looking into mksession

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .