Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7524929
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 30, 20262026-05-30T03:11:42+00:00 2026-05-30T03:11:42+00:00

I have several different processes and I would like them to all log to

  • 0

I have several different processes and I would like them to all log to the same file. These processes are running on a Windows 7 system. Some are python scripts and others are cmd batch files.

Under Unix you’d just have everybody open the file in append mode and write away. As long as each process wrote less than PIPE_BUF bytes in a single message, each write call would be guaranteed to not interleave with any other.

Is there a way to make this happen under Windows? The naive Unix-like approach fails because Windows doesn’t like more than one process having a file open for writing at a time by default.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-30T03:11:43+00:00Added an answer on May 30, 2026 at 3:11 am

    It is possible to have multiple batch processes safely write to a single log file. I know nothing about Python, but I imagine the concepts in this answer could be integrated with Python.

    Windows allows at most one process to have a specific file open for write access at any point in time. This can be used to implement a file based lock mechanism that guarantees events are serialized across multiple processes. See https://stackoverflow.com/a/9048097/1012053 and http://www.dostips.com/forum/viewtopic.php?p=12454 for some examples.

    Since all you are trying to do is write to a log, you can use the log file itself as the lock. The log operation is encapsulated in a subroutine that tries to open the log file in append mode. If the open fails, the routine loops back and tries again. Once the open is successful the log is written and then closed, and the routine returns to the caller. The routine executes whatever command is passed to it, and anything written to stdout within the routine is redirected to the log.

    Here is a test batch script that creates 5 child processes that each write to the log file 20 times. The writes are safely interleaved.

    @echo off
    setlocal
    if "%~1" neq "" goto :test
    
    :: Initialize
    set log="myLog.log"
    2>nul del %log%
    2>nul del "test*.marker"
    set procCount=5
    set testCount=10
    
    :: Launch %procCount% processes that write to the same log
    for /l %%n in (1 1 %procCount%) do start "" /b "%~f0" %%n
    
    :wait for child processes to finish
    2>nul dir /b "test*.marker" | find /c "test" | >nul findstr /x "%procCount%" || goto :wait
    
    :: Verify log results
    for /l %%n in (1 1 %procCount%) do (
      <nul set /p "=Proc %%n log count = "
      find /c "Proc %%n: " <%log%
    )
    
    :: Cleanup
    del "test*.marker"
    exit /b
    
    ==============================================================================
    :: code below is the process that writes to the log file
    
    :test
    set instance=%1
    for /l %%n in (1 1 %testCount%) do (
      call :log echo Proc %instance% says hello!
      call :log dir "%~f0"
    )
    echo done >"test%1.marker"
    exit
    
    :log command args...
    2>nul (
      >>%log% (
        echo ***********************************************************
        echo Proc %instance%: %date% %time%
        %*
        (call ) %= This odd syntax guarantees the inner block ends with success  =%
                %= We only want to loop back and try again if redirection failed =%
      )
    ) || goto :log
    exit /b
    

    Here is the output that demonstrates that all 20 writes were successful for each process

    Proc 1 log count = 20
    Proc 2 log count = 20
    Proc 3 log count = 20
    Proc 4 log count = 20
    Proc 5 log count = 20
    

    You can open the resulting “myLog.log” file to see how the writes have been safely interleaved. But the output is too large to post here.

    It is easy to demonstrate that simultaneous writes from multiple processes can fail by modifying the :log routine so that it does not retry upon failure.

    :log command args...
    >>%log% (
      echo ***********************************************************
      echo Proc %instance%: %date% %time%
      %*
    )
    exit /b
    

    Here are some sample results after “breaking” the :log routine

    The process cannot access the file because it is being used by another process.
    The process cannot access the file because it is being used by another process.
    The process cannot access the file because it is being used by another process.
    The process cannot access the file because it is being used by another process.
    The process cannot access the file because it is being used by another process.
    The process cannot access the file because it is being used by another process.
    The process cannot access the file because it is being used by another process.
    The process cannot access the file because it is being used by another process.
    The process cannot access the file because it is being used by another process.
    The process cannot access the file because it is being used by another process.
    The process cannot access the file because it is being used by another process.
    The process cannot access the file because it is being used by another process.
    The process cannot access the file because it is being used by another process.
    The process cannot access the file because it is being used by another process.
    The process cannot access the file because it is being used by another process.
    The process cannot access the file because it is being used by another process.
    The process cannot access the file because it is being used by another process.
    The process cannot access the file because it is being used by another process.
    The process cannot access the file because it is being used by another process.
    The process cannot access the file because it is being used by another process.
    The process cannot access the file because it is being used by another process.
    The process cannot access the file because it is being used by another process.
    The process cannot access the file because it is being used by another process.
    The process cannot access the file because it is being used by another process.
    The process cannot access the file because it is being used by another process.
    The process cannot access the file because it is being used by another process.
    The process cannot access the file because it is being used by another process.
    Proc 1 log count = 12
    Proc 2 log count = 16
    Proc 3 log count = 13
    Proc 4 log count = 18
    Proc 5 log count = 14
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have several different lists I want to call. They all have the same
If several instances of the same code are running on different servers, I would
I have several different content type nodes (videos, image galleries, stories...) that I would
We have several websites on different domains and I'd like to be able to
Say I have a .mat file with several instances of the same structure, each
We have a scripting engine (running under Windows) that launches another application several thousand
I have some data files from a legacy system that I would like to
I have several different c# worker applications that run various continuous tasks: sending emails
We have several different optimization algorithms that produce a different result for each run.
I have several files that are in several different languages. I thought they were

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.