getting arguments of matlab when launched in background

조회 수: 1 (최근 30일)
pfb
pfb 2013년 12월 18일
댓글: pfb 2013년 12월 19일
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

답변 (3개)

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

pfb
pfb 2013년 12월 18일
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 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
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)

pfb
pfb 2013년 12월 19일
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

댓글을 달려면 로그인하십시오.

카테고리

Help CenterFile Exchange에서 Startup and Shutdown에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by