Link to home
Start Free TrialLog in
Avatar of tkchen
tkchen

asked on

PC Batch File: Wait for the job finishing

A commercial code (code_A) runs on command line window (win2k and win9x). After the job is submitted, the control will be returned to the window immediately and two or three consecutive sub-programs (p1, p2 & p3) on background (cpu time = 1 min. to 2 weeks). The sub-program shows in processes window of task manager, but does not show in application window of task manager. Since the code is commercial software, sometime the sub-programs are suspend for network license.

I like to have a batch file that runs the code_A iteratively (100 times). The question is how to wait for the job (p2 or p3) to finish in the batch file?  

Thanks

TKC
Avatar of Kavar
Kavar

rem use the start command
start /wait "exename.exe"
Avatar of tkchen

ASKER

thanks, Kavar,
         I have tried this one before without success.

 If run the code directly,
 d:\tmp> Code_a input=aaa.txt
The prompt returns quickly and program runs on background.

 if run using start command using batch file (run_code.bat) as

echo off
start /wait Code_a input=aaa.txt

and run the batch file in command line window (W1).

d:\tmp> run_code.bat

A new command line window (W2) was generated to submit the job. The prompt (cursor) returns immediately in new window (W2) and program runs in background.

The prompt of the first window (W1) was returned after the w2 closed manually and answer the question as

^CTerminate batch job (Y/N)? y

Any other ideas?

Thanks again.

TKC

are you asking on how to wait on both 2 and 3, but not 2 before 3?

If so the only easy way I can think of this is to use semiphore files in batch or use vbscript.
(vbscript is your best option in my opinion)

'begin script
dim command1
dim command2
command1="step2 exe"
command2="step3 exe"
dim wsh
set wsh=createobject("wscript.shell")
set exe1=wsh.exec(command1)
set exe2=wsh.exec(command2)
do while exe1.status=1 or exe2.status=1
wscript.sleep 1000
loop
'end script

save this as a vbs, then in your batch, do a start /wait C:\winnt\system32\cscript.exe <vbsfilename>
Avatar of tkchen

ASKER

Hello, Kavar,
    Thanks for the quick reply.
    It sounds good except a small problem. The step2 and step3 are not execute in simultaneously but in sequence. Therefore , an error message will show if anyone is not runing.

set exe1=wsh.exec(command1)
set exe2=wsh.exec(command2)

Thanks.

TKC
they run simulataneously, it looks as if it waits for one, but it doesn't
Avatar of tkchen

ASKER


In the analysis, sub_program 1 always excecute. The sub-2 will start after sub-1 finished. sub-3 may or may not start after sub-2 finished, depend upon the conditions.

In the Vbscript, it requires both sub-2 and sub-3 run simultaneously. It may need several  "IF" statements to check the statuses of sub-1, sub-2 and sub-3.

Sorry for the confusion.

Thanks.

TKC
so in vbscript...(notice I had my run bits swapped, <>1 means still running =1 means done

'begin script
dim command1
dim command2
dim command3
command1="calc.exe"
command2="calc.exe"
command3="calc.exe"
dim wsh
set wsh=createobject("wscript.shell")
set exe1=wsh.exec(command1)
do while exe1.status<>1
wscript.sleep 1000
loop
set exe2=wsh.exec(command2)
set exe3=wsh.exec(command3)

do while exe2.status<>1 or exe3.status<>1
If exe3.status<>1 and StopCondition=True Then
      exe3.terminate
End if
wscript.sleep 1000
loop

Function StopCondition()
'do some checking here to see of we should stop exe3
End Function
'end script
Avatar of tkchen

ASKER

Thanks.
Is there a way to check the status of a file and print it out using VBscript?
Sometime the codes (sub-1,2,3) are waiting for the license to start. If the codes not start,  set exe1=wsh.exec(command1) will generate an error message and stop. Therefore, I will check if the sub-1.out,sub-2.out,sub-3.out  files exist before check the status of code itself.

TKC
well in the script that I posted,
you can use exe1.stdout.readline to get any console responses
and exe1.stderr.readline to get any console errors
Avatar of tkchen

ASKER


Actually I like to check the status of a file. Should I use "dir' command in VBScript?

I thick it need to check if sub-1.out exist then check the status of sub-1 code. If sub-1.out not exist, wait for it.

BTW. If "exe1.stdout.readline" can get any console responses, can you solve my other question as well (post same time yesterday)?

Thanks.
ASKER CERTIFIED SOLUTION
Avatar of Kavar
Kavar

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of tkchen

ASKER

Thanks for the help. I think I can use the information to generate a VBscript file and conduction the analysis.

I appreciate your help and thanks again.

Best wishes to you.

TKC