4

I've this .bat file which I'd like to run in the background. The file basically launches processes. The output of the .bat file would be:

X:\bin>start_STAF.bat
start_STAF.bat

X:\bin>X:\ActiveState\Perl\perl-5.14.0\bin\perl.exe x:\bin\start_STAF.pl

The operation completed successfully.
The operation completed successfully.
The operation completed successfully.
*
STAFProc is now Started.  Leave this window up.
*

Pressing the Enter key after a few minutes returns the control back to the prompt. I don't like this because this process needs to be launched through a Perl script and I don't know how one would automate the Enter key press through Perl.

There's a few things that I've tried, unsuccessfully.

I tried using START /B start_STAF.bat but what this does is that it returns back to the prompt without launching the .bat file. However, the next time I execute tasklist, the .bat file launches on stdout. This brings me back to square one.

I tried redirecting the stdout/stderr as start_STAF.bat >nul 2>&1 but this too, does not launch the .bat file.

Is there anything else that I can do? Thanks!

Nikhil Hegde
  • 341
  • 1
  • 3
  • 15
  • https://metacpan.org/pod/Expect#Can-I-use-this-module-with-ActivePerl-on-Windows – simbabque Sep 30 '17 at 09:09
  • @NikhilHegde As you have executed a Stack Overflow search with the search term [\[batch-file\] run background](https://stackoverflow.com/search?q=%5Bbatch-file%5D+run+background), which topic was the most promising for you? – Mofi Sep 30 '17 at 14:52
  • @Mofi Anything related to running a batch-file in the background would be promising. And the nature of the problem is such that the batch file that I'm currently working on, is read/execute only. – Nikhil Hegde Oct 01 '17 at 13:58
  • try `start start_STAF.bat` – John Doe Oct 02 '17 at 11:16
  • That `start` will open a new cmd windows which will execute the batch file and after it started the process it will return to your cmd window from where you executed the command. – John Doe Oct 02 '17 at 11:33
  • @NikhilHegde If `start_STAF.bat` contains only the single line to run Perl interpreter with a Perl script which obviously requires user input to end it, then don't use the batch file at all and just run Perl executable with the script file as parameter with `start "" /B "X:\ActiveState\Perl\perl-5.14.0\bin\perl.exe" "X:\bin\start_STAF.pl"`. The running Perl interpreter must be stopped using `taskkill` command. For a short time a console window is shown as `start` is an internal command of `cmd.exe` which on execution always opens a console window. – Mofi Oct 03 '17 at 05:51
  • @NikhilHegde Other solutions posted on SO for running a console application in background without opening ever a console window suggest the only method working for this case: running a Windows GUI application like `wscript.exe` (Windows Scripting Host) with a script which starts the console application in background and which do not show a GUI application window at all. – Mofi Oct 03 '17 at 05:54
  • @JohnDoe, that's not the case. I've tried `start start_STAF.bat` and `start /B start_STAF.bat` but in does not return to the prompt until I hit Enter after a few minutes – Nikhil Hegde Oct 03 '17 at 13:56
  • @Mofi, your first suggestion of killing the `perl.exe` process may be the road I take. Running GUI is probably not going to be possible in the environment that I'm set up in without having the STAF process running already – Nikhil Hegde Oct 03 '17 at 14:12
  • @Mofi, the moment I kill `perl.exe` process, `STAF.exe` and `STAFProc.exe` both get killed. When I execute `start perl.exe start_STAF.pl`, I can see the STAF processes starting but the moment I kill `perl.exe`, the STAF processes disappear – Nikhil Hegde Oct 03 '17 at 14:34
  • Just remove the `/B` from the command. Test this `system("start test.bat"); print "\ndone";` and in the same folder create `test.bat` with this content: `ping 1.1.1.1 -n 1 -w 10001 >nul && echo "end with this batch"`. Basically the script will launch in a new window the batch file which will stay opened ( while pinging for 10 s) but the control will return to the cmd window right after launching the batch file. – John Doe Oct 04 '17 at 11:42
  • @JohnDoe, `start start_STAF.pl` worked. Thanks for the help! – Nikhil Hegde Oct 11 '17 at 13:26

2 Answers2

5

Just run the command START start_STAF.bat.

John Doe
  • 1,058
  • 8
  • 30
2

For windows 10

  1. write .bat file(syncfiles.bat)
"C:\Program Files\Oracle\VirtualBox\VBoxHeadless.exe" --comment "linux_mint" --startvm "14f426cc-845d-46cb-9f6e-4dbb31a3769a"
  1. write .vbs file (start.vbs):
Set WshShell = CreateObject("WScript.Shell") 
WshShell.Run chr(34) & "syncfiles.bat" & Chr(34), 0
Set WshShell = Nothing
  1. run vbs file double click start.vbs
Community
  • 1
  • 1
Lương Vũ
  • 21
  • 1
  • 2