Commit ce0c5cf8 authored by Alan Mitchell's avatar Alan Mitchell

Added script to main_cron to check for old main_cron processes.

parent d6a55fba
......@@ -7,13 +7,14 @@ cron tasks. It should be run using the django-extensions runscript facility:
from datetime import datetime
import time
from . import calc_readings
from . import daily_status
from . import backup_django_db
from . import backup_readingdb
from . import check_alerts
from . import run_periodic_scripts
from . import terminate_old_cron
def suppress_errors(func):
'''Runs the function 'func' and suppresses all errors.
......@@ -67,3 +68,8 @@ def run():
# run the sensor reading database backup every 3 days
if (yr_day % 3) == 0 and hr == 2 and hr_div == 6:
suppress_errors(backup_readingdb.run)
# once an hour, check for lingering main_cron processes and kill
# old ones.
if hr_div == 2:
suppress_errors(terminate_old_cron.run)
'''Terminates any main_cron processes that are older than a certain
time threshhold. Should be run with the django-extensions runscript facility:
manage.py runscript terminate_old_cron
This script is also called from the main_cron.py script.
'''
import time
import getpass
import psutil
# Hold a main_cron process must be in seconds before being terminated.
OLD_SECS = 3600
def run():
'''Looks for main_cron processes and terminates ones that are older than one hour.
'''
# 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()
......@@ -21,4 +21,5 @@ modbus-tk==0.5.11
xlrd==1.2.0
metar==1.7.0
lxml==4.3.2
django-lockdown==3.0.0
\ No newline at end of file
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