Python Forum
check process status and start if it stop linux
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
check process status and start if it stop linux
#1
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
Reply
#2
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.
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply
#3
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
Reply
#4
(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
Reply
#5
I think I find the solution.
I warp whole script in other script

while True:
    my_check_process_script
    .....
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  ''.join and start:stop:step notation for lists ringgeest11 2 2,423 Jun-24-2023, 06:09 AM
Last Post: ferdnyc
  Use Python to start/stop a server service via a webform? oakleaf2001 0 1,748 Apr-04-2020, 06:14 AM
Last Post: oakleaf2001
  start and stop omxlayer playback dezmob 2 4,759 Jan-27-2020, 09:43 AM
Last Post: dezmob
  how to stop and start a script for 30 seconds laspaul 9 7,635 Jan-16-2020, 02:13 PM
Last Post: laspaul
  How to sharing object between multiple process from main process using Pipe Subrata 1 3,657 Sep-03-2019, 09:49 PM
Last Post: woooee
  Problem: Once I cancel the process my program will start working! Hadad 0 1,644 Jul-24-2019, 04:09 PM
Last Post: Hadad
  Linux: Automatically kill process with fixed exe path anddontyoucomebacknomore 4 3,039 Apr-22-2019, 07:35 AM
Last Post: anddontyoucomebacknomore
  What's the difference b/w assigning start=None and start=" " Madara 1 2,315 Aug-06-2018, 08:23 AM
Last Post: buran
  Can I Control loop with Keyboad key (start/stop) Lyperion 2 3,333 Jul-28-2018, 10:19 AM
Last Post: Lyperion
  win10 Service(script) start and stop hwa_rang098tkd 0 2,459 Jun-21-2018, 07:42 PM
Last Post: hwa_rang098tkd

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020