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

8
  • just create bat file add set env=python.exe start python script.py Commented Aug 1, 2017 at 23:25
  • But that only starts the python file, it doesnt restart it, when it crashs? Commented Aug 1, 2017 at 23:26
  • tasklist /FI "IMAGENAME eq python.exe" 2>NUL | find /I /N "python.exe">NUL if "%ERRORLEVEL%"=="0" echo Program is running Commented Aug 1, 2017 at 23:27
  • Can you please write that into a gist or ghostbin? Commented Aug 1, 2017 at 23:29
  • Take a look at forever (github.com/foreverjs/forever). Commented Aug 1, 2017 at 23:56

1 Answer 1

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"])
Sign up to request clarification or add additional context in comments.

13 Comments

? 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?
add python to your environment variable .. set env = "To your path python.exe"
Sorry I have no clue of batch
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 )
right click ..create new file as text add your commands and save it as .bat
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.