windows - 进程运行时清空日志文件?

标签 windows batch-file windows-server-2003

今天我运行了一个批处理脚本。实际上,它仍在运行,所以我希望在为时已晚之前找出解决方案。

my_script.bat > output.log

事实证明,该日志文件比我预期的要大得多。哎呀!所以我想截断它。所以我尝试了这个,但失败了:

echo. > output.log
The process cannot access the file because it is being used by another process.

呃哦。它适用于 Linux,所以我想我只是假设它也适用于 Windows。我能做什么?

我可以停止我的批处理脚本,然后使用更智能的日志记录重新启动它。我的脚本如下所示:

echo "First iteration"
my_program.exe --parameters

echo "Second iteration"
my_program.exe --parameters

echo "Third iteration"
my_program.exe --parameters
...

我不想kill my_program.exe 因为它正在做一些非常重要的事情。我希望批处理脚本在 my_program.exe 完成后“中断”。我担心如果我按 Ctrl-C,它会杀死 my_program.exe,这不太好。

我能做些什么:

  • “正在运行”截断我的日志文件
  • 杀死我的批处理脚本,同时不伤害其当前正在执行的进程?

这是一个 Windows 2003 SP2 服务器。帮助!

最佳答案

文件是用 FILE_SHARE_READ 打开的,所以你不能删除或截断它。但是您可以使用以下技巧:

这将阻止 cmd.exe 写入日志文件:

  1. 下载process explorer .
  2. 运行它。
  3. 选择“查找”->“查找句柄或 DLL”。
  4. 输入日志文件的名称 (output.log)。
  5. 它必须在 cmd.exe 中找到。
  6. 选择它。
  7. 查看下方 Pane 。
  8. 将在该 cmd.exe 中列出所有句柄。
  9. 选择并关闭日志文件的句柄。

脚本再往前走,会停止日志,出现“句柄无效”。在控制台中。

关于windows - 进程运行时清空日志文件?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7406741/

相关文章:

windows - Python 看不到 C :\Windows\System32\GroupPolicy 中的文件或文件夹

windows - 通过ssh ping Windows时如何修复 “Unreachable”?

c++ - TabCtrl_InsertItem : do we need to keep memory of the text alive?

linux - 使用linux从网站下载批处理文件

visual-studio-2010 - 无法使用 VS2010 进行远程调试

windows - 我们可以对 Azure VM 虚拟 IP 使用 Route 53 或任何托管 DNS 吗?

batch-file - 使用批处理文件暂停 exe

windows - 批量合并 CSV 删除 header

java - 创建文件夹有效,但权限未继承

asp.net - 如何在 Visual Studio 2010 中调试 ASPNET 工作进程的转储文件?