Skip to content

Instantly share code, notes, and snippets.

@pmarreck
Last active January 29, 2024 13:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmarreck/28cef3a701265f213957ec9f480e4730 to your computer and use it in GitHub Desktop.
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
#!/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."
}
@dubigrasu
Copy link

What if the user itself is called "steam" (like on older SteamOS)?

@pmarreck
Copy link
Author

pmarreck commented Jan 29, 2024

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