I am running a Python script that is meant to launch a .bat file on Windows 7. The batch file runs a sort of Monte Carlo program for 2 minutes. At the end of those 2 minutes the program must update a data file that I will read later in the same Python script, do some data analysis on the output and relaunch the same .bat file if necessary.
I do this process an unlimited amount of time until I get a satisfactory output.
The Python code looks like this
import os
import numpy as np
os.system('myBatch.bat > messages.txt')
while True:
# force flushing
fd = open("output.txt",'a')
fd.flush()
os.fsync(fd.fileno())
fd.close()
# read data
data = np.loadtxt("output.txt")
# some data analysis ...
# testing
if dataSatisfactory:
break
else:
os.system('myBatch.bat > messages.txt')
As you can see I am trying to force flushing the output.txt file that must be written by the program in the batch file. As I have no control over this compiled executable program the output file is not getting updated after the command os.system('myBatch.bat > messages.txt'), but only after I kill the whole python process.
I tried to add the 4 lines after # force flushing, but apparently still not working.
For the sake of clarification, the input of the executable is the output.txt as well as the output, so the executable must over-write the file output.txt.
The file messages.txt is to redirect all the messages of the executable from the stdout to messages.txt.
Any advice or a hint?
NB: The batch file looks like this
.\TheExecutable %output.txt
if your batch file does nothing but calling your monte carlo program, get rid of it and call the program directly from python: os.system("TheExecutable.exe output.txt")
If you can make your program write to standard output instead of a file, you could use subprocess.check_output("TheExecutable.exe") which just returns that standard output. See the documentation of numpy.loadtxt() for functions loading from string instead of a filename.
This way, you completely avoid writing & reading to/from disk.
Related
In a batch script redirecting the output to a file like so
set "output=C:\output.txt"
echo Blah blah blah. >> %output%
Is it required that the file is closed after the redirection of writing stuff to it is completed (similar to the approach in other programming languages)?
I have tried searching for related information online but could not find something on it; I assume the fact that most scripts are closed after they finish their tasks (commands) is maybe the reason why.
But if say a script is to run in an endless loop where a different output file is written (e.g. by appending the time to the output file name) or if new output is constantly being redirected to the same output file, could the "not closing of the file" potentially lead to problems, memory or other?
No, you don't have to close any file handles in batch scripts. You don't know the file handle value so you could not even close it if you wanted to.
On Windows, all open kernel handles are closed when a process ends/crashes but since a batch file is interpreted by cmd.exe without starting a new cmd.exe process in most cases, it cannot take advantage of the automatic handle cleanup and will manually close the file handle after each redirected operation.
Currently, I'm working with a finite element software called DIANA, I've been always capable of running "simple" models with MATLAB executing a batch file. Nevertheless, now I'm starting to use more "complex" models (just more robust, with more elements), but the procedure is exactly the same to the one that I've been using before. The problem is that now I can't run the model from MATLAB, it apparently start to run it, but stops suddenly. I'm using the next commands:
dos('path\filename.bat')
or
system(fullfile(path,'filename.bat' ))
Both lines execute the .bat file. I thought that I had something wrong with the files, so I reviewed them but apparently there's nothing wrong with them. Then I just made double click on the .bat file, and the procedure runs without problems. Then I realized that the problem wasn't the files, it was the execution from MATLAB. Now that you know the context of the problem, can you giving me some advice in order to avoid that MATLAB "kills" the process internally?
The trace of execution showed in MATLAB:
diana: working directory is *CURRENT DIRECTORY NAME*
diana: input file is *FILENAME*.dat
diana: command file is *FILENAME*.dcf
diana: output file is *FILENAME*.out
diana: filos file is diana.ff
DIANA JOB 8028 finished
The trace execution making directly double click in the batch file is the following:
In the remarked files showed in the previous figure, are contained the output of the model, while executing directly from MATLAB, those don't appear, because MATLAB finish the process before.
Thanks in advance for any answer!
Pd: I'm using a computer with the next characteristics: Windows7, 12 Gb RAM, OS 64bits, Processor Intel Xeon 2.53 GHz.
What you are running is not strictly equivalent to a double click on the .bat file, specially because current directory is different, and if the batch file needs to access to auxiliary files (relative path), then the files won't be found.
Try adding this in your batch file as the first command (or right after echo off)
Cd /d %~dp0
Here is the solution provided by Diana support team:
It seems that MATLAB sets some environment symbols that limit the
memory usage per thread.
When you start Diana from Matlab via a batch file that resets the
symbols all works fine.
Such a batch file may look like:
rem === Diana Environment Setup ===
call "C:\Program Files\Diana 10.0\dialogin.bat"
rem Unset the symbols added by MATLAB
set KMP_STACKSIZE=
set KMP_HANDLE_SIGNALS=
set KMP_BLOCKTIME=
rem start the diana job
diana test2
I have a command line program that produces output that I would like to log to a file.
I know that I can redirect the output of a command (foo.exe for example) to a file using the > operator, but for this current project I need more than just simple output functionality. I would also mention that I can't change any of the code in foo.exe.
I am looking for a command line utility/command that can do the following:
Log the output of a foo.exe to a file
Place a timestamp on each individual output of foo.exe
When the current log file reaches a certain specified size (say 1mb), close the current file and start putting log data in a new file.
In a perfect world the utility would encapsulate the command I wish to execute and log its output, looking something like the following:
log.exe -MaxLogSize=1mb -FileName="FooOutput.log" -Execute="foo.exe"
Any help or suggestions are appreciated!
I am trying to run a solver (CBC) through the CMD interface.
The following line works and executes properly when I put the input mps file into the same directory as the solver:
CMD>> "solver_path\solver.exe" "input.mps"
However, I want to run the same code as above but give the solver a pathname for the input.txt file. i.e.
CMD>> "solver_path\solver.exe" "C:/.../input.mps"
In the above intance the solver is not able to open the file, I do not understand why this is happening as
CMD>>"C:/.../input.mps"
opens the file so it definitely exists and can be opened.
Any advice would be much appreciated.
Short story: need a method to get the write-status of a file on a server (using BASH) from a client (using CMD batch).
Long-time lurker, first-time poster. I did many searches on variations of what I'm looking for and have not yet found enough data.
I am writing a batch file in CMD (because the clients could be any WinOS [XP - up] with unknown packages installed). The batch uses puTTY's "plink" to connect via SSH to the server. Once connected to the server, plink executes a command to write data to a new file.
Once that file is written, I use PSCP to copy the file to the client
So far, so good; I have successfully accomplished all of this.
The creation of that file is instantaneous but the time it takes to write all of the data is unknown / variable. Therefore I need an automated method to determine when the file is complete, to then copy it. Simply using timeout/sleep for XX seconds is not feasible in my circumstances.
The approach I have taken so far (as of yet unsuccessfully) is to repeatedly grab the filesize using "stat -c '%s' filemane" and run that in a loop until grab1 EQU grab2, indicating a complete file. I am finding this difficult because I can't get the output of stat into the CMD batch to process it.
Q1: Is this (stat result going into CMD for loop) the best approach? Maybe there's something existing in BASH?
Q2: if Q1 is true, any ideas on how to get the stat result into the CMD batch as a variable to parse/analyze the data?
Thanks in advance for suggestions and your time.
DCT
Have the command writing the file write it with a temp filename. So if it will be called xyz.txt, have it written with filename tmpxyz.txt.tmp, then the final step will be a rename.
That way you can just check for the presence of the named file.
Usually a good idea to give the file a unique name, probably incorporating the date and time, I find.