【matlab】【linux】ssh 远程运行matlab脚本文件的方法:nohup

分类: MATLAB |
服务器安装matlab软件。
ssh能正常运行。
实现功能:
1. 字符环境下运行matlab.
2.字符环境下运行matlab脚本文件,脚本文件无图像的屏幕输出。
3.脚本文件需长时间运行,运行结果输出到文件。matlab的标准重定向到另一个文件。
4.客户端退出登陆后,matlab保持运行,直到脚本程序结束或错误退出时,matlab程序才退出。
实现方法。
采用nohup命令。
具体实现过程:
使用命令:
nohupmatlab -nojvm -nodisplay -nosplash-nodesktop < matlabscript.m 1>running.log 2>running.err &
或
nohup matlab -nojvm -nodisplay -nosplash < matlabscript.m 1>running.log 2>running.err &
或:
nohup matlab -nojvm -nodisplay < matlabscript.m 1>running.log 2>running.err &
其中:
matlabscript.m是将要运行的脚本文件,存在于服务器上;
running.log是matlab标准屏幕输出的重定向文件,将建立于服务器上;
running.err是matlab运行时的错误重定向文件,将建立于服务器上。
%-------------------
例子:
存在于~/matlabwork/project/下的一个matlab脚本文件:
文件名为:startwork.m
代码=============================
%-----------------------
% This is a script run as a main funtion
% Code by yiling
% Date:2009.02.28
% Email: yilinghl@gmail.com; yilinghl@yahoo.cn; yilingr0@yahoo.cn
%-----------------------
% The goal of this script is to test performent on the remote server.
% This script will be still running even the user logout except that
% matlab being kill by message 9 or the script run to accomplish(return).
%
% The script contain the fuction:
%
%
%
%
%-----------------------
%--------------------------------------------------------------------------
% clear workspace.
close all;
clear;
clc;
%-----------------------
% add outside self-defined function path.
addpath ~/matlabwork/scriptlib;
%-----------------------
% import the input parameters for file inputpar.txt which is formated by
% ASCII code. Parameters is arranged in a row vector sized [1,npar].
fid=fopen('inputpar.txt','r');
parameter=fscanf(fid,'%g',[1,inf]);
fclose(fid);
clear fid;
[mpar,npar]=size(parameter);
clear mpar;
%-----------------------
% main work start
% we do a simple loop thing
%---------
% total count
count=199;
it=1;
ai=0;
bi=0;
ar=0;
br=0;
idp=0;
%---------
fid=fopen('resoult.txt','w');
%--------------------------------------------------------------------------
% end of parameter setting.
%--------------------------------------------------------------------------
while it<=count
end
%--------------------
fclose(fid);
%--------------------
save parameter.mat parameter it ai bi ar br idp;
%--------------------------------------------------------------------------
%**************************************************************************
%*****************************
%**************************************************************************
=======================================================================
上述脚本调用的外部函数reffun.m,存于~/matlabwork/scriptlib/下。
function [ar,br]=reffun(ai,br)
% this is a outside standart function
% coded by yiling
ar=ai;
br=br+1;
end
========================================================================
另外startwork.m脚本需要请取一个文体格式的参数文件inputpar.txt, 与脚本存在于同一目录~/matlabwork/project/
内容为:
1
2 3
4 6 7
8
9
0 11.5
========================================================
运行:
文件存放结构:
不离线的运行情况:
(写错了,应是-nodisplay)
http://s4/mw690/6fb8aa0d4dfde309d68d3&690远程运行matlab脚本文件的方法:nohup" TITLE="【matlab】【linux】ssh
============================
============================
下面我们再次运行,修改startwork.m的count=2999999;使脚本运行时间增大,足使我们退出登陆,并重新登陆。(写错了,应是-nodisplay)
退出登陆
重新登陆
===================================
另外补充:
(1)如果你想中断正在运行的matlab请使用
(2)如果你想在字符环境的matlab中运行脚本,请使用
(3)如果你想用ssh运行有X界面的matlab,登陆时请使用
===========
参考文献:
http://www.math.umbc.edu/~gobbert/matlab.html
Running Matlab Remotely (Under Linux)Usually, it is most effective and convenient to run Matlab locally, in particular for interactive work, but there are a number of reasons why you might want to run Matlab remotely across a network. For instance, your local machine may not have Matlab installed at all, or your local machine may not have sufficient memory for your task. In these cases, you can consider accessing another computer by remote login and running Matlab there. This concept only makes sense under Unix/Linux on the remote machine; Windows or Macintosh do not allow for remote access (as far as I know). Hence, for any new windows to be opened, also your local system needs to be Unix/Linux. However, the performance of such remote windows may be so slow that you will want to avoid them anyway and rather run Matlab purely inside your login shell; naturally, this precludes any graphical output and restricts this use to numerical tasks only.
So, we want to start Matlab (i) without its desktop and (ii)
without any graphics (including the splash screen); this can be
accomplished by saying
The above is the most restrictive way in which to start Matlab. If
in fact your local machine is Linux, you could
use
For the remaining elements in the command-line above, I am assuming
you are using the csh or tcsh shell; in other shells, the
redirections may be slightly different. The "<</kbd>"
redirects stdin (namely from file