poundifdef
poundifdef

Reputation: 19372

Empty log file while process is running?

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:

This is a Windows 2003 SP2 server. Help!

Upvotes: 1

Views: 6171

Answers (2)

Harry Johnston
Harry Johnston

Reputation: 36328

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.

Upvotes: 0

Sergey Podobry
Sergey Podobry

Reputation: 7189

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.

Upvotes: 4

Related Questions