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