[Last Call] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 18854
  • Last Modified:

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
0
tkchen
Asked:
tkchen
  • 6
  • 6
1 Solution
 
KavarCommented:
rem use the start command
start /wait "exename.exe"
0
 
tkchenAuthor Commented:
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

0
 
KavarCommented:
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>
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
tkchenAuthor Commented:
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
0
 
KavarCommented:
they run simulataneously, it looks as if it waits for one, but it doesn't
0
 
tkchenAuthor Commented:

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
0
 
KavarCommented:
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
0
 
tkchenAuthor Commented:
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
0
 
KavarCommented:
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
0
 
tkchenAuthor Commented:

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.
0
 
KavarCommented:
to open a file in vbscript
dim fso,infile
set fso=createobject("scripting.filesystemobject")
set infile=fso.opentextfile("<file name>")
bloboftext=infile.readall

(now blob of text has all text in the text file)

To read a directory
dim fso,folder,file
set fso=createobject("scripting.filesystemobject")
set folder=fso.getfolder("<path to folder>")
for each file in folder.files
  wscript.echo file.name
next
0
 
tkchenAuthor Commented:
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
0

Featured Post

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

  • 6
  • 6
Tackle projects and never again get stuck behind a technical roadblock.
Join Now