title: Constantly monitor a program/process using Python
body: <p>I am trying to constantly monitor a process which is basically a Python program. If the program stops, then I have to start the program again. I am using another Python program to do so.</p>
  
  <p>For example, say I have to constantly run a process called <code>run_constantly.py</code>. I initially run this program manually, which writes its process ID to the file "PID" (in the location out/PROCESSID/PID). </p>
  
  <p>Now I run another program which has the <em>following code</em> to monitor the program <code>run_constantly.py</code> from a Linux environment:</p>
  
  <pre><code>def Monitor_Periodic_Process():
  
      TIMER_RUNIN = 1800
      foo = imp.load_source("Run_Module","run_constantly.py")
      PROGRAM_TO_MONITOR = ['run_constantly.py','out/PROCESSID/PID']
      while(1):
          # call the function checkPID to see if the program is running or not
          res = checkPID(PROGRAM_TO_MONITOR)
          # if res is 0 then program is not running so schedule it
          if (res == 0):
              date_time = datetime.now()
              scheduler.add_cron_job(foo.Run_Module, year=date_time.year, day=date_time.day, month=date_time.month, hour=date_time.hour, minute=date_time.minute+2)
              scheduler.start()
              scheduler.get_jobs()
              time.sleep(TIMER_NOT_RUNIN)
              continue
          else:
              #the process is running sleep and then monitor again
              time.sleep(TIMER_RUNIN)
              continue
  </code></pre>
  
  <p>I have not included the <code>checkPID()</code> function here. <code>checkPID()</code> basically checks if the process ID still exists (i.e. if the program is still running) and if it does not exist, it returns <code>0</code>. In the above program, I check if <code>res == 0</code>, and if so, then I use Python's scheduler to schedule the program. However, the major problem that I am currently facing is that the process ID of this program and the <code>run_constantly.py</code> program <em>turns to be same</em> once I schedule the <code>run_constantly.py</code> using the <code>scheduler.add_cron_job()</code> function. So if the program <code>run_constantly.py</code> crashes, the following program still thinks that the <code>run_constantly.py</code> is running (since both process IDs are same), and therefore continues to go into the else loop to sleep and monitor again.</p>
  
  <p>Can someone tell me how to solve this issue?  Is there a simple way to constantly monitor a program and reschedule it when it has crashed?</p>