Commit 934dfa9b authored by Alan Mitchell's avatar Alan Mitchell

Changed script to terminate old main_cron to use standard library packages.

Had trouble installing the 3rd party 'psutil' package on Webfaction servers.
parent ce0c5cf8
......@@ -5,9 +5,8 @@ time threshhold. Should be run with the django-extensions runscript facility:
This script is also called from the main_cron.py script.
'''
import time
import getpass
import psutil
import subprocess
# Hold a main_cron process must be in seconds before being terminated.
OLD_SECS = 3600
......@@ -18,21 +17,12 @@ def run():
# determine the User name that is running BMON.
this_user = getpass.getuser()
# A list of all the old processes to terminate.
to_terminate = []
for p in psutil.process_iter(['username']):
if p.info['username'] == this_user:
# Check all the pieces of the command (past the interpreter command in the first
# index) to see if the "main_cron" process is part of the name.
for fld in p.cmdline()[1:]:
if 'main_cron' in fld:
if time.time() - p.create_time() > OLD_SECS:
to_terminate.append(p)
break
for p in to_terminate:
print(f"Terminating: {' '.join(p.cmdline())}")
p.terminate()
_, alive = psutil.wait_procs(to_terminate, timeout=3)
for p in alive:
p.kill()
completed = subprocess.run('ps -u $USER -o pid,etimes,command', shell=True, text=True, check=False, stdout=subprocess.PIPE)
for proc in completed.stdout.splitlines():
if 'runscript main_cron' in proc:
flds = proc.split()
pid = flds[0].strip()
etime = float(flds[1])
if etime > OLD_SECS:
print(f'Terminating: {pid}')
subprocess.run(f'kill {pid}', shell=True, check=False)
......@@ -22,4 +22,3 @@ xlrd==1.2.0
metar==1.7.0
lxml==4.3.2
django-lockdown==3.0.0
psutil==5.7.0
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment