windows 进程运行时清空日志文件?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7406741/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Empty log file while process is running?
提问by poundifdef
Today I ran a batch script. Actually, it is still running, so I'm hoping to figure out a solution before it is too late.
今天我运行了一个批处理脚本。实际上,它仍在运行,所以我希望在为时已晚之前找出解决方案。
my_script.bat > output.log
It turns out that that log file is going to be much much bigger than I was expecting. Oops! So I want to truncate it. So I tried this, which failed:
事实证明,该日志文件将比我预期的要大得多。哎呀!所以我想截断它。所以我尝试了这个,但失败了:
echo. > output.log
The process cannot access the file because it is being used by another process.
Uh oh. It works on Linux, so I guess I just assumed that it'd work on Windows too. What can I do?
哦哦。它适用于 Linux,所以我想我只是假设它也适用于 Windows。我能做什么?
I could stop my batch script and then re-start it with smarter logging. My script looks like this:
我可以停止我的批处理脚本,然后使用更智能的日志记录重新启动它。我的脚本如下所示:
echo "First iteration"
my_program.exe --parameters
echo "Second iteration"
my_program.exe --parameters
echo "Third iteration"
my_program.exe --parameters
...
I don't want to kill my_program.exe
because it is doing some Pretty Important Stuff. I want the batch script to "break" after my_program.exe has finished. I fear that if I do Ctrl-C, it will kill my_program.exe, which would not be good.
我不想,kill my_program.exe
因为它正在做一些非常重要的事情。我希望批处理脚本在 my_program.exe 完成后“中断”。我担心如果我按 Ctrl-C,它会杀死 my_program.exe,这可不好。
What can I do to either:
我可以做什么:
- Truncate my log file "in flight"
- Kill my batch script whilst leaving its currently executing process unharmed?
- 截断我的日志文件“飞行中”
- 杀死我的批处理脚本,同时保持其当前正在执行的进程不受伤害?
This is a Windows 2003 SP2 server. Help!
这是一个 Windows 2003 SP2 服务器。帮助!
回答by Sergey Podobry
The file is opened with FILE_SHARE_READ, so you cannot delete or truncate it. But you can do the following trick:
该文件使用 FILE_SHARE_READ 打开,因此您无法删除或截断它。但是您可以执行以下技巧:
This will stop cmd.exe from writing to the log file:
这将阻止 cmd.exe 写入日志文件:
- Download process explorer.
- Run it.
- Select Find->Find handle or DLL.
- enter the name of your log file (output.log).
- It has to be found in cmd.exe.
- Select it.
- Look at the lower pane.
- There will be a list of all handles in that cmd.exe.
- Select and close the handle for your log file.
- 下载进程浏览器。
- 运行。
- 选择 Find->Find handle or DLL。
- 输入日志文件的名称 (output.log)。
- 它必须在 cmd.exe 中找到。
- 选择它。
- 查看下方的窗格。
- 该 cmd.exe 中将有一个所有句柄的列表。
- 选择并关闭日志文件的句柄。
The script will go further, the log will be stopped and there will be "The handle is invalid." in the console.
脚本会更进一步,日志会停止,并且会出现“句柄无效”。在控制台中。
回答by Harry Johnston
It's easy enough to kill the batch script but leave the current instance of my_program.exe running. You could kill the relevant cmd.exe process using Task Manager, but the easiest solution is just to rename the batch file.
杀死批处理脚本很容易,但让 my_program.exe 的当前实例继续运行。您可以使用任务管理器终止相关的 cmd.exe 进程,但最简单的解决方案是重命名批处理文件。