我有几个不同的进程,我希望他们都登录到同一个文件.这些进程在 Windows 7系统上运行.一些是python脚本,另一些是cmd批处理文件.

在Unix下,你只需让所有人以附加模式打开文件并写下来.只要每个进程在单个消息中写入的PIPE_BUF字节少于PIPE_BUF字节,就可以保证每个写入调用不与任何其他写入调用交错.

有没有办法在Windows下实现这一目标?天真的类Unix方法失败了,因为Windows不喜欢默认情况下一次打开文件的多个进程.

可以将多个批处理过程安全地写入单个日志文件.我对 Python一无所知,但我想这个答案中的概念可以与Python集成.

Windows允许最多一个进程在任何时间点打开特定文件以进行写访问.这可用于实现基于文件的锁定机制,以确保事件在多个进程中被序列化.有关示例,请参见https://stackoverflow.com/a/9048097/1012053和http://www.dostips.com/forum/viewtopic.php?p=12454.

由于您要做的只是写入日志,因此您可以将日志文件本身用作锁定.日志操作封装在子例程中,该子例程尝试以追加模式打开日志文件.如果打开失败,则例程循环返回并再次尝试.一旦打开成功,将写入日志然后关闭,并且例程返回给调用者.例程执行传递给它的任何命令,并且在例程中写入stdout的任何内容都会重定向到日志.

这是一个测试批处理脚本,它创建5个子进程,每个进程写入日志文件20次.写入是安全交错的.

@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

以下是输出,表明每个进程的所有20次写入都是成功的

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

您可以打开生成的“myLog.log”文件,以查看写入是如何安全交错的.但输出太大,无法在此发布.

通过修改:log例程以便在失败时不重试,很容易证明来自多个进程的同时写入可能会失败.

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

以下是“破解”:log例程后的一些示例结果

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

如何在Windows下共享日志文件?的更多相关文章

  1. Xcode 6 – iPhone崩溃日志文件

    在以前的Xcode版本中,我可以导入一个.crash崩溃日志文件,该文件是通过复制iPhone上“诊断和使用”部分的崩溃报告而获得的.在Xcode6中,我似乎无法在主办单位找到一种办法.我有.crash文件,但无法找到任何地方导入它来象征它.在Xcode6中没有连接物理设备的情况下可以做到吗?解决方法>在Xcode–窗口中打开–>设备.>连接设备,如果没有附加–!

  2. Android系统日志文件位置

    在ubuntu中有/var/log/syslog文件.我的问题是什么是android中的ubuntus系统日志的等价文件,我知道我有一个root用户的Android手机.解决方法有几种方法可以查看系统日志–但是查找日志文件的位置要比较困难.我将列出3个选项,允许您查看日志,但我不知道它们保存在哪里.我可能错了,但从我收集的内容来看,Android系统并没有保留“日志文件”.相反,有一个缓冲区包含“日志”!

  3. Android Logcat旋转大小和旋转参数数

    我试图弄清楚logcatutilitiy的-r和-n参数是什么意思.当我的日志文件超过-r千字节数时;它会删除我的日志文件的整个内容,使其保留为空或创建新文件以继续记录过程?究竟是什么意思–也意味着,ofc?

  4. Node.js 在本地生成日志文件的方法

    这篇文章主要介绍了Node.js 在本地生成日志文件的方法,本文通过实例代码给大家介绍的非常详细,具有一定的参考借鉴价值,需要的朋友可以参考下

  5. PHP简单实现定时监控nginx日志文件功能示例

    这篇文章主要介绍了PHP简单实现定时监控nginx日志文件功能,涉及php定时读取nginx服务器日志以及基于curl的数据传输相关操作技巧,需要的朋友可以参考下

  6. 如何在Windows下共享日志文件?

    我有几个不同的进程,我希望他们都登录到同一个文件.这些进程在Windows7系统上运行.一些是python脚本,另一些是cmd批处理文件.在Unix下,你只需让所有人以附加模式打开文件并写下来.只要每个进程在单个消息中写入的PIPE_BUF字节少于PIPE_BUF字节,就可以保证每个写入调用不与任何其他写入调用交错.有没有办法在Windows下实现这一目标?

  7. 如何在Windows中旋转tomcat日志?什么是最好的方法?

    我真的厌倦了在我们的Windows服务器中放入无数小时后和尝试各种解决方案后无法解决tomcat日志轮换问题.>环境:Java1.6x,Tomcat6(作为服务运行),log4j1.2,Server200332位迄今为止尝试的方法:>cronolog没有工作,没有设置后创建的文件.>管理为Tomcat全局配置DailyRollingFileappender的log4j,正在创建没有扩展名的日志文件

  8. centos – 显示错误的磁盘大小

    对于其中一个磁盘,Df-h在我的服务器上显示错误的空白区域:Cpanel表明它只有34GB免费,但还有更多.几分钟前,我删除了超过80GB的日志文件.所以,我确信它完全错了.fdisk-l/dev/sda2也显示错误:如果没有格式化,我该怎么做才能解决这个问题?并且打开文件描述符就是它需要使用才能做到这一点.所以…使用“lsof”并查找已删除的文件.重新启动写入日志文件的服务,你很可能会看到空间可用.

  9. windows – 如何在不锁定perl的情况下拖尾日志文件

    我不知道你是否使用Windows,尽管shebang系列没有提示.因此,有关您环境的更多详细信息将会有所帮助.

  10. centos – 如何删除旧的日志文件?

    我试图找出如何删除旧的日志文件,我正在使用Centos6.5,在我的/var/log中我看到这些旧的日志文件在/var/log/httpd目录中也是一样的我需要删除以数字值结尾的旧文件,但保留主文件一次.我可以用什么命令来做这件事.提前致谢您可以使用以下命令:它将删除所有以“messages-”和“access_log-”开头的旧日志

随机推荐

  1. static – 在页面之间共享数据的最佳实践

    我想知道在UWP的页面之间发送像’selectedItem’等变量的最佳做法是什么?创建一个每个页面都知道的静态全局变量类是一个好主意吗?

  2. .net – 为Windows窗体控件提供百分比宽度/高度

    WindowsForm开发的新手,但在Web开发方面经验丰富.有没有办法为Windows窗体控件指定百分比宽度/高度,以便在用户调整窗口大小时扩展/缩小?当窗口调整大小时,可以编写代码来改变控件的宽度/高度,但我希望有更好的方法,比如在HTML/CSS中.在那儿?

  3. 使用Windows Azure查询表存储数据

    我需要使用特定帐户吗?>将应用程序部署到Azure服务后,如何查询数据?GoogleAppEngine有一个数据查看器/查询工具,Azure有类似的东西吗?>您可以看到的sqlExpressintance仅在开发结构中,并且一旦您表示没有等效,所以请小心使用它.>您可以尝试使用Linqpad查询表格.看看JamieThomson的thispost.

  4. windows – SetupDiGetClassDevs是否与文档中的设备实例ID一起使用?

    有没有更好的方法可以使用DBT_DEVICEARRIVAL事件中的数据获取设备的更多信息?您似乎必须指定DIGCF_ALLCLASSES标志以查找与给定设备实例ID匹配的所有类,或者指定ClassGuid并使用DIGCF_DEFAULT标志.这对我有用:带输出:

  5. Windows Live ID是OpenID提供商吗?

    不,WindowsLiveID不是OpenID提供商.他们使用专有协议.自从他们的“测试版”期结束以来,他们从未宣布计划继续它.

  6. 如果我在代码中进行了更改,是否需要重新安装Windows服务?

    我写了一个Windows服务并安装它.现在我对代码进行了一些更改并重新构建了解决方案.我还应该重新安装服务吗?不,只需停止它,替换文件,然后重新启动它.

  7. 带有双引号的字符串回显使用Windows批处理输出文件

    我正在尝试使用Windows批处理文件重写配置文件.我循环遍历文件的行并查找我想要用指定的新行替换的行.我有一个’函数’将行写入文件问题是%Text%是一个嵌入双引号的字符串.然后失败了.可能还有其他角色也会导致失败.如何才能使用配置文件中的所有文本?尝试将所有“在文本中替换为^”.^是转义字符,因此“将被视为常规字符你可以尝试以下方法:其他可能导致错误的字符是:

  8. .net – 将控制台应用程序转换为服务?

    我正在寻找不同的优势/劣势,将我们长期使用的控制台应用程序转换为Windows服务.我们为ActiveMQ使用了一个叫做java服务包装器的东西,我相信人们告诉我你可以用它包装任何东西.这并不是说你应该用它包装任何东西;我们遇到了这个问题.控制台应用程序是一个.NET控制台应用程序,默认情况下会将大量信息记录到控制台,尽管这是可配置的.任何推荐?我们应该在VisualStudio中将其重建为服务吗?我使用“-install”/“-uninstall”开关执行此操作.例如,seehere.

  9. windows – 捕获外部程序的STDOUT和STDERR *同时*它正在执行(Ruby)

    哦,我在Windows上:-(实际上,它比我想象的要简单,这看起来很完美:…是的,它适用于Windows!

  10. windows – 当我试图批量打印变量时,为什么我得到“Echo is on”

    我想要执行一个简单的批处理文件脚本:当我在XP中运行时,它给了我预期的输出,但是当我在Vista或Windows7中运行它时,我在尝试打印值时得到“EchoisOn”.以下是程序的输出:摆脱集合表达式中的空格.等号(=)的两侧可以并且应该没有空格BTW:我通常在@echo关闭的情况下启动所有批处理文件,并以@echo结束它们,所以我可以避免将代码与批处理文件的输出混合.它只是使您的批处理文件输出更好,更清洁.

返回
顶部