3

This isn't very googleable, and other than writing prog.exe -params several times in a batch file (which would obviously only work as many times as I wrote it) I can't think how to repeatedly do this.

The CLI program I'm running sometimes crashes. I don't want to manually restart it every time it does.

OJFord
  • 621
  • 3
  • 8
  • 20

4 Answers4

5

Perhaps just write the command in the batch once, and then also add the batch file to the batch itself (so it loops)...

Something like:

Go.bat:

@echo off
Prog.exe -params
Go.bat

Hit Ctrl-C when you want to break the batch loop.

Ƭᴇcʜιᴇ007
  • 111,883
  • 19
  • 201
  • 268
3
:main
@start /wait prog.exe -params
goto :main
Vlastimil Ovčáčík
  • 2,748
  • 1
  • 24
  • 32
0

I actually decided to use C in the end, as I had trouble with the batch file opening infinite instances :/

Here's a very simple alternative in C for anyone interested:

int main(){
    while (1){
        system("C:\\path\\to\\prog.exe -params \"param vars\"");
    }
}

By default, system() requires the command or process to finish or return before the next line is executed (If you don't want this behaviour, add & at the end) - which is exactly what we want in this instance.

If prog.exe closes, we look at the next line of C, which since this is an infinite while loop, reopens the program.

OJFord
  • 621
  • 3
  • 8
  • 20
  • If you Google and find this, and not sure what to do with it since you wanted batch - download tcc, open CMD, run `C:\tcc\tcc.exe infprog.c -o C:\infprog.exe` and then run the executable you've created. – OJFord Nov 20 '13 at 21:30
  • You probably had trouble because your prog.exe executes and returns control directly back to the batchfile before finishing. That's why techie007 suggested running it with `start /wait` in his comment. This does the same as your `system()`. – Rik Nov 21 '13 at 00:28
  • I did use start /wait. – OJFord Nov 21 '13 at 07:58
  • 1
    That is strange. The `start /wait C:\path\to\prog.exe -params` should have worked. But maybe your `prog.exe` spawns itself so `cmd` thinks it's ended. You could try `wait /b C:\path\to\prog.exe -params`. The `/b` makes sure the programs stays in the same window as `cmd.exe`. If that doesn't work maybe `start /b /wait C:\path\to\prog.exe -params` works. Otherwise there is no other option than your C-program. – Rik Nov 21 '13 at 09:45
  • I was using `start /wait /b ...` - not too fussed to try again now though to be honest, I have it working now (with C). – OJFord Nov 25 '13 at 01:54
-1

IF program closes on crash you can detect if its running or not.

(You can check if its running and run it if need be - you can loop the check at interval)

https://stackoverflow.com/questions/162291/how-to-check-if-a-process-is-running-via-a-batch-script

IF the program doesn't close on crash: Short of coding a custom program to do it you can use a Macro recorder with image recognition to execute whatever action you want when it recognizes an event you specify.

However this gets complicated if you want to actively work on the machine while this process is running. You might as well just make a quick-launch shortcut for it and click once manually per crash. The other options are just good if you AFK and you want to make sure it re-runs in your absence.

helena4
  • 328
  • 2
  • 17
  • This does not really add anything helpful. You don't indicate which answer is relevant nor do you quote the relevant answer. In addition to not really adding anything new, the author already accepted the answer, so its not clear what the purpose of basically repeating other peoples (over at stackoverflow) answer is. – Ramhound Nov 17 '15 at 12:43
  • There are plentiful of working solutions over in the thread that don't involve custom applications, for anyone to use. So criticize all you want that I didn't chew it up and spit it back in his mouth for him. – helena4 Nov 17 '15 at 13:27
  • This answer was in a review queue because of your reputation. All users below 100 reputation have their first answer reviewed. I was attempting to highlight some problems with this answer, if you choose to do nothing with the advice, that is entirely up to you. – Ramhound Nov 17 '15 at 13:35