0

I have a really complicated Python script going on, sometimes it just gets an error, and the only way to debug this, is restarting it, because everything else would make no sense and the error would come back in no time (I already tried a lot of things, so please dont concentrate on that)

I want a .bat script (im on Windows unfortunately) that restarts my python script, whenever it ends. Another python script is also fine.

How can I do that? Thanks in advance

user8393645
  • 110
  • 1
  • 1
  • 13

1 Answers1

1
set env=python.exe  
tasklist /FI "IMAGENAME eq python.exe" 2>NUL | find /I /N "python.exe">NUL if "%ERRORLEVEL%"!="0(
   start python script.py
)

Other way from python to execute python

import subprocess
from subprocess import call

def processExists(processname):
    tlcall = 'TASKLIST', '/FI', 'imagename eq %s' % processname
    # shell=True hides the shell window, stdout to PIPE enables
    # communicate() to get the tasklist command result
    tlproc = subprocess.Popen(tlcall, shell=True, stdout=subprocess.PIPE)
    # trimming it to the actual lines with information
    tlout = tlproc.communicate()[0].strip().split('\r\n')
    # if TASKLIST returns single line without processname: it's not running
    if len(tlout) > 1 and processname in tlout[-1]:
        print('process "%s" is running!' % processname)
        return True
    else:
        print(tlout[0])
        print('process "%s" is NOT running!' % processname)
        return False



if not processExists('python.exe')
   call(["python", "your_file.py"])
Hariom Singh
  • 3,512
  • 6
  • 28
  • 52
  • ? This still isnt clear code, also it doesnt execute my python file, where do I have to put the folder in where the .py is in? – user8393645 Aug 01 '17 at 23:32
  • add python to your environment variable .. set env = "To your path python.exe" – Hariom Singh Aug 01 '17 at 23:33
  • Sorry I have no clue of batch – user8393645 Aug 01 '17 at 23:33
  • set env="C:\Users\admin\AppData\Local\Programs\Python\Python36-32 python.exe" tasklist /FI "IMAGENAME eq python.exe" 2>NUL find /I /N "python.exe">NUL if "%ERRORLEVEL%"!="0( start python sh2bot.py ) – user8393645 Aug 01 '17 at 23:34
  • right click ..create new file as text add your commands and save it as .bat – Hariom Singh Aug 01 '17 at 23:34
  • Lets say I want to execute the script named sh2bot.py in the folder C:\Users\admin\AppData\Local\Programs\Python\Python36-32 what should I put into the bat – user8393645 Aug 01 '17 at 23:35
  • Just add the path start python C:\Users\admin\AppData\Local\Programs\Python\Python36-32\sh2bot.py – Hariom Singh Aug 01 '17 at 23:36
  • Like this? The bat executes and closes immediately after – user8393645 Aug 01 '17 at 23:39
  • nvm got it running i think – user8393645 Aug 01 '17 at 23:40
  • yes you can either open cmd and paste those commands ..if you want to see execution – Hariom Singh Aug 01 '17 at 23:41
  • Go widnows start -> cmd ->right click start as administartor->paste your commands – Hariom Singh Aug 01 '17 at 23:42
  • For the python script: Where do I specify the path?And I get a lot of compile errors, so whats the script for starting sh2bot at C:\Users\admin\AppData\Local\Programs\Python\Python36-32\ – user8393645 Aug 02 '17 at 00:11
  • In general, using `shell=True` isn't a good way to hide the console window if a console application such as tasklist.exe creates a new console. It's fine here, but see [this answer](https://stackoverflow.com/a/7006424/205580) for alternatives. That said, simply searching for a "python.exe" process is a bad idea. In general, there could be multiple instances of python.exe running across multiple Windows sessions. Instead, the script can be modified to respawn itself and wait on the child as a watchdog process, preferably using a Job object that kills the child if the parent is killed. – Eryk Sun Aug 02 '17 at 23:10