Solved

CMD prompt intermittently hangs after bat file execution

Posted on 2010-11-26
4
913 Views
Last Modified: 2012-05-10
Hi All

Here in the office we have a bat file which runs every 5 minutes. This bat file ftp's to a remote location, grabs a couple of files and then inputs the contents of those files into a database.

We are having a problem lately whereby the CMD prompt which the bat file opens does not close. It just hangs there on the screen blank. As a result of that the scheduled task which is supposed to run every 5 mins does not run.

This doesnt happen every time but does happen frequently which means I get phone calls/emails wondering whats going on.

What I want to find out is how to makle sure that if the prompt hangs on the screen I have some way of killing it automatically. Whether that is some other sort of scheduled task that searches for it and kills it if it sees it or what I dont know,

All suggestions are apreciated and a rough copy of the bat file is below

@echo off 
echo Starting Import > Import.log 
FTP -s:ftp.txt >> Import.log 
ren *.csv *.process
for /f %%a in ('dir /b *.process') do sqlldr userid=username@database log=loader.log data="%%a" control=loader.ctl 
sqlplus -s username@database@runbatchsql.sql >> Import.log  
ren *.process *.complete
del /Q *.complete
echo Import Complete >> Import.log
EXIT

Open in new window

0
Comment
Question by:darran_d
4 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 250 total points
ID: 34217343
Your batch file is not reentrant. If an execution takes more than 5 minutes, a second process will start working in the same folder on the same files that the first process is still working on. This can cause all kinds of trouble.
I'd recommend to NOT schedule this to run every 5 minutes.
Instead, schedule it to run once, and let it loop infinitely (or set an end time with the scheduler), with a pause of 5 minutes or less inbetween loops.
In case you don't have it yet, sleep.exe is part of the W2k3 Resource Kit Tools, http://www.microsoft.com/downloads/en/details.aspx?FamilyID=9D467A69-57FF-4AE7-96EE-B18C4790CFFD&displaylang=en.

@echo off 
echo %Date% %Time% Starting Import >Import.log
:Loop
echo %Date% %Time% Continuing Import >>Import.log
FTP -s:ftp.txt >>Import.log
ren *.csv *.process
for /f %%a in ('dir /b *.process') do sqlldr userid=username@database log=loader.log data="%%a" control=loader.ctl 
sqlplus -s username@database@runbatchsql.sql >>Import.log  
ren *.process *.complete
del /Q *.complete
echo %Date% %Time% Import Complete >>Import.log
sleep 300
goto :Loop

Open in new window

0
 
LVL 68

Assisted Solution

by:Qlemo
Qlemo earned 250 total points
ID: 34217387
The batch file does not need to be reentrant, as the scheduler will not try to run the task again as long as it is hanging. But if you provide a time for how long the batch is allowed to run in the task, the batch will be killed. The risk of that is that data might get imported repeatedly, if the SQL*Loader executed successful on at least one .process file, but the remainder did not work. It would be better to include the rename into the FOR loop - load one file, rename it, next file.
0
 

Author Closing Comment

by:darran_d
ID: 34391524
The problem seems to have resolved itself for some reason but all comments were technically correct so I am splitting the points among all respons
0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Active Directory replication delay is the cause to many problems.  Here is a super easy script to force Active Directory replication to all sites with by using an elevated PowerShell command prompt, and a tool to verify your changes.
ADCs have gained traction within the last decade, largely due to increased demand for legacy load balancing appliances to handle more advanced application delivery requirements and improve application performance.
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…
Video by: Mark
This lesson goes over how to construct ordered and unordered lists and how to create hyperlinks.

920 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

16 Experts available now in Live!

Get 1:1 Help Now