Skip to content

Instantly share code, notes, and snippets.

@admk
Created November 3, 2013 17:52
Show Gist options
  • Save admk/7292849 to your computer and use it in GitHub Desktop.
Save admk/7292849 to your computer and use it in GitHub Desktop.
Mac OS X 下在快播或其插件不运行的时候停止QvodTerminal在后台运行。Terminate QvodTerminal on Mac OS X when no qvod plug-ins or application is running.
#!/usr/bin/env python
# encoding: UTF-8
"""
Usage: nohup sudo ./qvod_sentinel
"""
import os
import time
import signal
from subprocess import Popen, PIPE
from datetime import datetime
def pid(proc):
ps = Popen(['ps', 'aux'], stdout=PIPE)
processes = ps.stdout.readlines()
ps.terminate()
processes = [p for p in processes if proc in p]
pid_list = []
for p in processes:
p = p.split(' ')
for s in p[1:]:
if s:
pid_list.append(int(s))
break
return pid_list
def now():
return datetime.now().strftime('%Y-%m-%d %H:%M:%S')
print 'Guarding prisoner...'
while True:
try:
pid_list = pid('QvodPlayerPlugin') + pid('\xe5\xbf\xab\xe6M^R\xad')
if not pid_list:
print '\r', now(), 'Qvod processes are not running',
for p in pid('QvodTerminal'):
print '\r', now(), 'Killing QvodTerminal',
os.kill(p, signal.SIGKILL)
else:
print '\r', now(), 'Qvod processes are running',
except Exception as e:
print e
time.sleep(60.0)
@ldong
Copy link

ldong commented Jul 9, 2014

Nice!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment