Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
#!/usr/bin/python
import sys
import time
def is_sleeping(pid):
with open("/proc/%d/stat" % pid) as stat:
state = stat.read().strip().split()
return state[2] == 'S'
def io_stats(pid):
with open("/proc/%d/io" % pid) as io:
stats = (stat.split(": ") for stat in io.readlines())
return {stat[0]: int(stat[1]) for stat in stats}
if __name__ == '__main__':
pid = int(sys.argv[1])
while True:
stats = io_stats(pid)
last_diff = stats['read_bytes'] - stats['rchar']
while is_sleeping(pid):
stats = io_stats(pid)
cur_diff = stats['read_bytes'] - stats['rchar']
if cur_diff == last_diff:
print("Process seems to be blocked reading the terminal!")
else:
print("Process is blocked doing something else")
last_diff = cur_diff
time.sleep(1)
print('Process is not sleeping anymore')
time.sleep(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment