Created
December 10, 2015 23:04
-
-
Save ricomoss/a6f5bd339752fa3e84c4 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 | |
import os | |
import subprocess | |
import re | |
from signal import SIGTERM | |
from datetime import datetime, timedelta | |
TIMEOUT = 10 | |
CLIP_DURATION = 60 * 60 | |
def setup_env(): | |
os.environ['LD_LIBRARY_PATH'] = '/home/rico/workspace/uplynk/dependencies/slicer_Linux_64/lib' | |
def revert_env(): | |
del(os.environ['LD_LIBRARY_PATH']) | |
def _handle_kill_command(line): | |
username = os.environ['USER'] | |
ps_vals = line.split(' ') | |
for index, val in enumerate(ps_vals): | |
if val not in (username, ''): | |
pid = val | |
cmd = ['kill', '-9', pid] | |
p = subprocess.Popen(cmd) | |
print('Killed process {}'.format(pid)) | |
p.wait() | |
return | |
def kill_hanging_slicer(): | |
cmd = ['ps', '-aux'] | |
p = subprocess.Popen(cmd, stdout=subprocess.PIPE) | |
for b_line in p.stdout: | |
line = b_line.decode('utf-8') | |
regex = '/slicer/liveslicer' | |
m = re.search(regex, line.strip()) | |
if m: | |
p.wait() | |
_handle_kill_command(line) | |
return | |
print('No processes to kill.') | |
def start_slicer(): | |
start = datetime.now() | |
cmd = ['/home/rico/workspace/uplynk/beam/slicer/liveslicer', | |
'-config', '/home/rico/workspace/uplynk/beam/slicer/config.local'] | |
p = subprocess.Popen(cmd, stdout=subprocess.PIPE) | |
for line in p.stdout: | |
print(line.strip().decode('utf-8')) | |
now = datetime.now() | |
if now - start > timedelta(0, CLIP_DURATION): | |
break | |
try: | |
p.wait(timeout=TIMEOUT) | |
os.kill(p.pid, SIGTERM) | |
except (ProcessLookupError, subprocess.TimeoutExpired): | |
pass | |
if __name__ == '__main__': | |
setup_env() | |
start_slicer() | |
kill_hanging_slicer() | |
revert_env() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment