This is the expected behavior because when MATLAB has been started without the desktop, it expects to be able to read from standart input ('stdin'), however, when you start it in the background, it cannot read from 'stdin' and the process will be stopped.
As a workaround, you can create a text file which includes the commands you want to execute and use input redirection operator "<" instead of "-r" option. For example, assuming the file "foo.txt" includes the lines
you can execute the command
matlab -nodisplay -nodesktop -nojvm -nosplash <foo.txt >out.txt &
As another workaround, you can write a shell script which automates the process.
Best Answer