监视并重新启动python的批处理 [英] Batch that monitors and restarts a python

查看:101
本文介绍了监视并重新启动python的批处理的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下命令进行批量处理,以在任何一个python文件关闭或崩溃时重新启动我的python文件:

I'm trying to make a batch that restarts my python files whenever any of them close or crash, with the following command:

@echo off
:a
cd C:\Users\PC\Desktop\testfolder
test1.py
test2.py
test2.py
goto a

但是它不起作用,我该怎么办?

but it is not working, what should I do?

推荐答案

在我的情况下,无限循环"和python文件的组合将使您的CPU大量过载.修改一段代码(仅在单个文件扩展名(*.bat, *.txt)中工作).参见下文,了解更多一般信息.

A combination of an "infinite loop" which is needed in your case and python files will overload your CPU a lot I think. Have a revised piece of code (working only in single file extensions (*.bat, *.txt)). See below for something more general.

@echo off
setlocal EnableExtensions

:start_python_files
start "1st" "test1.py"
start "2nd" "test2.py"
start "3rd" "test3.py"

:check_python_files
call:infinite 1st test1.py
call:infinite 2nd test2.py
call:infinite 3rd test3.py
goto:check_python_files

:infinite
tasklist /FI "WINDOWTITLE eq %1 - %2" | findstr /c:PID > nul
rem findstr /c:PID command added above to confirm that tasklist has found the process (errorlevel = 0). If not (errorlevel = 1).
if %errorlevel% EQU 1 (start "%1" "%2")

这种方法可能会持续一段时间,因此,如果文件被关闭(约2-3秒,具体取决于您的CPU过载).

Well, this way may last some time, so if a file is closed (~2-3 secs depending on your CPU overload).

如果它不适合您,请通知我.我尚未安装python,也不知道打开它们时的命名方式:).

Please notify me if it is not working for you. I haven't python installed and I don't know how they are named when they are opened :).

因此,现在(如您所愿)要求您提供完整的答案,让我解释一下我的代码:

So, now as you have (kindly???) requested complete answers let me explain my code:

  • 我启用扩展(setlocal EnableExtensions)来更改call命令,如下所示:
  • I enable extensions (setlocal EnableExtensions) to change call command as follows:

CALL命令现在接受标签作为CALL的目标.语法 是:

CALL command now accepts labels as the target of the CALL. The syntax is:

CALL:标签参数

来自call /?命令.您应该在一个新的cmd中键入它以获得更多信息

From call /? command. You should type it in a fresh cmd for more information

  • 我用start命令指定了窗口标题,因此我的代码可以使用.在新的cmd窗口中键入start /?.

  • I specify the window title with the start command, so my code will work. Type start /? in a fresh cmd window.

I call infinite子例程,向其发送参数(窗口标题和文件名).可以使用%1(第一个参数)和%2(第二个参数)进行访问.

I call the infinite subroutine sending to it arguments (window title and filename). These can be accessed with %1 (first argument) and %2 (second argument).

infinite子例程中,我搜索窗口标题(WINDOWTITLE)等于(eq)以格式化window title - filename.即使它不存在,tasklist也会返回errorlevel0并显示以下消息:

In the infinite subroutine, I search for window title (WINDOWTITLE) EQUAL (eq) to format window title - filename. Even if it doesn't exist tasklist will return errorlevel value 0 with the message:

信息:没有运行符合指定条件的任务.

INFO: No tasks are running which match the specified criteria.

因为这里PID字符串不存在(如果找到,它将存在),我们将findstr进行搜索.如果找到,errorlevel将为0.否则,将是1.

As here PID string doesn't exist (if it is found it will exist), we put findstr to search for it. If found, errorlevel will be 0. Else, it would be 1.

  • 如果errorlevel1,则表示未找到该进程,这意味着该文件已关闭.因此,我们使用发送的参数(start "window title (%1)" "filename (%2)")重新打开它.

  • If the errorlevel is 1, that means that process not found, which means that the file is closed. So, we reopen it with the arguments sent (start "window title (%1)" "filename (%2)").

call子例程结束后,我们将无限期地返回check_python_files子例程,直到用户终止或关闭计算机.

As we have called the infinite subroutine, after its end, we will return to check_python_files subroutine doing all of these above infinitely, until user termination or computer shutdown.

正如稍后在聊天中所讨论的,当我们标准运行python文件(使用start "window title")时,窗口标题将是python.exe文件的完整路径.我找到了一种解决方法:start cmd /c命令.修改后的代码:

As later discussed in chat, when we run python files standardly (with start "window title") window title will be the full path to python.exe file. I found a way to fix it: start the cmd /c command. A revised piece of code:

@echo off
setlocal EnableExtensions

:start_python_files
start "1st" "cmd /c test1.py"
start "2nd" "cmd /c test2.py"
start "3rd" "cmd /c test3.py"

:check_python_files
call:infinite 1st test1.py
call:infinite 2nd test2.py
call:infinite 3rd test3.py
goto:check_python_files

:infinite
tasklist /FI "WINDOWTITLE eq %1" | findstr /c:PID > nul
rem findstr /c:PID command added above to confirm that tasklist has found the process (errorlevel = 0). If not (errorlevel = 1).
if %errorlevel% EQU 1 (start "%1" "cmd /c %2")

我刚刚从窗口标题中仅添加了cmd /c多余的内容(并删除了%2),

I have just added only a cmd /c extra (and remove %2) from window title as it was not needed.

cmd /c告诉系统运行一个新的cmd,该cmd将执行字符串指定的命令,然后终止.

cmd /c tells system to run a new cmd which will carry out the command specified by string and then it will terminate.

简介:

  1. 应该运行命令以获取有关其工作方式的更多信息:

  • call /?
  • start /?
  • goto /?
  • tasklist /?
  • findstr /?
  • cmd /?
  • call /?
  • start /?
  • goto /?
  • tasklist /?
  • findstr /?
  • cmd /?

我建议在新的cmd窗口中运行以上命令.

I suggest to run the above in a fresh new cmd window.

  1. 一些有趣的参考:

  • https://ss64.com/nt/start.html
  • How do I pass command line parameters to a batch file?
  • Set Windows command-line terminal title in Python

很抱歉让您陷入困境.无论如何,谢谢您为我提供了这么好的信息,以帮助我了解我哪里做错了.

I am really sorry for putting you into this mess. In anyway, thank you for providing such good information for me to understand where I was wrong.

这篇关于监视并重新启动python的批处理的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆