Monday, 25 April 2022

How to monitor the RAM and kill a docker program?

step: how to run it in crontab?

@reboot sleep 900 && /usr/bin/python3 /home/ninja/Documents/watchdog.py

 

code:-

---

import psutil
import platform
from datetime import datetime
import os
from datetime import timedelta
import time

def get_size(bytes, suffix="B"):
    """
    Scale bytes to its proper format
    e.g:
        1253656 => '1.20MB'
        1253656678 => '1.17GB'
    """
    factor = 1024
    for unit in ["", "K", "M", "G", "T", "P"]:
        if bytes < factor:
            return f"{bytes:.2f}{unit}{suffix}"
        bytes /= factor




while True:

    svmem = psutil.virtual_memory()
    cVal = svmem.percent
    if cVal > 90:
        print("start killing")
        flag = True

        try:
            os.system('docker rm $(docker stop $(docker ps -a -q --filter ancestor=docker pull nvcr.io/nvidia/deepstream:6.0-triton --format="{{.ID}}"))')
        except:
            flag = False
            print("Fail to kill")

        time.sleep(10)

        svmem = psutil.virtual_memory()
        cVal = svmem.percent

        if flag and cVal < 25:
            try:
                print("restarting")
                os.system("/home/ninja/Documents/run1.sh &")
                os.system("/home/ninja/Documents/run2.sh &")


            except:
                print("Fail to relaunch")



    time.sleep(60)

No comments:

Post a Comment