How to run Matlab script with parameters using "nohup" to run in background, detached from the terminal (Redhat)

I have a script that has a function which take in 3 positional arguments. If I use
matlab -nodisplay -nojvm
to get the matlab terminal and then use below inside the MatLab terminal
myscript . dataset_name False
I could make it run.
But this is problematic as this is a long running program in a remote server, and I cannot guarantee a stable internet connection to keep the tunnel open. Therefore I want to
  1. Run this as a single command in Linux terminal itself
  2. Detach the process from the terminal, and run in background, prefereably similar to how we do when we run a Python script in using the terminal. Which is:
nohup python myscript.py > experiment.log 2>&1 &
So to do this first I need to address the point 1, and then 2. So far I have tried out the below answer but it didn't work for me:
matlab -nodisplay -nosplash -nojvm -nodesktop -r "myscript . dataset_name False"
Gives: Not enough input arguments. Error in myscript. "fprintf('Base directory: %s\n',base_dir);"
Here base_dir is the first argument. I have tried out putting "'" around the arguments but didn't work.

Respuestas (1)

Scripts do not accept any input parameters, so you must be using a misleadingly named function.
Try
matlab -nodisplay -nosplash -nojvm -nodesktop -r "myscript('.','dataset_name', 'False')"

6 comentarios

Thank you for the support. However, I have tried this version as well, but that gave me:
.../bin/matlab: eval: line 1780: syntax error near unexpected token `)'
.../bin/matlab: eval: line 1780: `exec ".../bin/glnxa64/MATLAB" -nodisplay -nosplash -r 'myscript('"'"'.'"'"','"'"'dataset_name'"'"',' 'False') -nodesktop -nojvm'
Also the amount of apostrophes in the error looks a bit weird to me.
To handle the above I have tried this StackOverflow answer and this MatLab community answer, but did not work for me.
To give bit more infromation, I am running the script from the root directory where the myscript.m is. As you inferred, the function is named as same as the script and that is why I could pass the arguments.
You seem to have switched to ' quoting instead of " quoting, and got the quoting wrong for the 'False' . If you follow the pattern you were using you would have needed
".../bin/glnxa64/MATLAB" -nodisplay -nosplash -r 'myscript('"'"'.'"'"','"'"'dataset_name'"'"',' '"'"'False'"'"') -nodesktop -nojvm'
Sorry I think I didn't quite understand. Did you mean I have switched to ' quoting instead of " quoting inside the script?
The answer I posted was to use
matlab -nodisplay -nosplash -nojvm -nodesktop -r "myscript('.','dataset_name', 'False')"
which uses " " quotes around the myscript() call, and uses ' ' quoting of individual strings.
The error message you posted was
.../bin/matlab: eval: line 1780: `exec ".../bin/glnxa64/MATLAB" -nodisplay -nosplash -r 'myscript('"'"'.'"'"','"'"'dataset_name'"'"',' 'False') -nodesktop -nojvm'
which shows that ' quoting was used, together with elaborate schemes to introduce ' quotes.
Create a script that contains
fiber_analysis_pipeline('.', 'RT05', 'False');
now
system("nohup matlab -nodisplay -nosplash -nojvm -nodesktop -r 'NameOfThatScript' 2>&1 &")

Iniciar sesión para comentar.

Categorías

Más información sobre Startup and Shutdown en Centro de ayuda y File Exchange.

Productos

Versión

R2024a

Preguntada:

el 4 de Abr. de 2025

Comentada:

el 7 de Abr. de 2025

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by