1.

Solve : Called batch file hangs - no return value?

Answer»

Hello

I have a batch file that calls another. In the called batch file I am stopping a windows service, restarting a device (using devcon) and restarting the service. I use this arrangement on a media pc to restart a tuner card on resume from standby. Most of the time this works fine. From time to time however the devcon statement in the called batch file hangs. As a consequence the calling batch file GETS no response, never exits and the PC sits there with a blank screen. As far as I can ascertain I am not getting a return value from the called batch file. As all of this happens in unattended mode I am looking at timing OPTIONS in the calling batch file such that if a response is not received within a nominated period of time I restart the PC. Are there any techniques that forum members could suggest to deal with this? Have you tried a time delay at the end of the first and second batch to make sure that the process is complete before the batches end. Had a problem once were a batch starting a POS Service for cash registers would hang randomly, but when adding a time delay before exit of the batch it fixed it... There are a number of ways to add a time delay. using Choice, Ping, Sleep, and other methods to use up cpu cycles and burn some time before exiting.Thanks I shall try them.

Pseudocode will be something LIKE

Call b.bat
Wait some time
Evaluate errorlevel
If not==0 then reboot

My concern will be to make sure that I am catching a return code from the originally called batch file (b.bat) and not from whatever process I use to wait.

Will advise how I go.

Cheers
Quote from: cjsmelb on March 18, 2009, 01:20:05 AM

Wait some time

quick question- what are you waiting for?

The line after the CALL command will only be executed after everything in the CALL'd batch is finished executing.if b.bat never return/hang, how would you ever get an %errorlevel% on a.bat??? even the next line after call b.bat is not going to be processed.

my temporary suggestion is: (if i interprete your problem correctly)
1. create a timer.bat to check if devcon is still running. (run timer.bat as another process/thread)

as your description is pretty vague, not to mention my english and interpretion skill is very bad as well, and i don't do mind-reading either.
why not you post your code so we can help you better.Thanks for the responses. I've attached my code below. Hope it makes some sense.

************

Below is the calling batch file. Fairly simple and PIPES the output to a log file. It is in this batch file that I would like to TRAP the response code from the called batch file. The parameter is assigned by Windows' Task Scheduler which triggers this process in the first place - typically it is a simple text statement such as 'ScheduledTask' helpful as an audit trail.

Primary: MyStart.bat

call c:\MyFiles\mystartexec.bat %1 >> c:\MyFiles\mystartlog.txt
exit

***********************************************

Below is the called batch file. The issue I have is that from time to time the devcon statement (and once or twice the net start statement) fails to execute and the process essentially hangs. Why devcon hangs is a question I have been unable to answer but will continue to investigate (the device itself is a Hauppauge TV tuner card). In the short term though I'd like to identify that the problem has occurred and handle it externally and the only reliable way given the situation is to reboot. The internal logic (in mystartexec.bat) of trapping the devcon return code is not working (presumably because it has hung) so my thougth was to identify the problem in mystart.bat by allowing some reasonable period of time for a response from mystartexec.bat and then if nothing had been received assume a problem and reboot. The various echo statements are there simply to log progess so that I can debug.

Called: MyStartExec.bat

@echo off
echo *****************************************
time /t
date /t
Echo Resuming
Echo Source - %1
NET START | FIND "Windows Media Center Receiver Service" > nul
echo Net find error level is %ERRORLEVEL%
if %ERRORLEVEL% == 2 goto Trouble
if %ERRORLEVEL% == 1 goto Stopped
if %ERRORLEVEL% == 0 goto Going

:Trouble
Echo Other error so best to restart
echo Trouble error level is %ERRORLEVEL%
shutdown -r -t 01
goto end

:Stopped
Echo Media Centre was already stopped or maybe not exist but try devcon
goto Devcon

:Going
Echo Media Centre can now be stopped
net stop "Windows Media Center Receiver Service" /y
echo Stop media service error level is %ERRORLEVEL%
if %ERRORLEVEL% == 1 goto Trouble
if %ERRORLEVEL% == 0 goto Devcon

:Devcon
echo At Devcon
devcon restart usb\vid_2040*
echo devcon error level is %ERRORLEVEL%
if %ERRORLEVEL% == 1 goto Trouble
if %ERRORLEVEL% == 0 goto Continue

:Continue
rem ping 1.1.1.1 -n 3 -w 1000 >NUL
echo At Continue
net start "Windows Media Center Receiver Service"
echo net start error level is %ERRORLEVEL%
if %ERRORLEVEL% == 1 goto Trouble
%systemroot%\ehome\ehshell.exe /homepage: RecordedTV.BrowsePage.xml /PushStartPage: True - Recorded TV
echo WMC error level is %ERRORLEVEL%
goto end

:end
time /t
echo end
echo ****************************************
exitadd the following batch file:
Code: [Select]::ProcessTimer.bat
@echo off
if "%~1"=="" echo Usage: %0 ProcessName & goto:eof
%2start %comspec% /c %0 %1 :: & goto:eof

>nul ping -n 60 localhost
tasklist|find "%~1" && (taskkill/f /im "%~1" || %0 %1)
::pause

then change this line:
devcon restart usb\vid_2040*
to
call processtimer "devcon.exe"
devcon restart usb\vid_2040*


the same goes for net start:
call processtimer "net.exe"
net start "Windows Media Center Receiver Service"



what the ProcessTimer doing is wait 60seconds, then check if the process still running, if yes, force kill it, if it's still running, try again.

Warning: Killing a running process might produce unexpected result.


this is from the helpfile tasklist/? and might be interest for you:
Filters:
Filter Name Valid Operators Valid Value(s)
----------- --------------- --------------
STATUS eq, ne RUNNING | NOT RESPONDINGThanks for the suggestion. Have been unavailable a few days will try tonight and advise.
Cheers


Discussion

No Comment Found