Python Forum

Full Version: check process status and start if it stop linux
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi all,

I want to write script to checking my process if it running OK.

my flow is:

-ssh server-->check process--->if true--->End
|if flase>---->start process---->check process again--->End

But I have problem with step "check process again", it duplicate for previous check process.

if __name__=='__main__':
	check_process()
	restart_process()
	while True:
        ......
		check_process()		
		time.sleep(10)	
		if "expression":
			print ('All service start success')
			break
How I prevent this duplicate
if __name__=='__main__':
    while True:
        if not check_process():
            restart_process()
        time.sleep(10)
So I guess check_process returns a Boolean where True means process is running and False means process is not running.
If the process is not running (False), restart_process() is called.
I use schedule if shall to this from Python and not using OS schedule.
Example using psutil to scan processes running.
If process is running nothing happens,if not running will start it.
This check is done every 10-sec.
import schedule
import time, os
import psutil

def check_process():
    PROCNAME = "notepad.exe"
    for proc in psutil.process_iter():
        if proc.name() == 'notepad.exe':
            return True
    else:
        return False

def check_run():
    if check_process():
        print('Notepad is running')
    else:
        print('Notepad in not running')
        print('Start <notepad.exe>')
        os.startfile("notepad.exe")

schedule.every(.10).minutes.do(check_run)
while True:
    schedule.run_pending()
    time.sleep(1)
Here close Notepad a couple of times,and it will be restart again.
Output:
Notepad is running Notepad is running Notepad is running Notepad in not running Start <notepad.exe> Notepad is running Notepad is running Notepad in not running Start <notepad.exe> Notepad is running
(May-16-2018, 11:44 AM)snippsat Wrote: [ -> ]I use schedule if shall to this from Python and not using OS schedule.
Example using psutil to scan processes running.
If process is running nothing happens,if not running will start it.
This check is done every 10-sec.
import schedule
import time, os
import psutil

def check_process():
    PROCNAME = "notepad.exe"
    for proc in psutil.process_iter():
        if proc.name() == 'notepad.exe':
            return True
    else:
        return False

def check_run():
    if check_process():
        print('Notepad is running')
    else:
        print('Notepad in not running')
        print('Start <notepad.exe>')
        os.startfile("notepad.exe")

schedule.every(.10).minutes.do(check_run)
while True:
    schedule.run_pending()
    time.sleep(1)
Here close Notepad a couple of times,and it will be restart again.
Output:
Notepad is running Notepad is running Notepad is running Notepad in not running Start <notepad.exe> Notepad is running Notepad is running Notepad in not running Start <notepad.exe> Notepad is running

Thank you for your answer
But I have another problem that, I cannot calculate exactly time when process success start(sometime it take too long, sometime very fast to start successful). So if I set schedule to check process,It case can happen.

EX: restart_process take 20 minutes, but all script have run about every 10 minutes >> So if process die, It not enough time to start.

How to deal with this case
I think I find the solution.
I warp whole script in other script

while True:
    my_check_process_script
    .....