进程运行时日志文件为空?

发布于 2024-12-04 12:48:56 字数 843 浏览 1 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

回眸一遍 2024-12-11 12:48:56

该文件是使用 FILE_SHARE_READ 打开的,因此您无法删除或截断它。但您可以执行以下技巧:

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

  1. 下载 进程资源管理器
  2. 运行它。
  3. 选择“查找”->“查找句柄或 DLL”。
  4. 输入日志文件的名称 (output.log)。
  5. 必须在 cmd.exe 中找到它。
  6. 选择它。
  7. 查看下面的窗格。
  8. 该 cmd.exe 中将有一个所有句柄的列表。
  9. 选择并关闭日志文件的句柄。

脚本会更进一步,日志会停止,并且会出现“句柄无效”。在控制台中。

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:

  1. Download process explorer.
  2. Run it.
  3. Select Find->Find handle or DLL.
  4. enter the name of your log file (output.log).
  5. It has to be found in cmd.exe.
  6. Select it.
  7. Look at the lower pane.
  8. There will be a list of all handles in that cmd.exe.
  9. Select and close the handle for your log file.

The script will go further, the log will be stopped and there will be "The handle is invalid." in the console.

时光清浅 2024-12-11 12:48:56

终止批处理脚本但保持 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文