CHEERSNetworkClimate & health

Home / Resources / Weather station

CHEERS technical resources · WindChaser

Source code for wind_bearer_software.utils.program_checker

WindChaser technical documentation: Source code for wind_bearer_software.utils.program_checker. Original instructions, component references and source limitations.

Original documentation: Min Tai Yap and Julian Walker · © 2025. Migrated to CHEERS on 3 December 2026. This is working documentation; unresolved source gaps remain visible.

Software source reference
"""
This module is used to prevent cron from running multiple instances of the main runner at the same time, and
it also contains a helper function to terminate a program if the user is not aware of the command to do so.

Authors:
    Min Tai Yap
"""


import subprocess

[docs] def program_checker(program_name): """ Checks if an instance of program_name is already run. The usage of this method is to prevent cron from running multiple instances of the program in parallel. This has the advantage that, in case the program has crashed, it can be automatically restarted by cron at the times specified in the crontab_materials file. As it is meant to be called by the program itself, it won't indicate that there are multiple instances of the same program running when there is only one program running, as this is the program that calls program_checker. :param program_name: Name of the program, e.g., "mr_V6" :returns: False when there are multiple programs running, True when this is not the case, and a tuple of False and None in any error case """ try: # Use pgrep with -f to match the full command line result = subprocess.run(['pgrep', '-fa', program_name], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) # Get the matching PIDs and their command lines from the output processes = result.stdout.strip().split("\n") # Filter processes to exclude those containing 'cron' or 'sh' which are cron-related filtered_processes = [proc for proc in processes if 'cron' not in proc and 'sh' not in proc] if len(filtered_processes) == 0: # redundant print(f"Program '{program_name}' is not running.") return False else: # Extract PIDs from the filtered processes pids = [proc.split()[0] for proc in filtered_processes] # Assuming PID is the first column if len(pids) == 1: print(f"Program '{program_name}' is running with PID {pids[0]}.") return True else: print(f"Program '{program_name}' is running with multiple PIDs: {', '.join(pids)}") return False except Exception as e: print(f"Error checking program: {e}") return False, None
[docs] def terminate_program(pid): """ Terminates the program with the given PID as a shorthand notation for the shell command or to be used in other modules. :param pid: PID of the process to be killed :returns: True on successful termination, False else """ try: # Run the kill command to terminate the process result = subprocess.run(['kill', '-9', str(pid)], stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) if result.returncode == 0: print(f"Successfully terminated process with PID {pid}.") return True else: print(f"Failed to terminate process with PID {pid}. Error: {result.stderr}") return False except Exception as e: print(f"Error terminating program: {e}") return False