Solved Batch file help - Find and sys variables

March 28, 2013 at 08:12:13
Specs: Windows 7
Need some help with batch files please.

I have a recurring task where three related services need to be restarted. Basically I'm getting the pids of three services, killing them, then restarting the service. I set the pids manually now with setpids. I need help in setting the pids *automatically* instead of manually.

I'm close but using three different bat files. I'd like to combine the three into one batch file.

getpids.bat
@echo off
echo service1
tasklist /svc | find "service1"
echo ---
echo service2
tasklist /svc | find "service2"
echo ---
echo service3
tasklist /svc | find "service3"
echo ----
echo Done


Setpids.bat
echo service1=%1
set temppid1=%1
echo service2=%2
set temppid1=%2
echo service3=%3
set temppid3=%3


killpids.bat
taskkill /f /pid %tempid1%
taskkill /f /pid %tempid2%
taskkill /f /pid %tempid3%
set %tempid1%=
set %tempid2%=
set %tempid3%=
net start "service1"
ping -w 200 128.0.0.1
net start "service2"
ping -w 200 128.0.0.1
net start "service3"


See More: Batch file help - Find and sys variables

Report •

#1
March 28, 2013 at 09:35:25
✔ Best Answer
@echo off  & setlocal

for /F "tokens=2" %%a in ('tasklist /svc ^| find "service1"') do set temppid1=%%a
for /F "tokens=2" %%a in ('tasklist /svc ^| find "service2"') do set temppid2=%%a
for /F "tokens=2" %%a in ('tasklist /svc ^| find "service3"') do set temppid3=%%a

echo ---
echo service1 pid: %temppid1%
echo ---
echo service2 pid: %temppid2%
echo ---
echo service3 pid: %temppid3%
echo ----

taskkill /f /pid %tempid1%
taskkill /f /pid %tempid2%
taskkill /f /pid %tempid3%

net start "service1"
ping -w 200 128.0.0.1
net start "service2"
ping -w 200 128.0.0.1
net start "service3"


Report •

#2
March 28, 2013 at 14:25:27
Works great. I'll do a little tweaking to make it look good. Thanks.

Report •
Related Solutions


Ask Question