进程运行时日志文件为空?
今天我运行了一个批处理脚本。实际上,它仍在运行,所以我希望在为时已晚之前找到解决方案。
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
...
我不想杀死 my_program.exe
因为它正在做一些非常重要的事情。我希望批处理脚本在 my_program.exe 完成后“中断”。我担心如果我执行 Ctrl-C,它会杀死 my_program.exe,这不太好。
我可以做什么:
- “运行中”截断我的日志文件
- 杀死我的批处理脚本,同时保持其当前正在执行的进程不受损害?
这是Windows 2003 SP2 服务器。帮助!
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?
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.
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!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
该文件是使用 FILE_SHARE_READ 打开的,因此您无法删除或截断它。但您可以执行以下技巧:
这将阻止 cmd.exe 写入日志文件:
脚本会更进一步,日志会停止,并且会出现“句柄无效”。在控制台中。
The file is opened with FILE_SHARE_READ, so you cannot delete or truncate it. But you can do the following trick:
This will stop cmd.exe from writing to the log file:
The script will go further, the log will be stopped and there will be "The handle is invalid." in the console.
终止批处理脚本但保持 my_program.exe 的当前实例运行很容易。您可以使用任务管理器终止相关的 cmd.exe 进程,但最简单的解决方案就是重命名批处理文件。
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.