← Back to writing

July 16, 2016 · 2 min read

Engineering

Launching-matlab-in-background-correctly-in-linux

Launching-matlab-in-background-correctly-in-linux.

You want to launch Matlab for a very long script and then disconnect your remote terminal, and when back, not finding bad surprise.

Suppose your script myeigenvalues.m is:

A=randn(1000);
X=eig(A);
save
  1. Create a helper script helper_myegeinvalues.m that launches myeigenvalues.m script but surrounded by a try-catch block like this:
try
    myeigenvalues;
catch ME
    disp(ME);
    disp('SCRIPT CRASHED');
    exit;
end
disp('SCRIPT RUNNED SUCCESSFULLY');

The try-catch block is needed to avoid leaving Matlab open in idle status if an error occurred once launched as background process.

  1. Create another helper bash-script file helper_myegeinvalues.sh that runs the script helper_myegeinvalues.m
matlab -nodisplay -nodesktop -nosplash -r "helper_myegeinvalues"
  1. Make the helper_myegeinvalues.sh file executable by issuing
chmod +x helper_myegeinvalues.sh
  1. Run the helper_myegeinvalues.sh as a nohup command and collect all the output into the file stdout.txt
nohup ./helper_myegeinvalues.sh > stdout.txt &
  1. You can now leave the process run and stay happy.

Continue exploring

All articles ↗