Created
January 16, 2023 06:37
-
-
Save jpsutton/28b186078dbec4a4efe144d86b11a64b to your computer and use it in GitHub Desktop.
The Division - Lutris Pre-Launcher Script
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/bash | |
# This is a quick-and-dirty script to monitor the execution of a process, wait for it to die, | |
# and then ensure that the Ubisoft client dies shortly thereafter. This is useful, as when | |
# running under Lutris, Ubisoft games spawn the Ubisoft client, and it remains running after | |
# the game itself has quit. On my system, the Ubisoft client remaining running will keep | |
# the screen awake, which is decidedly undesirable. | |
# Enable the following 3 lines for debugging, if needed | |
#set -x | |
#exec > >(tee -i ~/prelauncher_log.txt) | |
#exec 2>&1 | |
_EXEC='TheDivision\.exe' | |
# Thank you @xzfc ... https://superuser.com/a/784102/1759816 | |
pidtree() ( | |
[ -n "$ZSH_VERSION" ] && setopt shwordsplit | |
declare -A CHILDS | |
while read P PP;do | |
CHILDS[$PP]+=" $P" | |
done < <(ps -e -o pid= -o ppid=) | |
walk() { | |
echo $1 | |
for i in ${CHILDS[$1]};do | |
walk $i | |
done | |
} | |
for i in "$@";do | |
walk $i | |
done | |
) | |
# Watch for the executable to launch, then wait 30 seconds, as it will die and a new one will spawn | |
while [ /bin/true ]; do | |
sleep 0.1s | |
_OUT=$(ps -fww x | grep -P "$_EXEC"'[0]{0}$' | grep -v 'bin/wine') | |
if [ $? -eq 0 ]; then | |
sleep 30s | |
break | |
fi | |
done | |
# Wait for the new executable to launch, and then determine which parent process needs to have it's children killed | |
while [ /bin/true ]; do | |
sleep 0.1s | |
_OUT=$(ps -fww x | grep -P "$_EXEC"'[0]{0}$' | grep -v 'bin/wine') | |
if [ $? -eq 0 ]; then | |
_WATCH_PID=$(echo "$_OUT" | awk '{ print $2 }') | |
_KILL_PID=$(ps -fww x | grep -P "$_EXEC"'[0]{0}$' | grep python | awk '{ print $2 }') | |
break | |
fi | |
done | |
# Wait for the executable to disappear, then kill child PIDs of the python process and the python process itself | |
while [ /bin/true ]; do | |
sleep 1s | |
ps -p "$_WATCH_PID" > /dev/null 2>&1 | |
if [ $? -ne 0 ]; then | |
sleep 10s | |
_TO_DIE=$(pidtree "$_KILL_PID") | |
# Try to kill all children nicely | |
pkill -P "$_KILL_PID" | |
sleep 5s | |
# Check for each child process individually, and kill forefully if any are still running | |
for _PID_CHECK in $(echo $_TO_DIE); do | |
ps -p "$_PID_CHECK" > /dev/null 2>&1 | |
if [ $? -eq 0 ]; then | |
kill -9 "$_PID_CHECK" | |
sleep 1 | |
fi | |
done | |
break | |
fi | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment