nohup won't finish my MATLAB script - matlab

I have a Matlab script that I run with nohup when I ssh to a server. For some reason, it looks like the programs runs "flawlessly" but when I check the output file it's blank, and none of the files it was supposed to generate are created. So it seems as if MATLAB goes inside the function that I want to run, and then quits sporadically. Yet it quits with a [+DONE] flag which is very strange, since it really isn't done, if anything, I don't think it actually started...
I don't get the same problem if I open up MATLAB and run the same function on the GUI while I'm ssh'ed.
This is what I put in the terminal:
nohup matlab -nojvm -nodisplay -nosplash <function_CV.m >CV_file.txt </dev/null &
I added the "< /dev/null"
because my nohup was getting weird with a looped 'Bad file descriptor error' for a pointless instruction such as magic(3);exit;. So I'm not sure if maybe my nohup version is buggy?
Update: I've decided to use screen instead. I'd still appreciate any workarounds to this problem.

Indeed it is very strange. The only solution I could came up with would be to replace:
nohup matlab -nojvm -nodisplay -nosplash <function_CV.m >CV_file.txt </dev/null &
with:
nohup matlab -nojvm -nodisplay -nosplash function_CV.M > /dev/null 2>&1 & echo $! > CV_file.txt
I tried it with some own .m file and it worked, so either it should work for you, either the problem is in your function_CV.m.
Hope this helps!

Related

return Matlab Command Window to cmd

I would like to create a Jenkins job which would trigger Matlab function. It is easy!
C:\Program Files\MATLAB\R2012a\bin\matlab.exe -nodisplay -r "disp('Hello World!');exit"
The question is how to return the matlab command window.
It is of course cool to start some process, but I need to track it.
Some of you say probably “.log”. I know, but I want to see it alive in Jenkins console.
Is there a way to achieve it?
Have you tried calling Matlab with the option -logfile output.log? That should dump the whole Command Window Output into the logfile output.log. See also this link to the documentation.
If waiting the matlab process to finish is fine with you, then just add the -wait -log switches:
"%PATH_TO_MATLAB_BIN%\matlab.exe" -nodisplay -r "disp('Hello World!');exit" -wait -log

Run MATLAB script on cmd without opening the GUI window or the icon [duplicate]

I am running a MATLAB script from the Windows command prompt:
"C:\Program Files\MATLAB\R2014B\bin\matlab" -nodisplay -nosplash -nodesktop -wait -r "test.m"
The test.m is simple:
function test
disp('Hello!');
The output is displayed in the Matlab Command Window. Is there any way how I can force output to the windows prompt?
Use the command line option -log when you call Matlab from the command line (or any other shell or batch (e.g. cmd or bat) script).
It isn't documented as of Matlab 2017b, but it works.
Side note: -nodisplay isn't supported in the Windows version of Matlab, but if you want to prevent it from displaying figures, use -noFigureWindows instead.
Since R2019b, there is a new command line option, -batch, which redirects the output to the command line and handles other stuff for batch processing. See the documentation for Windows.
matlab -batch "statement to run"
This starts MATLAB without the desktop or splash screen, logs all output to stdout and stderr, exits automatically when the statement completes, and provides an exit code reporting success or error.
I don't know of a way to do it in Windows to get Matlab to actually run in the DOS window which is what you would need for the display to be written in it. (FYI: You can in LINUX - but I assume you need to run in Windows).
For running in Matlab you have 2 alternatives that I can think of:
-logfile FILE on launch which will record all output to a FILE you specify - however how and when the file is written to disk is controlled by Matlab and I haven't tested to see - if your code doesn't do much it might only be written on Matlab exit.
diary FILE in your Matlab command, i.e. -r "diary FILE.TXT; test.m; diary OFF" - this is similar to above - but uses the diary function.
However you can get what you want if you can run your code compiled (I know thats a big if as you may not have the compiler - or if you regularly want to update test.m this is not the most efficient...
When you run a compiled code from a DOS prompt all the terminal messages are written to the DOS prompt. One thing I'd advise if this is suitable is to delete the "splash.png" file from your installation directory to avoid the splash screen displaying when you run from the DOS as its (probably) not needed.
I have found a solution at:
https://www.mathworks.com/matlabcentral/answers/91607-how-can-i-redirect-the-command-window-output-to-stdout-and-stderr-when-running-matlab-7-8-r2009a-i#answer_100958
I will replicate it here for convenience.
First I need to modify the matlab script to output to a text file:
function test
fid=fopen('output.txt','w');
fprintf(fid,'Hello!');
fclose(fid);
Then I should run the Matlab using a bat file with one additional command to display the contents of the output.txt:
"C:\Program Files\MATLAB\R2014B\bin\matlab" -nodisplay -nosplash -nodesktop -wait -r "test.m"
type output.txt
The type command will display the contents of 'output.txt' in the command window. So answer from #matlabgui was almost there. Thank you.
It is not a very elegant solution, but it works.

Force Matlab output to command line

I am running a MATLAB script from the Windows command prompt:
"C:\Program Files\MATLAB\R2014B\bin\matlab" -nodisplay -nosplash -nodesktop -wait -r "test.m"
The test.m is simple:
function test
disp('Hello!');
The output is displayed in the Matlab Command Window. Is there any way how I can force output to the windows prompt?
Use the command line option -log when you call Matlab from the command line (or any other shell or batch (e.g. cmd or bat) script).
It isn't documented as of Matlab 2017b, but it works.
Side note: -nodisplay isn't supported in the Windows version of Matlab, but if you want to prevent it from displaying figures, use -noFigureWindows instead.
Since R2019b, there is a new command line option, -batch, which redirects the output to the command line and handles other stuff for batch processing. See the documentation for Windows.
matlab -batch "statement to run"
This starts MATLAB without the desktop or splash screen, logs all output to stdout and stderr, exits automatically when the statement completes, and provides an exit code reporting success or error.
I don't know of a way to do it in Windows to get Matlab to actually run in the DOS window which is what you would need for the display to be written in it. (FYI: You can in LINUX - but I assume you need to run in Windows).
For running in Matlab you have 2 alternatives that I can think of:
-logfile FILE on launch which will record all output to a FILE you specify - however how and when the file is written to disk is controlled by Matlab and I haven't tested to see - if your code doesn't do much it might only be written on Matlab exit.
diary FILE in your Matlab command, i.e. -r "diary FILE.TXT; test.m; diary OFF" - this is similar to above - but uses the diary function.
However you can get what you want if you can run your code compiled (I know thats a big if as you may not have the compiler - or if you regularly want to update test.m this is not the most efficient...
When you run a compiled code from a DOS prompt all the terminal messages are written to the DOS prompt. One thing I'd advise if this is suitable is to delete the "splash.png" file from your installation directory to avoid the splash screen displaying when you run from the DOS as its (probably) not needed.
I have found a solution at:
https://www.mathworks.com/matlabcentral/answers/91607-how-can-i-redirect-the-command-window-output-to-stdout-and-stderr-when-running-matlab-7-8-r2009a-i#answer_100958
I will replicate it here for convenience.
First I need to modify the matlab script to output to a text file:
function test
fid=fopen('output.txt','w');
fprintf(fid,'Hello!');
fclose(fid);
Then I should run the Matlab using a bat file with one additional command to display the contents of the output.txt:
"C:\Program Files\MATLAB\R2014B\bin\matlab" -nodisplay -nosplash -nodesktop -wait -r "test.m"
type output.txt
The type command will display the contents of 'output.txt' in the command window. So answer from #matlabgui was almost there. Thank you.
It is not a very elegant solution, but it works.

How to open a matlab program using windows command prompt? (in a single window) [duplicate]

Suppose that;
I have an m-file at location:
C:\M1\M2\M3\mfile.m
And exe file of the matlab is at this location:
C:\E1\E2\E3\matlab.exe
I want to run this m-file with Matlab, from command-line, for example inside a .bat file. How can I do this, is there a way to do it?
A command like this runs the m-file successfully:
"C:\<a long path here>\matlab.exe" -nodisplay -nosplash -nodesktop -r "run('C:\<a long path here>\mfile.m'); exit;"
I think that one important point that was not mentioned in the previous answers is that, if not explicitly indicated, the matlab interpreter will remain open.
Therefore, to the answer of #hkBattousai I will add the exit command:
"C:\<a long path here>\matlab.exe" -nodisplay -nosplash -nodesktop -r "run('C:\<a long path here>\mfile.m');exit;"
Here is what I would use instead, to gracefully handle errors from the script:
"C:\<a long path here>\matlab.exe" -nodisplay -nosplash -nodesktop -r "try, run('C:\<a long path here>\mfile.m'), catch, exit, end, exit"
If you want more verbosity:
"C:\<a long path here>\matlab.exe" -nodisplay -nosplash -nodesktop -r "try, run('C:\<a long path here>\mfile.m'), catch me, fprintf('%s / %s\n',me.identifier,me.message), end, exit"
I found the original reference here. Since original link is now gone, here is the link to an alternate newreader still alive today:
exit matlab when running batch m file
On Linux you can do the same and you can actually send back to the shell a custom error code, like the following:
#!/bin/bash
matlab -nodisplay -nojvm -nosplash -nodesktop -r \
"try, run('/foo/bar/my_script.m'), catch, exit(1), end, exit(0);"
echo "matlab exit code: $?"
it prints matlab exit code: 1 if the script throws an exception, matlab exit code: 0 otherwise.
Here are the steps:
Start the command line.
Enter the folder containing the .m file with cd C:\M1\M2\M3
Run the following: C:\E1\E2\E3\matlab.exe -r mfile
Windows systems will use your current folder as the location for MATLAB to search for .m files, and the -r option tries to start the given .m file as soon as startup occurs.
Since R2019b, there is a new command line option, -batch. It replaces -r, which is no longer recommended. It also unifies the syntax across platforms. See for example the documentation for Windows, for the other platforms the description is identical.
matlab -batch "statement to run"
This starts MATLAB without the desktop or splash screen, logs all output to stdout and stderr, exits automatically when the statement completes, and provides an exit code reporting success or error.
It is thus no longer necessary to use try/catch around the code to run, and it is no longer necessary to add an exit statement.
cat 1.m | matlab -nodesktop -nosplash
And I use Ubuntu
Thanks to malat. Your comment helped me.
But I want to add my try-catch block, as I found the MExeption method getReport() that returns the whole error message and prints it to the matlab console.
Additionally I printed the filename as this compilation is part of a batch script that calls matlab.
try
some_code
...
catch message
display(['ERROR in file: ' message.stack.file])
display(['ERROR: ' getReport(message)])
end;
For a false model name passed to legacy code generation method, the output would look like:
ERROR in file: C:\..\..\..
ERROR: Undefined function or variable 'modelname'.
Error in sub-m-file (line 63)
legacy_code( 'slblock_generate', specs, modelname);
Error in m-file (line 11)
sub-m-file
Error in run (line 63)
evalin('caller', [script ';']);
Finally, to display the output at the windows command prompt window, just log the matlab console to a file with -logfile logfile.txt (use additionally -wait) and call the batch command type logfile.txt
I run this command within a bash script, in particular to submit SGE jobs and batch process things:
/Path_to_matlab -nodisplay -nosplash -nodesktop < m_file.m
Since none of the answers has information about feeding input argument, it is important to
add it here. After some research, I found this link
Feeding the arguments is very similar to how we run a Matlab function.
matlab -r 'try myfunction(argument1,argument2); catch; end; quit'
If you are somehow getting an argument from bash/terminal, you simply need to insert that into the bash command as:
matlab -r 'try myfunction($MY_BASH_ARG,argument2); catch; end; quit'
(This is after a couple of trial and error)

Matlab: Running an m-file from command-line

Suppose that;
I have an m-file at location:
C:\M1\M2\M3\mfile.m
And exe file of the matlab is at this location:
C:\E1\E2\E3\matlab.exe
I want to run this m-file with Matlab, from command-line, for example inside a .bat file. How can I do this, is there a way to do it?
A command like this runs the m-file successfully:
"C:\<a long path here>\matlab.exe" -nodisplay -nosplash -nodesktop -r "run('C:\<a long path here>\mfile.m'); exit;"
I think that one important point that was not mentioned in the previous answers is that, if not explicitly indicated, the matlab interpreter will remain open.
Therefore, to the answer of #hkBattousai I will add the exit command:
"C:\<a long path here>\matlab.exe" -nodisplay -nosplash -nodesktop -r "run('C:\<a long path here>\mfile.m');exit;"
Here is what I would use instead, to gracefully handle errors from the script:
"C:\<a long path here>\matlab.exe" -nodisplay -nosplash -nodesktop -r "try, run('C:\<a long path here>\mfile.m'), catch, exit, end, exit"
If you want more verbosity:
"C:\<a long path here>\matlab.exe" -nodisplay -nosplash -nodesktop -r "try, run('C:\<a long path here>\mfile.m'), catch me, fprintf('%s / %s\n',me.identifier,me.message), end, exit"
I found the original reference here. Since original link is now gone, here is the link to an alternate newreader still alive today:
exit matlab when running batch m file
On Linux you can do the same and you can actually send back to the shell a custom error code, like the following:
#!/bin/bash
matlab -nodisplay -nojvm -nosplash -nodesktop -r \
"try, run('/foo/bar/my_script.m'), catch, exit(1), end, exit(0);"
echo "matlab exit code: $?"
it prints matlab exit code: 1 if the script throws an exception, matlab exit code: 0 otherwise.
Here are the steps:
Start the command line.
Enter the folder containing the .m file with cd C:\M1\M2\M3
Run the following: C:\E1\E2\E3\matlab.exe -r mfile
Windows systems will use your current folder as the location for MATLAB to search for .m files, and the -r option tries to start the given .m file as soon as startup occurs.
Since R2019b, there is a new command line option, -batch. It replaces -r, which is no longer recommended. It also unifies the syntax across platforms. See for example the documentation for Windows, for the other platforms the description is identical.
matlab -batch "statement to run"
This starts MATLAB without the desktop or splash screen, logs all output to stdout and stderr, exits automatically when the statement completes, and provides an exit code reporting success or error.
It is thus no longer necessary to use try/catch around the code to run, and it is no longer necessary to add an exit statement.
cat 1.m | matlab -nodesktop -nosplash
And I use Ubuntu
Thanks to malat. Your comment helped me.
But I want to add my try-catch block, as I found the MExeption method getReport() that returns the whole error message and prints it to the matlab console.
Additionally I printed the filename as this compilation is part of a batch script that calls matlab.
try
some_code
...
catch message
display(['ERROR in file: ' message.stack.file])
display(['ERROR: ' getReport(message)])
end;
For a false model name passed to legacy code generation method, the output would look like:
ERROR in file: C:\..\..\..
ERROR: Undefined function or variable 'modelname'.
Error in sub-m-file (line 63)
legacy_code( 'slblock_generate', specs, modelname);
Error in m-file (line 11)
sub-m-file
Error in run (line 63)
evalin('caller', [script ';']);
Finally, to display the output at the windows command prompt window, just log the matlab console to a file with -logfile logfile.txt (use additionally -wait) and call the batch command type logfile.txt
I run this command within a bash script, in particular to submit SGE jobs and batch process things:
/Path_to_matlab -nodisplay -nosplash -nodesktop < m_file.m
Since none of the answers has information about feeding input argument, it is important to
add it here. After some research, I found this link
Feeding the arguments is very similar to how we run a Matlab function.
matlab -r 'try myfunction(argument1,argument2); catch; end; quit'
If you are somehow getting an argument from bash/terminal, you simply need to insert that into the bash command as:
matlab -r 'try myfunction($MY_BASH_ARG,argument2); catch; end; quit'
(This is after a couple of trial and error)