Last active
January 29, 2024 13:14
-
-
Save pmarreck/28cef3a701265f213957ec9f480e4730 to your computer and use it in GitHub Desktop.
Automatically clean up any orphaned Steam or Proton processes on Linux if any didn't exit cleanly
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
#!/usr/bin/env bash | |
function kill_steam_proton_pids() { | |
# This bash one-liner performs the following tasks: | |
# 1. It sets the PS_PERSONALITY environment variable to 'linux' which standardizes the output format of the 'ps' command. | |
# 2. Runs the 'ps' command with the following options: | |
# - 'e' to include processes from all users, | |
# - 'o pid,args' to only show process ID and command arguments, | |
# - '--sort=-pid' to sort by process ID in descending order (so it kills the newest processes first), | |
# - '--no-headers' to not include column headers in the output. | |
# 3. It pipes this output to 'awk', a scripting language used for manipulating data and generating reports, | |
# where it checks if the string 'steam' (case-insensitive) is present in any of the lines. | |
# 4. It then pipes this filtered output to 'tail -n +2' to ignore the first line (the awk filtering process itself). | |
# 5. Pipes the output to another 'awk' command which prints only the first column (the process ID). | |
# 6. Finally, it pipes these process IDs to 'xargs' which executes the 'kill' command for each ID, terminating the processes. | |
function killit() { | |
PS_PERSONALITY=linux ps -eo pid,args --no-headers | awk -v filter="steam" 'tolower($0) ~ filter && $0 !~ " awk "' | tee /dev/stderr | awk '{print $1}' | xargs -I {} kill $1 {} | |
} | |
killit | |
sleep 2 # give it a chance to seppuku | |
# This usually leaves ~2 processes running still, which we now kill with extreme prejudice. You had your chance. | |
killit -9 | |
echo "Steam is now fragged." | |
} |
In re-remembering how this was implemented, I only output the process ID (which is always a number) and command (with arguments), and filter on that; so the users that own the process(es) aren't even shown, and it should not therefore inadvertently kill processes owned by steam
And I can confirm that it does, in fact, clear out any rogue proton processes that can prevent Steam from reopening a game properly, as I was using this function regularly with success. It is, however, the equivalent of using a shotgun to open a padlock; I welcome any refinements!
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
What if the user itself is called "steam" (like on older SteamOS)?