Reputation: 19372
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
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
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:
The script will go further, the log will be stopped and there will be "The handle is invalid." in the console.
Upvotes: 4