getting arguments of matlab when launched in background
1 vue (au cours des 30 derniers jours)
Afficher commentaires plus anciens
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 commentaires
Réponses (3)
José-Luis
le 18 Déc 2013
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 commentaires
Walter Roberson
le 18 Déc 2013
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 commentaires
Walter Roberson
le 19 Déc 2013
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)
Voir également
Catégories
En savoir plus sur Startup and Shutdown dans Help Center et File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!