How to kill wine processes when they crash or are going to crash?

Sometimes it happens that a Wine application crashes (slowing down the system and making it almost unusable). In most cases I'm able to kill the program with xkill, but sometime I've to restart as Ubuntu seems not to respond very well (the only thing that works is ALT+F2, the launcher; xkill doesn't). I've tried to use wineboot -r or -f but they don't seem to work very well.. If something is unclear, let me know I'll try to explain better :)


You can safely kill wine sessions either via ALT+F2 or via a terminal by typing

wineserver -k

If it is really doesnt want to shutdown then you can force it via

wineserver -k9

killall nameofexefile.exe

just like linux processes


Well, as a wine programmer, I often will munge up the whole damn thing, so I use my super special killwine script. This is a hard death (wineserver -k is the nice way to do it and always preferred).

#!/bin/bash

wine_cellar="${HOME}/.local/share/wine"

if (($#)); then
    if [[ -e "${wine_cellar}/$1" ]]; then
        WINEPREFIX="${wine_cellar}/$1"
        shift
    elif [[ "${1:0:1}" != "-" ]]; then
        echo "ERROR: Didn't understand argument '$1'?" >&2;
        exit 1
    fi
fi

if ((${#WINEPREFIX})); then
    pids=$(
        grep -l "WINEPREFIX=${WINEPREFIX}$" $(
            ls -l /proc/*/exe 2>/dev/null |
            grep -E 'wine(64)?-preloader|wineserver' |
            perl -pe 's;^.*/proc/(\d+)/exe.*$;/proc/$1/environ;g;'
        ) 2> /dev/null |
        perl -pe 's;^/proc/(\d+)/environ.*$;$1;g;'
    )
else
    pids=$(
        ls -l /proc/*/exe 2>/dev/null |
        grep -E 'wine(64)?-preloader|wineserver' |
        perl -pe 's;^.*/proc/(\d+)/exe.*$;$1;g;'
    )
fi

if ((${#pids})); then
    set -x
    kill $* $pids
fi

This assumes that you're wine prefixes are under ~/.local/share/wine. Usage examples are:

killwine                       # Just kill all instances of wine
killwine -9                    # Hard kill them all
killwine lotro                 # Only kill wine under ${HOME}/.local/share/wine/lotro
killwine -INT lotro            # Same as above, but use SIGINT
WINEPREFIX=/tmp/crap killwine  # Kill only the instance under /tmp/crap
sudo reboot                    # Pretend you're running windows.

I don't know, but I don't think you'll often end up with various processes hung in memory (what this script takes care of) on a normal or even normal+staging release, but I do quite a lot because of hacking the server and ntdll.

EDIT: This script will only work on a Linux-based OS and assumes that the proc file system is mounted on /proc, etc.