Batch or VBscript : stop/start tomcat locally

Hello experts,

I have in multiple machines tomcat app I would like through a script to automatically stop the tomcats of every server without connecting to every machine that means to launch from my local machine the script and stop-start the tomcats application of different servers.
All the servers have Windows server 2008 OS.

Ex:

IP machine B

Machine A
stop
start tomcat

IP machine B
stop
start tomcat


Thank you in advance for your help.
LVL 1
LD16Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Steve KnightIT ConsultancyCommented:
If they are a member of a domain most easily then don't need to authenticate to each and can just use SC.exe

e.g. stop service, wait 10 secs, start it again.

sc \\servername stop "Service Name"
ping 127.0.0.1 -n 10
sc \\servername start "Service Name"
sc \\severname query "Service Name"

The Service name is the one shown in a list from sc query and may not match what you see in services GUI.

If you have a handful of machines you can just list the commands, or do it as a subroutine for each, or read from a text file, e.g.

@echo off
set error=
set logfile=logfile.txt
del %logfile% 2>NUL

call :restart Server1 "ServiceName"
call :restart Server2 "ServiceName1"
call :restart Server3 "ServiceName1"

if "%error%"=="" (
  echo No errors
) ELSE (
  echo Errors:
  echo %error%
)
START "" "%logfile%"
pause

exit /b

:restart
REM Restart service listed
sc \\%1 stop %2
if errorlevel 1 set error=%error%[%1 - %2 - %errorlevel% STOP],
ping 127.0.0.1 -n 5
sc \\%1 start %2
if errorlevel 1 set error=%error%[%1 - %2 - %errorlevel% START],
sc \\%1 query %2 >> %logfile%
exit /b

Open in new window

0
Steve KnightIT ConsultancyCommented:
To read a text file instead, replace:

call :restart Server1 "ServiceName"
call :restart Server2 "ServiceName1"
call :restart Server3 "ServiceName1"

Open in new window


with a for loop to read the file:

for /f "delims=," %%a in ('type yourtextfile.txt') do call :restart %%a %%~b

Open in new window


and your text file formatted as:

Server1 "ServiceName"
Server2 "ServiceName1"
etc.

Open in new window

0
LD16Author Commented:
Hello Steve Knight,

I tried your sc solution and it works. What is the pourpose of  "ping 127.0.0.1 -n 10"?

Concerning the second solution Is it possible to:
add an a specific path to ouput the log file ? And have a script just for stopping the tomcat and another for the restart?

Thank you in advance .
0
Steve KnightIT ConsultancyCommented:
Sure. Ping just to add delay betwern stop and start. Probably not needed for most, remove it and see.

You can just save batch again leaving out the sc stop or start line.

Can post example but on phone at mo.

Steve
0
Steve KnightIT ConsultancyCommented:
Have split start/stop etc. up if that makes it easier, then keep three different version of file etc. i.e. do any combinations you need of:

call :restart Server1 "ServiceName"
call :stop Server1 "ServiceName"
call :start Server1 "ServiceName"

Steve

@echo off
set error=
set logfile=c:\somepath\to\your\logfile.txt
del %logfile% 2>NUL

call :restart Server1 "ServiceName"
call :restart Server2 "ServiceName1"
call :restart Server3 "ServiceName1"

call :stop Server1 "ServiceName"

call :start Server1 "ServiceName"

if "%error%"=="" (
  echo No errors
) ELSE (
  echo Errors:
  echo %error%
)
START "" "%logfile%"
pause

exit /b

:start
  sc \\%1 stop %2
  if errorlevel 1 set error=%error%[%1 - %2 - %errorlevel% STOP],
exit /b

:stop
  sc \\%1 start %2
  if errorlevel 1 set error=%error%[%1 - %2 - %errorlevel% START],
exit /b

:check
  sc \\%1 query %2 >> %logfile%
exit /b

:wait
REM Wait 5 secs if needed, if not remove
  ping 127.0.0.1 -n %1
exit /b

:restart
REM Restart service listed

call :stop %1 %2
call :wait 5
call :start %1 %2
call :check %1 %2

exit /b

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Windows Batch

From novice to tech pro — start learning today.