JSI Tip 9635. How can I start an application, and automatically restart it if the user ends it?

I have scripted Always.bat to start a process, and automatically restart it if the user terminates it.

The syntax for using Always.bat is:

Always WindowType Application \[OptionalParameters\]


WindowType         is 1 if you wish to start the application in a normal window,
                      2 to start the application minimized,
                      3 to start the application maximized.

Application        is the full path to the executable that you wish to launch.

OptionalParameters are optional parameters to pass to Application.
NOTE: The process that is responsible to run and/or restart the Application is hidden from the user.

Always.bat contains:

@echo off
if \{%2\}==\{\} @echo Syntax: Always Window Program \[param1 ... paramN\]&goto :EOF
set p1=%1
set p1=%p1:"=%
set p2=%2
set p2="%p2:"=%"
if "%p1%" LSS "1" goto err1
if "%p1%" GTR "3" goto err1
if not exist %p2% goto err2
set vb=%TEMP%\Always_%RANDOM%_
set vb1="%vb%1.vbs"
set vb2="%vb%2.vbs"
@echo dim arg>%vb1%
@echo Set WshShell = WScript.CreateObject("WScript.Shell")>>%vb1%
@echo Set objArgs = WScript.Arguments>>%vb1%
@echo arg=%vb2%>>%vb1%
@echo arg=arg ^& " " ^& objArgs(0)>>%vb1%
@echo For I = 1 to objArgs.Count - 1 >>%vb1%
@echo.    arg = arg ^& " " ^& objArgs(I)>>%vb1%
@echo Next>>%vb1%
@echo Return = WshShell.Run(arg, 0, False)>>%vb1%
@echo set WshShell = Nothing>>%vb1%
@echo dim win, arg>%vb2%
@echo Set WshShell = CreateObject("WScript.Shell")>>%vb2%
@echo Set objArgs = WScript.Arguments>>%vb2%
@echo win=objArgs(0)>>%vb2%
@echo arg=objArgs(1)>>%vb2%
@echo Set objArgs = WScript.Arguments>>%vb2%
@echo For I = 2 to objArgs.Count - 1 >>%vb2%
@echo.   arg = arg ^& " " ^& objArgs(I)>>%vb2%
@echo Next>>%vb2%
@echo Do While True>>%vb2%
@echo  WshShell.Run arg, win, True>>%vb2%
@echo Loop>>%vb2%
start /min %SystemRoot%\System32\cscript.exe //nologo %vb1% %*
goto :EOF
@echo Syntax: Always Window Program \[param1 ... paramN\] - Windows not 1, 2, or 3.
goto :EOF
@echo Syntax: Always Window Program \[param1 ... paramN\] - %p2% not found.

Hide comments


  • Allowed HTML tags: <em> <strong> <blockquote> <br> <p>

Plain text

  • No HTML tags allowed.
  • Web page addresses and e-mail addresses turn into links automatically.
  • Lines and paragraphs break automatically.