Clear Filters
Clear Filters

How to run matlab code in the server background?

105 views (last 30 days)
Jingyu Liu
Jingyu Liu on 27 Feb 2022
Commented: Walter Roberson on 22 Aug 2024 at 4:40
Take the following matlab code for an example. How to run it in the server background? I tried nohup test.m & but failed.
% test.m
A = [
1,2,3;
4,5,6;
7,8,9
];
b = [1;1;1];
x = A*b;
save x;
Any suggestion is appreciated. Thanks!

Answers (2)

Vaibhav
Vaibhav on 4 Oct 2023
Hi Jingyu Liu,
I understand that you want to run the MATLAB code in the background. One workaround is to utilize MATLAB in command-line mode.
Here are the steps to access MATLAB in this mode:
  • Commence MATLAB in command-line mode by entering the following command in the terminal:
matlab -nodesktop
  • This initiates MATLAB without the graphical user interface (GUI).
  • With MATLAB running in command-line mode, you can execute your MATLAB code by typing commands or running a MATLAB script or function.To run a script, such as "test.m," simply enter the script's name and press enter. It is possible to access multiple "MATLAB command windows" concurrently.
To Execute a MATLAB script with "nohup" feature, first, open a terminal on your Linux machine. second, navigate to the directory where MATLAB script file is located and execute the below command in the terminal:
nohup matlab -nodisplay -nosplash -r "run('test.m'); exit" > output.log 2>&1 &
You can refer to below blog to know more about '-nodesktop' feature:
You can refer to below MATLAB answer to know more about “nohup” feature:
Hope this helps!
Regards,
Vaibhav
  3 Comments
Yigithan Mehmet Kose
Yigithan Mehmet Kose on 16 Jan 2024
Edited: Yigithan Mehmet Kose on 16 Jan 2024
Hello, when I run this, I get the following message:
[1] 69286
What does that mean?
Walter Roberson
Walter Roberson on 22 Aug 2024 at 4:40
You likely need to provide the path to MATLAB, such as
nohup /Applications/MATLAB_R2024a/bin/matlab -nodisplay -nosplash -r "run('test.m'); exit" > output.log 2>&1 &
The
[1] 69286
indicates that what was started was background job number [1] and that the process ID was 69286

Sign in to comment.


SHAIKH
SHAIKH on 22 Aug 2024 at 4:00
Edited: Walter Roberson on 22 Aug 2024 at 4:37
% Define the START and STOP inputs
START = 1; % Set START to 1 for ON, 0 for OFF
STOP = 0; % Set STOP to 1 for ON, 0 for OFF
% Determine the output based on the START and STOP inputs
% Output should be ON when START is ON and STOP is OFF
Output = START && ~STOP;
% Display the result
disp(['START Input: ', num2str(START)]);
disp(['STOP Input: ', num2str(STOP)]);
disp(['Output: ', num2str(Output)]);
  1 Comment
Walter Roberson
Walter Roberson on 22 Aug 2024 at 4:37
I do not understand how this solves the problem of running MATLAB code in the background ?

Sign in to comment.

Categories

Find more on Startup and Shutdown in Help Center and File Exchange

Tags

Community Treasure Hunt

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

Start Hunting!