windows batch file start browser after server restart - bash

I want to create a batch file which will start the tomcat server and after the server is started , I want to open a URL in browser.
In the below solution , the suggestion is to use timeout option .
How to launch application after server startup using batch file?
Is there any other better way to check if tomcat is started and then I can trigger to open the browser.

You can use this:
wmic process list brief | find /i "tomcat"
To see tomcat is running.
You can use powershell too:
test-netconnection -computername <name or ip> -port <port number>
get-process | select-string <process name>
First command will check if the specified port is listening and the second will check if the specified process is running. You can use them in whatever order you see fit and check the output. If the output is not in desired state, you can do sleep and loop again until tomcat is up.

start "" tomcat.exe
:loop
timeout /t 1 >NUL
tasklist /FI "imagename eq tomcat.exe" | findstr /I /C:"tomcat.exe" >NUL
if errorlevel 1 goto loop
chrome.exe http://%url%
tasklist verify, if started program really running. Do not simply trust the program start correctly

Related

How can I stop one instance via cmd of a process when several are running?

I start a program from a scilab script via the command line, start myprog.exe.
After the start my scilab script needs to keep going.
Now I want to stop exactly this instance of the process via the command line too, even if several instances of the same program are running.
Is that possible?
I know how to query via batch files whether a process of this program is running and then stop it, but I don't know how to get the exact allocation.
Is there something like a process id?
I use this to check if the process is running:
tasklist /fi "imagename eq ccx.exe" |find ":" > nul
if errorlevel 1 echo Program is running
if not errorlevel 1 echo Program is not running
Use the command tasklist to view all running tasks with their PID
then
Taskkill /PID 26356 /F
or
Taskkill /IM myprog.exe /F

How to kill a specific task in windows 2012 server

This is for windows 2012 server. I am trying to kill a specific task running under cmd window.
In this specific case I want to kill SWIMAUX. I have a command prompt open and every time I use taskkill command I get the following error. How can I kill that task?
C:\Siemens\hot_cold_backups>taskkill /f /fi "imagename eq SWIMAUX"
INFO: No tasks running with the specified criteria.
C:\Siemens\hot_cold_backups>taskkill /f /fi "services eq SWIMAUX"
INFO: No tasks running with the specified criteria.
You need to either specify the image name for the process you want to kill or the window title.
If the window has a specific title, then you can use the WINDOWTITLE parameter of the taskkill command
TASKKILL /F /FI "WINDOWTITLE eq SWIMAUX"
To find the image name of a process:
Open task manager and right click on the process you want to kill, and select Go to details
Find the highlighted executable and use it in your taskkill command, eg. taskkill /f /t /im process.exe
step 1:netstat -ano | findstr :
You should be known your PORT number
Step 2:taskkill /PID /F
id will be shown in the end of the TCP connection data in the result of above command

Batch file won`t start program when launched from windows service

I have created a batch file to run when a specific service stops.
The batch file should stop the relevant software running, restart some services (stop / start) and start the software again.
It`s a simple code:
#echo off
Taskkill /F /IM program1.exe
Taskkill /F /IM program2.exe
timeout /t 5
net stop service1
net stop service2 && net start service2
net start service3
timeout /t 2
start C:/path/program1.exe
start C:/path/program2.exe
Have set the service up to run the batch file as recovery at first failure.
Batch file is linked via a shortcut to be able to run it as administrator.
This works perfectly when running the batch file directly, but when it`s executed by the service recovery, the start-up of the software fails.
Does anyone have any idea what could be wrong?
In some circumstances timeout /t 5 might cause trouble. Try to replace it with PING -n 6 127.0.0.1 > NUL.

Windows Batch script leaves console window open

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"

Determine if Tomcat is running in Windows using the command prompt

Quite simply, how does one determine whether or not Tomcat is running in Windows, using the command prompt?
I am writing a batch script that must do this. This is the Bash version:
RESULT=`netstat -na | grep $2 | awk '{print $7}' | wc -l`
Where $2 is the port.
I am looking for something similar to that. Using Cygwin is out of the question, of necessity this script must be able to run on machines that only have Tomcat.
Test the status of the Tomcat Service with the SC command. MJB already suggested to test the service status with SC, yet another batch script (without FOR loop) for testing the status:
#ECHO OFF
SC query tomcat5 | FIND "STATE" | FIND "RUNNING" > NUL
IF ERRORLEVEL 1 (
ECHO Stopped
) ELSE (
ECHO Running
)
If you are not sure if the service name is tomcat5 you can list all service names with
SC query state= all | FIND "SERVICE_NAME"
You could use tasklist to check if the tomcat executable is running. For example:
#echo off
tasklist /FI "IMAGENAME eq tomcat.exe" | find /C /I ".exe" > NUL
if %errorlevel%==0 goto :running
echo tomcat is not running
goto :eof
:running
echo tomcat is running
:eof
It is also possible to check a remove server using the options /S, /U and /P. See tasklist /? for details.
Using WMIC
#echo off
wmic process list brief | find /i "tomcat.exe"
set result=%ERRORLEVEL%
if "%result%"=="1" echo "not running"
if "%result%"=="0" echo "running"
note : /i is to make the find operation case-insensitive.
This is the Windows version of the netstat based UNIX/LINUX solution asked in the question:
#echo off
netstat -na | find "LISTENING" | find /C /I ":8080" > NUL
if %errorlevel%==0 goto :running
echo tomcat is not running
goto :eof
:running
echo tomcat is running
:eof
Well, I am not very good with scripts but perhaps you could use this as a starting point:
netstat -a -n | findstr :8005
To get if someone is listening in port 8005. That is Tomcat's default port for remote administration, i.e. startup or shutdown.
Alternatively you could use the port that the http server listens to.
Hope this helps
use netstat -a in command prompt.
You'll find 8080 port listed there.
If you run Tomcat for Windows not like a service and don't want to exploit JMX the best way is
for /F %%I in ('tasklist /FI "WINDOWTITLE eq Tomcat" /NH') do if %%I==java.exe goto alreadyRun
where:
Tomcat - the window title of the Tomcat's terminal window by default
java.exe - the name of the Tomcat's processe. NOT tomcat.exe.
Yet another option, since this is probably running as a service
FOR /F "tokens=4 delims= " %%A IN ('SC QUERY tomcat5 ^| FIND "STATE"') DO SET status=%%A
echo "%status%"
status can be things like STOPPED, RUNNING ...
I check it by calling a vb script from command line
cscript //nologo checkurl.vbs | findstr "200"
IF errorlevel 1 GOTO :not_running
Save the below script as checkurl.vbs and replace the ip with machines ip
' Create an HTTP object
myURL = "http://10.1.1.1:8080/"
Set objHTTP = CreateObject( "WinHttp.WinHttpRequest.5.1" )
' Download the specified URL
objHTTP.Open "GET", myURL, False
On Error Resume Next
objHTTP.Send
intStatus = objHTTP.Status
If intStatus = 200 Then
WScript.Echo intStatus
Else
WScript.Echo "Error Connecting"
End If
I had problems with using sc query command, because even if tomcat crashed, the service would still be shown as running where in actual the port was not accessible
You can try searching for the process and extracting the line
For example:
ps|grep tomcat

Resources