Created
July 18, 2021 04:17
-
-
Save apocalyptech/cfe7a24f8b9c9452d18727b474b50f55 to your computer and use it in GitHub Desktop.
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 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