getting arguments of matlab when launched in background

1 view (last 30 days)
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

Answers (3)

José-Luis
José-Luis on 18 Dec 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.

pfb
pfb on 18 Dec 2013
José-Luis, thanks for your suggestion.
I had thought of that, and it's actually what I'm doing now, sort of.
Still, it would be useful to me to get the name of the stream into which the stdout is redirected by the nohup command.
I wonder whether I can do that via a system call.
Thanks anyway
Francesco

Walter Roberson
Walter Roberson on 18 Dec 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 Comments
Walter Roberson
Walter Roberson on 19 Dec 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)

pfb
pfb on 19 Dec 2013
Walter,
thanks a lot for your detailed suggestion. It's very interesting.
I get the gist of it, although I'm not sure I grasp the details.
I'm not very proficient in shell scripting.
My only thought here is that perhaps while I'm at it I'll give the script some arguments (script, output, logfile), rather than writing them in it.
I think I can do that.
Anyway, this is not what you (and José-Luis) meant by using a function instead of a script, right?
Thanks again F

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!