Windows Batch script leaves console window open - windows

I'm trying to set up a friends Windows 7 computer to run Nginx & PHP5. I found a script online for starting and stopping Nginx & PHP, after adding the directory change line I was able to make it work. However, there seems to be an issue causing it to leave the second console window that starts PHP open. Is there a way to make that console window close?
Batch script:
#ECHO OFF
CD C:\nginx
tasklist /FI "IMAGENAME eq nginx.exe" | find /I "nginx.exe" > NUL && (
GOTO STOP
) || (
GOTO START
)
:START
ECHO Starting nginx
start nginx
ECHO Starting PHP
start php\php-cgi.exe -b 127.0.0.1:9000 -c c:\nginx\php\php.ini
GOTO DONE
:STOP
ECHO Stopping nginx
start nginx -s quit
ECHO Stopping PHP
taskkill /f /IM php-cgi.exe
:DONE
TIMEOUT 3

You could use the /b parameter on START to start the application without opening another cmd window
START /b php\php-cgi.exe -b 127.0.0.1:9000 -c c:\nginx\php\php.ini
Update:
It appears this is the behavior of php-cgi.exe. See this article for the full story and workaround. http://wiki.nginx.org/PHPFastCGIOnWindows
After being launched, php-cgi.exe will keep listening for connections
in a command prompt window. To hide that window, use the tiny utility
RunHiddenConsole
Basically, you just need to d/l and unzip RunHiddenConsole to your nginx directory, then change this line to:
RunHiddenConsole.exe php\php-cgi.exe -b 127.0.0.1:9000 -c c:\nginx\php\php.ini

You're looking for
start php\php-cgi.exe -b 127.0.0.1:9000 -c c:\nginx\php\php.ini
/exit b

To run a .BAT Invisible you can use a simple vbs script.
Put this in a .VBS file :
CreateObject("Wscript.Shell").Run """" & WScript.Arguments(0) & """", 0, False
And then run your BAT like this :
wscript.exe "C:\invisible.vbs" "C:\YourBat.bat"

Related

batch - run command series on new window

First thing fist, this is the first attempt with batch.
I am writing a small utility script that executes very simple tasks, and I have already pretty much everything in place.
What I want to do is execute some of the actions in a different command window.
I managed to get a new window to open, but the problem I've got is that it exectues the first command only, and the second one still runs on the first window.
Here's some code:
ECHO OFF
CLS
:MENU
ECHO.
ECHO 1 - tail - Error
ECHO 2 - tail - Access
ECHO -----------------------------
ECHO X - Close
SET /P M=Select the action number then press ENTER:
IF %M%==1 GOTO error
IF %M%==2 GOTO access
IF %M%==X GOTO EOF
:error
ECHO.
start cmd /K ssh web
sleep 5
tail -f /var/log/apache2/error.log
GOTO MENU
:access
ECHO.
start cmd /K ssh web
sleep 5
tail -f /var/log/apache2/access.log
GOTO MENU
with this code, the new window opens and the ssh command runs. It waits 5 seconds and then it tries to tail the first window (the one with the menu) and not the newly opened ssh window.
How do I change this? any help?
Thanks in advance
When you run the command start cmd /K ssh web the control is passed straight back to your menu script. (your sleep command, which BTW isn't a default command within cmd, is also run on the menu script)
You may need to either try concatenating your commands using & or instead call another script.
concatenation example:
#ECHO OFF
CLS
:MENU
ECHO.
ECHO 1 - tail - Error
ECHO 2 - tail - Access
ECHO -----------------------------
ECHO X - Close
CHOICE /C 12X /N /M "Select the action number."
IF ERRORLEVEL 3 GOTO :EOF
IF ERRORLEVEL 2 CALL :ACTION access
IF ERRORLEVEL 1 CALL :ACTION error
GOTO :EOF
:ACTION
ECHO.
START "" /WAIT CMD /C "ssh web & sleep 5 & tail -f /var/log/apache2/%1.log"
GOTO :MENU
you have to give all the commands to the new window:
start cmd /K "ping google.de & timeout 5"
& is to run one command after the other. See here for some more useful options: &&, ||)
(I used commands, that work on my computer to show the principle)
Note: cmd /k will keep the window open. Use cmd /c if you want it to close when finished.

Windows batch file, wait for command to finish?

I can't seem to figure this out.
I am creating a live site and deploying it online
batch file:
START cmd /K "runas /user:administrator & cd C:\users\MyName\dropbox\!!GIT_HUB_REPOS_ALL\tangycode.github.io & hexo generate & hexo serve --draft"
START /wait "" http://localhost:4000/
The thing is running the command hexo generate & hexo serve --draft takes about 5-10 seconds, time varies. Ideally I want to wait for this to occur before going to live site at http://localhost:4000
Some reason this windows batch command just automatically opens up localhost:4000 right away though
I would use /B to stay in the same process and /wait tot wait until the first command is finished. You don't need the /wait in the second line unless there are more commands to follow. If this doens't work, experiment with leaving the cmd /K away. Since runas is an executable and the batch waits until it is finished it's possible you can let away the start command all together.
If all this doesn't work insert 5 ping commands, that is the classic way to wait for ± one second.
START /B /wait "runas /user:administrator & cd C:\users\MyName\dropbox\!!GIT_HUB_REPOS_ALL\tangycode.github.io & hexo generate & hexo serve --draft"
START /B "" http://localhost:4000/
I'll take a stab at this, (completely untested).
#Echo Off
(Set SrcDir=%UserProfile%\dropbox\!!GIT_HUB_REPOS_ALL\tangycode.github.io)
If Not Exist "%SrcDir%\" Exit/B
Start "" /D"%SrcDir%" /Wait /B RunAs /User:administrator^
"Cmd /C Start /Wait hexo generate & Start hexo serve --draft"
Start http://localhost:4000/

start ping www.google.si closes my cmd window

I need to open cmd window multiple times from a cmd window. Site address or ip would change, so i can ping router, pc, google,.. The problem is when i issue this command it closes my original window i can open more than i window like that. I have set the cmd windows to go to a menu to choose other options after that. I do have some parameters set, but its not working with or without them.
start ping 192.168.0.1
If nothing else, could i open .bat file with this command and change address and parameters somehow?
try to use a bat file with this code
start cmd /k PING TARGET_IP1 -n 1 -w 5000 >NUL
start cmd /k PING TARGET_IP2 -n 1 -w 5000 >NUL
start cmd /k PING TARGET_IP3 -n 1 -w 5000 >NUL
/k stay the window open
>nul is like #echo off
-w 5000 is just the timeout in miliseconds
I solved it. Not really sure when i solved the original cmd window closing but here is the code:
start cmd /c "color 0a & title ping %ip% %l% %t% %n% & cls & ping %ip% %l% %t% %n% & echo. & pause >nul | set /p = Press any key to EXIT.."
I used /c to issue more commands in a row. Now i can open cmd windows without getting main one closed, use parameters i want each time and keep it opened after it finnishes its job.

How to close CMD when another program is exited?

I want to close a cmd window when the program csgo.exe is exited. There is one cmd window open, which is ( node server.js ) and I would like the program to be stopped when csgo.exe is quit.
Here is the full .bat.
#ECHO OFF
start steam://rungameid/730
start cmd /K "cd C:\CSGO-HUD-master & node server.js"
start cmd /K "cd C:\Program Files (x86)\Mozilla Firefox & start firefox.exe http://localhost:2626/ & exit"
Thanks!
The is doable if you know the task name of the program you want to watch. Here is the code:
#echo off
:LOOP
tasklist|findstr calc.exe > nul
if %errorlevel%==1 goto ENDLOOP
ping 127.0.0.1 -n 2 > nul
goto LOOP
:ENDLOOP
exit
In this example we want our bat to terminate after the calculator (calc.exe) is closed.
To achieve this, we first execute tasklist which gives us a list of all running programs. Then we check the output for the occurrence of calc.exe. If it is still running, findstr calc.exe will set %errorlevel% to 0 so we can just jump back and check again. If calc.exe is not running, %errorlevel% will be set to 1 so we know that we can jump out of the loop and terminate.
I've added the line ping 127.0.0.1 -n 2 > nul to avoid busy waiting. This line will make your code "wait" for one second between two iterations. Consider that to adjust the waiting time you'll have to replace 2 by the desired amount of seconds to wait +1. So if you want to wait for five seconds the code would be ping 127.0.0.1 -n 6 > nul.

Windows batch file not working properly

My Batch file is not executing the START serial_new.exe, it only terminates serial_new.exe.
it also opens and closes immediately.
ECHO OFF
c:
cd C:\wamp\www\mobilesms\
START serial_new.exe
echo testing echo..
ping 1.1.1.1 -n 1 -w 5000 > nul
taskkill /F /IM serial_new.exe
Try
start "serial_new" serial_new.exe
The start command needs a title for the first argument.

Resources