getting arguments of matlab when launched in background
2 次查看(过去 30 天)
显示 更早的评论
Hi everybody,
I often run matlab in background (under linux and mac OS), using a command like
nohup matlab -nodesktop -nodisplay -nosplash < script.m &> output.txt &
For several reasons I'd like to access the name of the output file (here 'output.txt') from inside the script (here 'script.m'). I found out that I can access the PID of the job through the undocumented function 'feature', but so far I did not find a way to get the name of the stream where the stdout is redirected. Is that possible at all?
Thanks a lot for your assistance Francesco
0 个评论
回答(3 个)
José-Luis
2013-12-18
A workaround that might make your life less complicated: make your script into a function and pass the output name as an argument to this function.
0 个评论
Walter Roberson
2013-12-18
You can use code to fstat() standard output (file id 1) in order to find the device and inode number associated with the output. You can use code to ftw() to find a name associated with that inode. But a file can have multiple "real" names in OS-X and Linux. If you are not lucky enough that the link count is exactly 1, then you have to find all of the names and figure out which is the "right" one for the circumstances.
Remembering that output.txt might have been a symbolic link to a completely different name: do you want the name "output.txt" or do you want the name linked to?
You can get the process ID (PID). You can system() of 'ps' to get the public command line associated with the process. Unfortunately, when that public command line is being created, the shell has already stripped out all I/O redirection and $ constructs, so the output name just isn't there. The operating system does not know what it is, as it is the shell that works it all out, does fopen() on the output file, does the appropriate fclose() and fdup() to get the file descriptors in place, and then does a fork() and exec() or popen(), What the operating system gets is at best the command line arguments passed to the routine; redirection has been removed from those as individual programs are not expected to have to handle redirection themselves. The shell might still have a copy of the command line (maybe), but you would have to root around in the shell memory to find it.
Thus, José-Luis's answer is the correct one: if you need to know the name of the output file, pass it in as an argument.
4 个评论
Walter Roberson
2013-12-19
cat > start_script <<EOF #!/bin/sh scriptname="script" logfile="$HOME/${scriptname}.log"
thispid=$$ thisfile="output${thispid}.txt" thisdir=$pwd
nohup matlab -nodesktop -nodisplay -nosplash -r "try cd $thisdir;$scriptname;catch ME; end;quit" &> $thisfile & echo "$(date) started $thispid to $pwd/$thisfile" >> $logfile EOF
chmod +x start_script
Now after that you can just
./start_script
to start it running, with the information being appended to a log file (in your home directory)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Interactive Control and Callbacks 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!