Skip to content

Instantly share code, notes, and snippets.

@jamesgecko
Last active February 12, 2016 21:30
Show Gist options
  • Select an option

  • Save jamesgecko/4956548 to your computer and use it in GitHub Desktop.

Select an option

Save jamesgecko/4956548 to your computer and use it in GitHub Desktop.
Is your [whatever] spin-locking a lot? Do you frequently open `top` just to terminate the process using all of the CPU? Behold, the magic of home automation.`pip install envoy`, then put this script in your path and `chmod +x` it.Note that this was a quick hacky thing built for the default top in Ubuntu. The man page says the columns should be c…
#! /usr/bin/env python
import envoy
def main():
r = envoy.run('top -l 2 -n 1 -o cpu -stats pid,command,cpu')
top_app = r.std_out.split('\n')[25].split()
pid = top_app[0]
process_name = ' '.join(top_app[1:-1]).strip() # Process names can can contain spaces
cpu = float(top_app[-1])
if cpu > 90:
envoy.run('kill -9 %s' % pid)
print('Killed ' + process_name)
else:
print("Didn't kill anything (" + process_name + " at " + str(cpu) + ")")
if __name__ == '__main__':
main()
#! /usr/bin/env python
import envoy
def main():
r = envoy.run('top -b -n 1')
top_app = r.std_out.split('\n')[7].split()
pid = top_app[0]
cpu = int(top_app[8])
if cpu > 90:
envoy.run('kill -9 %s' % pid)
if __name__ == '__main__':
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment