0

hc.bat

set num=0
setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=1-3 delims=;" %%a in (server.conf) do (
    findstr /b "^#" %%a >nul 2>&1
    if errorlevel 1 start /B check.bat %%a %%b %num% > check!num!.log
    set /A num=num+1


    )

)
endlocal
exit /b 2

check.bat

set svr=%1
set log=%2
set num=%3
echo %svr% %log% !num!
setlocal ENABLEDELAYEDEXPANSION
copy /y NUL output!num!.tmp >NUL
powershell -command "& {Get-Content %log% | Select-Object -last 1}">> output!num!.tmp
type output!num!.tmp | findstr /m "STUCK" >nul 2>&1 
if %errorlevel%==0 (goto action)
type output!num!.tmp | findstr /m "shut" >nul 2>&1 
if %errorlevel%==0 (goto action)

:action
type output!num!.tmp
del output!num!.tmp
exit /b 2

Two problems with the script 1) the process keeps holding to the log file check0.log 2) In the hc.bat, I'm trying to use the findstr to ignore lines that start with #, but it doesn't seem to be working

Master_Roshy
  • 1,245
  • 3
  • 12
  • 19
  • ad 1.) you need to expand `num` like `!num!` in **`hc.bat`**; ad 2.) you need to expand `errorlevel` using `!!` as well in **`check.bat`**; 3.) are you sure you want to run `check.bat` using the `start` command? consider changing to `call`... – aschipfl Aug 28 '15 at 13:55
  • you mean to expand num at line set /A num=num+1 ?? yes I want to use start because I'll be reading the from the server.conf file and each line output I want to run as a simultaneous process, thats why I put separate logging for each thread to avoid locking between threads P.S. I'm new to batch scripting – Master_Roshy Aug 28 '15 at 14:03
  • 1
    yes, `set /A num=!num!+1`, and in the line above `check.bat %%a %%b !num!`; I'm not sure whether the `start /B check.bat` will work like that, perhaps you could try `start /B cmd /C "check.bat %%a %%b %num% > check!num!.log"` instead; – aschipfl Aug 28 '15 at 14:54
  • thanks that worked. start /B cmd /C...it helped Also I want to know if the line : start /B cmd /C "check.bat %%a %%b %num% > check!num!.log" when I execute check.bat with the parameters supplied manually, I get the output of line type output!num!.tmp, but when I execute hc.bat, I don't see the output. – Master_Roshy Aug 28 '15 at 15:18
  • You mean the content of file `output!num!.tmp` being displayed on screen? you will _not_ see in on screen when `check.bat` is run by `hc.bat` because of the redirection `> check!num!.log` which contains the "display text" then; (actually you should use `>>` to _append_ data rather than _overwrite_); – aschipfl Aug 28 '15 at 16:02
  • I summarised the things from all the comments in an answer; please let's clean up our chat-like comments; if you still have further questions than, just add a comment to the answer... thanks! – aschipfl Aug 28 '15 at 16:10

1 Answers1

1

hc.bat:

Expand num like !num! (delayed expansion).
I modified the set /A statement to avoid (immediate) expansion right to =.
The redirection > means to overwrite data. Hence I changed it to >> in order to append data.
Insert cmd /C into start /B argument.
At findstr, the /b switch and ^ being the first char. in the search string are redudant. I gave the /r switch to force regular expression mode.
The 3rd for /F token was not used, so I simply removed it.

set num=0
setlocal ENABLEDELAYEDEXPANSION
for /f "tokens=1,2 delims=;" %%a in (server.conf) do (
    findstr /r "^#" %%a >nul 2>&1
    if errorlevel 1 start /B cmd /C "check.bat %%a %%b !num! >> check!num!.log"
    set /A num=+1
    )
)
endlocal
exit /b 2

check.bat:

Actually you do not need delayed expansion here (although it does no harm).

set svr=%1
set log=%2
set num=%3
echo %svr% %log% %num%
copy /y NUL output%num%.tmp >NUL
powershell -command "& {Get-Content %log% | Select-Object -last 1}">> output%num%.tmp
type output%num%.tmp | findstr /m "STUCK" >nul 2>&1 
if %errorlevel%==0 (goto action)
type %output%num%.tmp | findstr /m "shut" >nul 2>&1 
if %errorlevel%==0 (goto action)

:action
type output%num%.tmp
del output%num%.tmp
exit /b 2
aschipfl
  • 33,626
  • 12
  • 54
  • 99
  • so how do I get the output of check.bat to the screen? Also in hc.bat, I'm trying to use findstr to leave out lines starting with #, but it seems to be not working – Master_Roshy Aug 28 '15 at 17:32
  • `findstr /R "^#"` should work (you have given `/B`); I don't think there is a way to output the data to both screen _and_ to a file unfortunately... – aschipfl Aug 28 '15 at 18:51
  • There is a very interesting post concerning redirection and why it is not possible to redirect data to multiple targets without special (external) tools (like `tee`): [How do I echo and send console output to a file in a bat script?](http://stackoverflow.com/a/4588136/5047996) – aschipfl Aug 29 '15 at 12:29