-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmonitor.py
51 lines (40 loc) · 1.7 KB
/
monitor.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
import os, sys
import psutil
import subprocess
import logging
import logging.config
import numpy as np
from time import sleep
from timeit import default_timer as timer
if __name__ == "__main__":
logging.config.fileConfig('logging.conf')
logger = logging.getLogger('scarp_reduce')
interval = 15
max_periods = 4
this_pid = os.getpid()
os.chdir('/home/ubuntu/')
cpu_usage = []
for _ in range(2 * max_periods):
sleep(interval) # Allow matching job some startup time
cpu_usage.append(psutil.cpu_percent())
while True:
sleep(interval)
cpu_usage.append(psutil.cpu_percent())
cpu_usage = cpu_usage[-int(max_periods)::]
average_cpu_usage = np.sum(cpu_usage) / max_periods
if average_cpu_usage < 10.0:
commands = []
#commands.append(['sudo', 'umount', '-f', '/efs'])
#commands.append(['sudo', 'mount', '-t', 'nfs4', '-o', 'nfsvers=4.1,rsize=1048576,wsize=1048576,hard,timeo=600,retrans=2', 'fs-2fab1c86.efs.us-west-2.amazonaws.com:/', '/efs'])
#commands.append(['sudo', 'chown', '-R', 'ubuntu', '/efs'])
for p in psutil.process_iter():
if 'ipython' in p.name() and p.pid != this_pid:
commands.append(['sudo', 'kill', '{}'.format(p.pid)])
if p.pid == this_pid:
kill_me = ['sudo', 'kill', '{}'.format(p.pid)]
commands.append(['sudo', 'sysctl', '-w', 'vm.drop_caches=3'])
commands.append(['screen', '-wipe'])
commands.append(['screen', '-d', '-m', '/home/ubuntu/runme.sh'])
#commands.append(kill_me)
for c in commands:
subprocess.call(c)