Skip to content

Instantly share code, notes, and snippets.

@apocalyptech
Created July 18, 2021 04:17
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 apocalyptech/cfe7a24f8b9c9452d18727b474b50f55 to your computer and use it in GitHub Desktop.
Save apocalyptech/cfe7a24f8b9c9452d18727b474b50f55 to your computer and use it in GitHub Desktop.
#!/usr/bin/env python
# vim: set expandtab tabstop=4 shiftwidth=4:
import os
import time
import psutil
import signal
def find_main_pid():
"""
Find the Borderlands3 process to kill
"""
for proc in psutil.process_iter(attrs=["name", "cmdline"]):
if proc.info['name'] == 'Borderlands3.ex':
return proc.pid
return None
def find_secondary_pid():
"""
Find our secondary steam process which often seems to hang around
on the newer GE Proton that I'm using.
"""
for proc in psutil.process_iter(attrs=["name", "cmdline"]):
if (proc.info['name'] == 'steam'
and len(proc.info['cmdline']) >= 2
and 'Borderlands3.exe' in proc.info['cmdline'][1]
):
return proc.pid
return None
def kill_process(label, pid_func):
"""
Returns True if there was a process to kill, whether or not
it was successfully killed, or False otherwise
"""
pid = pid_func()
if pid is None:
print("Couldn't find {} process...".format(label))
return False
else:
print('Killing {} PID: {}'.format(label, pid))
os.kill(pid, signal.SIGTERM)
print('...')
time.sleep(2)
pid = pid_func()
if pid is None:
print('Killed!')
else:
print('Unable to kill PID {}!'.format(pid))
return True
# Main BL3 Proc
if kill_process('main Borderlands 3', find_main_pid):
print('Waiting another 5 sec for things to settle...')
time.sleep(5)
# ... and the Steam proc
kill_process('secondary Steam', find_secondary_pid)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment