CMD.EXE and ">> logfile"

Discussion in 'Scripting' started by Peter Juuls, Aug 3, 2006.

  1. Peter Juuls

    Peter Juuls Guest

    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
    Peter Juuls, Aug 3, 2006
    #1
    1. Advertising

  2. Peter Juuls

    Jim Guest

    "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
    >
    Jim, Aug 3, 2006
    #2
    1. Advertising

  3. Peter Juuls

    Babu VT Guest

    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
    Babu VT, Aug 3, 2006
    #3
  4. Peter Juuls

    David Brown Guest

    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
    David Brown, Aug 3, 2006
    #4
  5. Peter Juuls

    Peter Juuls Guest

    "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.
    Peter Juuls, Aug 4, 2006
    #5
  6. Peter Juuls

    Peter Juuls Guest

    > 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
    Peter Juuls, Aug 4, 2006
    #6
  7. Peter Juuls

    Peter Juuls Guest

    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.
    Peter Juuls, Aug 4, 2006
    #7
  8. Peter Juuls

    Jason Gurtz Guest

    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

    --
    Jason Gurtz, Aug 4, 2006
    #8
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. bestofcomputer
    Replies:
    1
    Views:
    254
    Jerold Schulman
    Dec 31, 2005
  2. bestofcomputer
    Replies:
    4
    Views:
    298
    bestofcomputer
    Jan 3, 2006
  3. bestofcomputer
    Replies:
    2
    Views:
    262
    bestofcomputer
    Jan 3, 2006
  4. A-S-I-F
    Replies:
    0
    Views:
    1,082
    A-S-I-F
    Jul 8, 2009
  5. VirendraSharma
    Replies:
    1
    Views:
    223
    Stefan Olsson / [payload]
    Jul 27, 2005
Loading...

Share This Page