Windows Vista Tips

Windows Vista Tips > Newsgroups > Windows Server > Scripting > CMD.EXE and ">> logfile"

Reply
Thread Tools Display Modes

CMD.EXE and ">> logfile"

 
 
Peter Juuls
Guest
Posts: n/a

 
      08-03-2006
I use scheduled batch files do many automated tasks on my Win2000/Win2003
servers, like checking for available diskspace. All batch files write to a
single logfile on the local disk, so I only need to check one logfile for
problems. Batch files always append a new line to the bottom of the logfile,
ex.
ECHO %DATE% %TIME% : Diskchecker says: Space is ok. >> central.log

PROBLEM: From time to time, the logfile is being deleted, for no obvious
reason. It seems to happen only when two batch files simultanously append a
record to the bottom of the logfile. CMD.EXE or some NTFS filesystem module
should coordinate this process and ensure that both records are appended,
instead of deleting the file, but apparently does not.

Q: How can I solve this? (I don't want to split up "central.log" into
seperate logfiles)

Best regards
Peter Juuls


 
Reply With Quote
 
 
 
 
Jim
Guest
Posts: n/a

 
      08-03-2006
"CMD.EXE or some NTFS filesystem module
should coordinate this process and ensure that both records are appended,
instead of deleting the file, but apparently does not.
"

where did you get this idea? this is not a multi-user database, its a simple
text file we're talking about.
you might consider a much better approach... forget the batch files and
write your batch jobs in VBS, and maybe save results to an access database,
or spreadsheet... anything is better than a batch/text file combination




"Peter Juuls" <> wrote in message
news:%...
>I use scheduled batch files do many automated tasks on my Win2000/Win2003
>servers, like checking for available diskspace. All batch files write to a
>single logfile on the local disk, so I only need to check one logfile for
>problems. Batch files always append a new line to the bottom of the
>logfile, ex.
> ECHO %DATE% %TIME% : Diskchecker says: Space is ok. >> central.log
>
> PROBLEM: From time to time, the logfile is being deleted, for no obvious
> reason. It seems to happen only when two batch files simultanously append
> a record to the bottom of the logfile. CMD.EXE or some NTFS filesystem
> module should coordinate this process and ensure that both records are
> appended, instead of deleting the file, but apparently does not.
>
> Q: How can I solve this? (I don't want to split up "central.log" into
> seperate logfiles)
>
> Best regards
> Peter Juuls
>



 
Reply With Quote
 
 
 
 
Babu VT
Guest
Posts: n/a

 
      08-03-2006
Hi Peter,

Worth checking if you have any > instead of >> in any of your
scripts.As you know > will overwrite the central.log file and *not*
append..
Also what is the point in writing simultaneously which can result in
garbled letters if possible to write! Only one application would need
to write to a log at a time and then followed by other!

rgds
Babu
After serious thinking Peter Juuls wrote :
> I use scheduled batch files do many automated tasks on my Win2000/Win2003
> servers, like checking for available diskspace. All batch files write to a
> single logfile on the local disk, so I only need to check one logfile for
> problems. Batch files always append a new line to the bottom of the logfile,
> ex.
> ECHO %DATE% %TIME% : Diskchecker says: Space is ok. >> central.log
>
> PROBLEM: From time to time, the logfile is being deleted, for no obvious
> reason. It seems to happen only when two batch files simultanously append a
> record to the bottom of the logfile. CMD.EXE or some NTFS filesystem module
> should coordinate this process and ensure that both records are appended,
> instead of deleting the file, but apparently does not.
>
> Q: How can I solve this? (I don't want to split up "central.log" into
> seperate logfiles)
>
> Best regards
> Peter Juuls



 
Reply With Quote
 
David Brown
Guest
Posts: n/a

 
      08-03-2006
Hello Peter,

I came up with a trick to avoid running too many of the same process at once,
you may be able to modify it for your purposes:

============
do first thing
call :wait_task
do second thing

:wait_task
:recheck
tasklist /fi "imagename eq netsh.exe" | find /i /c "netsh.exe" > NUL
if %errorlevel% EQU 0 goto recheck

goto :EOF
=============

The reaoson I wrote this was I was cleaning up a WINS database and was sending
a owner delete command to each of our 50 WINS servers using netsh. I wanted
wait for all of the processes to complete before moving on to the next task.


It works like this - Tasklist checks for a running instance of netsh.exe.
The output of tasklist is pipe to find, which looks for netsh.exe in the
output. If found, errorlevel will equal 0 so recheck, if not found, errorlevel
will equal 1, so continue. The "> NUL" sends the output of the find command
to nowhere rather than the screen.

With this you can check from any command instance, not just the one that
initiated the task. Tasklist can also check for tasks on remote machines.

> I use scheduled batch files do many automated tasks on my
> Win2000/Win2003
> servers, like checking for available diskspace. All batch files write
> to a
> single logfile on the local disk, so I only need to check one logfile
> for
> problems. Batch files always append a new line to the bottom of the
> logfile,
> ex.
> ECHO %DATE% %TIME% : Diskchecker says: Space is ok. >> central.log
> PROBLEM: From time to time, the logfile is being deleted, for no
> obvious reason. It seems to happen only when two batch files
> simultanously append a record to the bottom of the logfile. CMD.EXE or
> some NTFS filesystem module should coordinate this process and ensure
> that both records are appended, instead of deleting the file, but
> apparently does not.
>
> Q: How can I solve this? (I don't want to split up "central.log" into
> seperate logfiles)
>
> Best regards
> Peter Juuls



 
Reply With Quote
 
Peter Juuls
Guest
Posts: n/a

 
      08-04-2006

"Jim" <> skrev i en meddelelse
news:...
> "CMD.EXE or some NTFS filesystem module
> should coordinate this process and ensure that both records are appended,
> instead of deleting the file, but apparently does not.
> "
>
> where did you get this idea? this is not a multi-user database, its a
> simple text file we're talking about.


I (naively?) expected the operating system/file system would handle such a
basic operation, just like it handles other basic operations on files.

> you might consider a much better approach... forget the batch files and
> write your batch jobs in VBS, and maybe save results to an access
> database, or spreadsheet... anything is better than a batch/text file
> combination


I agree that taking another approach like writing a VBS-script would
probably solve the problem, because VBS will ensure that appending a line to
the logfile actually takes place. I might choose that approach, if I cannot
get the CMD.EXE-appending to work.


 
Reply With Quote
 
Peter Juuls
Guest
Posts: n/a

 
      08-04-2006
> Also what is the point in writing simultaneously which can result in
> garbled letters if possible to write! Only one application would need to
> write to a log at a time and then followed by other!


I do not need batch jobs to write simultanously, but it just happens from
time to time. They are scheduled to run every 10 minutes or every 30 minutes
and occasionally two jobs start at the same time. The first thing they do is
writing "I am starting now...", to the central logfile, and when two jobs
do that, at the same scheduled time, they may conflict.

I get your point: I could insert a delay-function in one of the batch-files
to avoid conflicts - or wite to seperate logfiles, ie. each job has a
logfile.

Best regards
Peter Juuls


 
Reply With Quote
 
Peter Juuls
Guest
Posts: n/a

 
      08-04-2006
Hi David,

Good idea, having the batch files check for running instances, and wait
until other instances have finished. A similar (and more generalized)
concept, that I actually used in another setup, is raising a flag (a flag
file), signalling to batch jobs to wait, ie. every batch job, that wants to
do a single-job-operation, like append lines to the logfile, checks if the
flag is raised, before it starts doing the job. If the flag is raised, the
batch job must wait x seconds, and rechecks the flag. When finally permitted
to start, the batch file raise the flag, does the job, and lower the flag.
The drawback of this concept is that jobs may crash while the flag is up and
therefore waiting jobs will keep waiting forever. So you need to find a way
to handle that situation, too.

Your suggestion and the flag concept might be necessary, to do what I want,
but it's a lot of work too. I hope to find a simpler solution.

Best regards
Peter Juuls

> I came up with a trick to avoid running too many of the same process at
> once, you may be able to modify it for your purposes:
>
> ============
> do first thing
> call :wait_task
> do second thing
>
> :wait_task
> :recheck
> tasklist /fi "imagename eq netsh.exe" | find /i /c "netsh.exe" > NUL
> if %errorlevel% EQU 0 goto recheck
> goto :EOF
> =============
>
> The reaoson I wrote this was I was cleaning up a WINS database and was
> sending a owner delete command to each of our 50 WINS servers using netsh.
> I wanted wait for all of the processes to complete before moving on to the
> next task.
>
> It works like this - Tasklist checks for a running instance of netsh.exe.
> The output of tasklist is pipe to find, which looks for netsh.exe in the
> output. If found, errorlevel will equal 0 so recheck, if not found,
> errorlevel will equal 1, so continue. The "> NUL" sends the output of the
> find command to nowhere rather than the screen.
> With this you can check from any command instance, not just the one that
> initiated the task. Tasklist can also check for tasks on remote machines.




 
Reply With Quote
 
Jason Gurtz
Guest
Posts: n/a

 
      08-04-2006
Peter Juuls wrote:
> Good idea, having the batch files check for running instances, and wait
> until other instances have finished.


Yea, just have a central "multiplexer" that controls logging or else a
simple control batch file that starts the other batch files sequentially
via "cmd /c".

If you are really determined to have these things running in parallel
then you will need to implement some kind of locking or synchronization
scheme. The spinlock method seems like it could be used successfully in
batch files. What you would do is in each batch file your logging
procedure first goes into a loop, checking for existence of a lock file;
if lock file not exist then exit loop, create lock file and do logging
then delete lock file. Just be careful to trap errors so the file is
always deleted. This may still fail in interesting ways on rare
occasions because you are racing against the kernel scheduler. It
should work for the most part though. See:
<http://en.wikipedia.org/wiki/Spinlock>

Since this is just text file logging, you could rely on the fact that
logging shouldn't ever take a long time and have each one delete the
spin lock and go ahead if they've checked more than n times and it's
still there. It's dirty, so test and do at your own risk. Use the ping
-n %seconds% 127.0.0.1 >NUL trick as a sleep interval.

~Jason

--
 
Reply With Quote
 
 
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Network Adapter Keeps Turning On and Off and On and Off and ... JP Windows Vista General Discussion 2 01-04-2009 10:50 PM
It goes on and on and on and on and never ends! zuoer Windows Vista General Discussion 32 07-23-2008 11:26 AM
to nancy and family from Uncle richard and and aunt susan. Uncle Richard and Aunt Sue. Windows Vista Mail 1 12-08-2007 12:46 PM
AoE3 and Display Driver and Comand and Concer 3 Eric Rich Windows Vista Games 0 04-12-2007 11:18 PM
Vista has never correctly installed, after dozens and dozens of tries and removal of cards and everything. Wisgary Windows Vista Installation 6 03-16-2007 12:42 PM