Solved

PC Batch File: Wait for the job finishing

Posted on 2004-04-13
12
18,843 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
 
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
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

I have published numerous articles here at Experts Exchange that present programs/scripts written in a language called AutoHotkey. Each of those articles has a brief paragraph describing where to download the product and how to install it. I have al…
Go is an acronym of golang, is a programming language developed Google in 2007. Go is a new language that is mostly in the C family, with significant input from Pascal/Modula/Oberon family. Hence Go arisen as low-level language with fast compilation…
An introduction to basic programming syntax in Java by creating a simple program. Viewers can follow the tutorial as they create their first class in Java. Definitions and explanations about each element are given to help prepare viewers for future …
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

747 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now