├── CONTRIBUTING.md ├── README.md └── zat.py /CONTRIBUTING.md: -------------------------------------------------------------------------------- 1 | ## Don't. 2 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # Zat'nik'tel (zat) 2 | 3 | Much like its Stargate namesake, **zat** is a process manager with the following behavior: 4 | 5 | * 1st shot 'stuns' (suspends) the process 6 | * 2nd shot kills the process 7 | * 3rd shot disintegrates the underlying command (or at least tries to) 8 | 9 | ``` 10 | $ ps a | grep sloth | grep -v grep 11 | 12416 s003 S+ 0:00.05 sloth 100ms 12 | $ zat 12416 13 | suspending 12416 sloth 14 | $ zat 12416 15 | killing 12416 sloth 16 | $ zat 12416 17 | disintegrating /usr/local/bin/sloth 18 | $ sloth 19 | -bash: sloth: command not found 20 | ``` 21 | 22 | _NOTE: I have only seen two seasons of SG-1 thus far. Please do not post spoilers via Issues, Pull Requests, etc._ 23 | -------------------------------------------------------------------------------- /zat.py: -------------------------------------------------------------------------------- 1 | #!/usr/bin/env python3 2 | 3 | import os 4 | import ast 5 | import sys 6 | import psutil 7 | 8 | state_path = os.path.expanduser("~/.zat") 9 | 10 | def load_state(): 11 | if os.path.exists(state_path): 12 | with open(state_path, 'r') as f: 13 | return ast.literal_eval(f.read()) 14 | return {} 15 | 16 | def save_state(state): 17 | with open(state_path, 'w') as f: 18 | f.write(repr(state)) 19 | 20 | if len(sys.argv) <= 1: 21 | print("Zat'nik'tel (zat)") 22 | print("Suspends, kills, and disintegrates commands.") 23 | print("") 24 | print("Usage:") 25 | print("zat ") 26 | 27 | for arg in sys.argv[1:]: 28 | pid = int(arg) 29 | pids = psutil.pids() 30 | 31 | if pid in pids: 32 | process = psutil.Process(pid) 33 | status = process.status() 34 | 35 | if status != 'stopped': 36 | # first shot, suspend the process 37 | print(f"suspending {pid} {process.name()}") 38 | process.suspend() 39 | else: 40 | # back up the exe path so we can disintegrate it later 41 | state = load_state() 42 | state[pid] = process.exe() 43 | save_state(state) 44 | 45 | # second shot, kill the process 46 | print(f"killing {pid} {process.name()}") 47 | process.kill() 48 | else: 49 | state = load_state() 50 | if pid in state.keys(): 51 | # third shot, completely disintegrate the command 52 | print(f"disintegrating {state[pid]}") 53 | os.remove(state[pid]) 54 | state.pop(pid, None) 55 | save_state(state) 56 | else: 57 | print(f"process {pid} doesn't exist, and hasn't been zat'd before either") 58 | --------------------------------------------------------------------------------