Solved

PC Batch File: Wait for the job finishing

Posted on 2004-04-13
12
18,847 Views
Last Modified: 2008-03-10
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
Comment
Question by:tkchen
  • 6
  • 6
12 Comments
 
LVL 10

Expert Comment

by:Kavar
ID: 10815853
rem use the start command
start /wait "exename.exe"
0
 
LVL 1

Author Comment

by:tkchen
ID: 10817010
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
 
LVL 10

Expert Comment

by:Kavar
ID: 10817085
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
DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

 
LVL 1

Author Comment

by:tkchen
ID: 10818079
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
 
LVL 10

Expert Comment

by:Kavar
ID: 10819345
they run simulataneously, it looks as if it waits for one, but it doesn't
0
 
LVL 1

Author Comment

by:tkchen
ID: 10823143

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
 
LVL 10

Expert Comment

by:Kavar
ID: 10823243
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
 
LVL 1

Author Comment

by:tkchen
ID: 10823537
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
 
LVL 10

Expert Comment

by:Kavar
ID: 10823604
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
 
LVL 1

Author Comment

by:tkchen
ID: 10823745

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
 
LVL 10

Accepted Solution

by:
Kavar earned 250 total points
ID: 10823800
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
 
LVL 1

Author Comment

by:tkchen
ID: 10824740
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

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
C# Error - Add Failed 12 87
Change path in batch file 2 40
Batch/VBscript: import massively chedule task in windows task scheduler 15 45
printf performancy 11 36
A short article about a problem I had getting the GPS LocationListener working.
Whether you've completed a degree in computer sciences or you're a self-taught programmer, writing your first lines of code in the real world is always a challenge. Here are some of the most common pitfalls for new programmers.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
With the power of JIRA, there's an unlimited number of ways you can customize it, use it and benefit from it. With that in mind, there's bound to be things that I wasn't able to cover in this course. With this summary we'll look at some places to go…

776 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question