Gracefully Closing Programs

In my ongoing efforts with learning to use a VPN client, I wanted a method of closing key internet programs. Somewhat like a VPN kill switch. The software I wanted to close included Firefox, Thunderbird, and QuiteRSS.

None of those three programs closed gracefully with kill -SIGTERM. Instead all three abruptly terminated.

The little problem turned out to be a challenge.

To my original shell script wrapper that manages my ProtonVPN connection I added a function to gracefully close affected software. I used the xwinfo command to obtain a window ID. I used xdotool to activate the window and send the Alt+F4 keyboard shortcut to close the window. The function snippet looks like this:

    Terminate_Internet_Apps () {
    WID="$(xwininfo -root -tree -int | grep "$1" | awk ‘{print $1}')"
    if [ "$WID" != "" ]; then
      MSG="Attempting to close $1 (window ID $WID)"
      xdotool windowactivate --sync "$WID"
      sleep 1
      xdotool key --clearmodifiers alt+F4 2>/dev/null
      MSG="Exit code: $EXIT_CODE"

The snippet works well.

Posted: Category: Usability Tagged: General

Next: VirtualBox Snapshots With Raw Disk Access

Previous: Updating Slackware — 5