form:http://hi.baidu.com/yilinghl/item/f7f395d10555ce866dce3fd0
ssh 远程运行matlab脚本文件的方法:nohup
服务器安装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:
% (1)Call outside self-defined function.
% (2)read input parameters from files.
% (3) write resoult to files.
% (4) put std.output to files.
%-----------------------
%--------------------------------------------------------------------------
% 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; % index of current iteration
ai=0; % input parameter for outside function
bi=0; % input parameter for outside funciion
ar=0; % output parameter for outside funciion
br=0; % output parameter for outside funciion
idp=0; % onother temp var.
%---------
fid=fopen('resoult.txt','w');
%--------------------------------------------------------------------------
% end of parameter setting.
%--------------------------------------------------------------------------
while it<=count
idp=mod(it,npar); % get the
data index in the parameter vector.
if
idp==0
idp=npar;
end
ai=parameter(idp); % set ai and bi.
bi=ai+1;
[ar,br]=reffun(ai,bi); % call outside self-defined
function:reffun.
fprintf(fid,'%-3.1f\t%-3.1f\n',ar,br); % write result to
file.
[it] % std.output.
it=it+1; % next count.
end
%--------------------
fclose(fid);
%--------------------
save parameter.mat parameter it ai bi ar br
idp; % write
var to mat-file.
%--------------------------------------------------------------------------
%**************************************************************************
%***************************** EOF ***********************************
%**************************************************************************
=======================================================================
上述脚本调用的外部函数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)
============================
============================
下面我们再次运行,修改startwork.m的count=2999999;使脚本运行时间增大,足使我们退出登陆,并重新登陆。(写错了,应是-nodisplay)
退出登陆
重新登陆
===================================
另外补充:
(1)如果你想中断正在运行的matlab请使用
kill -9 进程号
或 fg %n;Ctrl+C
(2)如果你想在字符环境的matlab中运行脚本,请使用
run 脚本文件名(不含.m扩展名)
(3)如果你想用ssh运行有X界面的matlab,登陆时请使用
ssh 主机名 -l 用户名 -X
===========
参考文献:
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 matlab
-nodisplay. You could also use matlab
-nodisplay -nojvm, which additionally does not start the JVM.
In both cases, attempts to open plot and similar windows might be
simply ignored or attempts to open the documentation windows might
result in various error messages.
The above is the most restrictive way in which to start Matlab. If
in fact your local machine is Linux, you could
use matlab -nodesktop -nosplash, which
(i) does not start the desktop and (ii) suppresses the splash
screen (the small window with Matlab's logo and version number that
usually pops up briefly). But this way of starting Matlab does
start the JVM and you have access to the HelpBrowser as well as to
plot windows, if you later so desire (and are willing to wait for
those to open, which might take a while depending on your network
connection).
Running
Matlab in the Background (Under Linux)The idea of running a job in
the background means that a software runs without any user
interaction and additionally does not block your login shell. As
remote access, this makes only sense under Unix/Linux. It applies
both on your local machine, but might be particularly useful on a
remote login; as in the previous section, this will be most useful
for purely numerical (i.e., non-graphical) jobs.
nohup
\matlab -nojvm -nodisplay < driver.m >& driver.log
&Here, the backslash in\matlabcircumvents
any possible alias you might have for Matlab (just to be on the
safe side) and the options-nojvm
-nodisplaysuppress
the starting of the JVM (and the startup of the desktop) and the
opening of any windows. The final ampersand ("&") actually puts
the command into the background, i.e., gives you the command-line
prompt in your shell back. Aside from entering any usual commands
now, you could in particular log out, without hanging or killing
your job, because of the standard Unix/Linux
commandnohupbefore
the call to Matlab. The filedriver.mthat
must be present in the current directory is a Matlab script file
(not: function file) that lists the commands to be executed by your
job; if your job is actually performed by a function, this script
file can simply be a single line that calls your function.
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 "<"
redirects stdin (namely from filedriver.m)
into Matlab. For the background run not to hang, there must not be
any screen output, so the ">&" redirects both
stdout and stderr to the
file driver.log; this file must not
exist initially. Again, the redirection commands may be different
in other shells and slight variations are possible (such as
overwriting an existing log file).