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.

Further reading

Read more in the tech topic.

Let's talk!

I'm Carlo Nicolini — I am interested on the reliability of AI reasoning systems (interpretability, inference-time methods, probabilistic language programming) and on quantitative portfolio optimization (I am a maintainer of skfolio). If you're working on something in these areas and think we might collaborate, chat, discuss, I'm happy to talk about it!

The best way to reach me is on via DM on LinkedIn.