EDA365欢迎您登录!
您需要 登录 才可以下载或查看,没有帐号?注册
x
ssh 远程运行matlab脚本文件的方法:nohup/ F. ]6 z9 b+ c u1 Z8 j
. l4 K2 j* C" R3 h; e8 u服务器安装matlab软件。' ~! w" s2 b+ z% B$ ^
ssh能正常运行。* y5 G0 M+ d( h0 y0 Q% M% k1 e0 I
7 g5 O. p4 x0 Z0 E$ C, r0 P# m" E实现功能:- h R+ d4 M L/ |
1. 字符环境下运行matlab.: Y7 l/ |- \3 D8 \. p( g' ^
2.字符环境下运行matlab脚本文件,脚本文件无图像的屏幕输出。5 k6 } |$ z2 A) R7 t
3.脚本文件需长时间运行,运行结果输出到文件。matlab的标准重定向到另一个文件。
& [& F( b* z1 L3 b% K3 V4.客户端退出登陆后,matlab保持运行,直到脚本程序结束或错误退出时,matlab程序才退出。- S" m4 f8 D$ T, n5 h! O* q
' S% B1 [1 m7 }* I/ I j# b- ?- Q实现方法。# t, e0 _1 D' D# }" V5 ]4 u
采用nohup命令。# }, T: N& u' O3 i+ j6 ~
7 l$ b0 I* d/ \! L- [9 Y' ~" o# I具体实现过程:
: _) ~# Z" F8 |! a* F; J. i1 V! g$ K- W! [$ t' S8 i' G
使用命令:
6 \& c6 E( \3 _ R/ ?8 e# Anohupmatlab -nojvm -nodisplay -nosplash-nodesktop < matlabscript.m 1>running.log 2>running.err &
& J! C3 ]- r% ^+ Y K0 \) n或
2 m$ S2 Z0 ~( W7 F' T$ dnohup matlab -nojvm -nodisplay -nosplash < matlabscript.m 1>running.log 2>running.err &
( r# C( E; G% z0 h或:
* N, M: m; o% Z/ p. D `nohup matlab -nojvm -nodisplay < matlabscript.m 1>running.log 2>running.err &% c2 Y6 y4 M; X9 S( W5 f
. S) j+ U2 }* b: A* a& U6 A L |& |
其中:
+ W/ V1 P3 G7 o+ f2 hmatlabscript.m是将要运行的脚本文件,存在于服务器上;
3 _' ]! N# {: L$ f9 Orunning.log是matlab标准屏幕输出的重定向文件,将建立于服务器上;
+ y- W. D5 R1 {3 t5 Z0 g9 H& k U8 }running.err是matlab运行时的错误重定向文件,将建立于服务器上。
9 }$ a0 f9 @" B3 X# k4 S1 f; O( ^) J! R9 @
%-------------------; _) U0 G! F& Q+ H: G4 q% D
例子:
: J! p1 H7 l5 T m9 A& X存在于~/matlabwork/project/下的一个matlab脚本文件:
8 m2 H- M* O) @文件名为:startwork.m1 O. i/ J# \* s8 ?/ g
代码=============================
/ m+ t$ q4 ^4 b9 M+ h$ H%-----------------------
: H6 T0 O- K F( b8 |% This is a script run as a main funtion0 `% n! ~. q) S
% Code by yiling
" k1 a" {& `' n: j, i) K% Date:2009.02.285 y3 {0 \) {" y
% Email: yilinghl@gmail.com; yilinghl@yahoo.cn; yilingr0@yahoo.cn$ p8 }" {* y+ s9 I" ~
%-----------------------
6 F0 ~1 x1 @/ [& @* o# `% The goal of this script is to test peRForment on the remote server.3 E! s; B8 D3 @: b- E/ O8 P
% This script will be still running even the user logout except that
* }' V. e3 t! R+ ]: w% matlab being kill by message 9 or the script run to accomplish(return).
* \0 k7 x% z, @/ G0 W* {. p%$ E% h7 X: [' r# h K
% The script contain the fuction: A0 M6 ]; { L
% (1)Call outside self-defined function.
( n7 Z4 ~# ?& {; m0 @% (2)read input parameters from files.
/ h: Z8 ]/ ~, u, p$ T% (3) write resoult to files.
9 v5 x6 Q2 Z: m( |1 E# ~( U% (4) put std.output to files.8 P3 K5 ]% D1 A
%-----------------------1 w& ]+ B7 i0 f' h4 H* r, V* O- _
%--------------------------------------------------------------------------) X+ a6 J; |8 r$ o
% clear workspace.
6 M4 x$ u. G7 m) f* f3 `2 p5 G5 cclose all;
8 p/ ^1 X8 P1 S+ C) _clear;, M# h6 w2 j# ~3 Y1 i$ S3 T
clc;
" Z7 B- L1 ~+ w) F%-----------------------
7 I+ K# u8 y2 _: G% add outside self-defined function path.
% k# K6 e5 U- F* s" I; c" A vaddpath ~/matlabwork/scriptlib;
, U) M" Y5 z7 K" q/ s%-----------------------
D- [: n2 Z& U- H. _% import the input parameters for file inputpar.txt which is formated by
, X+ g4 ^/ M0 ?1 P* y; E- [0 e! }% ASCII code. Parameters is arranged in a row vector sized [1,npar].% n4 z3 j% Q7 O8 a1 v0 E. S8 S
fid=fopen('inputpar.txt','r');
* M# N3 r, t1 L, `0 y/ o4 Z) Zparameter=fscanf(fid,'%g',[1,inf]);8 J! q; W6 G+ x: n% V+ G2 K6 o7 w
fclose(fid);; N$ W. D$ Z& v# o+ Z- d6 A4 D
clear fid;
& H4 d8 \. I! i[mpar,npar]=size(parameter);* B6 T3 M4 T1 V S3 y
clear mpar;
) q7 y! [9 \$ S- J0 b%-----------------------
: |) g/ }0 P" t' e0 T( a4 Y! Y9 g% main work start& ]( d1 ^% _. ]1 [4 E. u0 z
% we do a simple loop thing
: _6 X9 U1 }8 N3 f%---------
& R+ a6 _/ N1 c# L, c# G0 b" ]% total count
& M' t5 g( r$ u- V8 b7 i# t+ J0 Dcount=199;, ]! m3 [) K- U3 I) S0 `. Z/ T
it=1; % index of current iteration
" T) _* Q. J; V: {% H! |ai=0; % input parameter for outside function
- @; o3 E. b4 E$ G3 X' p5 @2 y, Cbi=0; % input parameter for outside funciion
- h4 J5 j. G# b: G ^6 H8 Oar=0; % output parameter for outside funciion
8 D5 Q% V* z9 h1 K9 c/ ]br=0; % output parameter for outside funciion
. m# s) n" p# I3 [, o8 ?idp=0; % onother temp var.
# Q) q n. G7 I9 g# d J%---------
1 F. w/ D q( B4 n- [fid=fopen('resoult.txt','w');: f/ m2 M, \$ N" [, c
%--------------------------------------------------------------------------
0 \) @' J; a9 n! g; M- U# _/ {( C% end of parameter setting.
- E. W3 u: ~" ^* k% m+ y) g%--------------------------------------------------------------------------2 M" P% p7 o' i( \" E2 e2 \1 p
while it<=count
, E3 w8 p; l7 N5 D$ ]0 X$ e$ Z& t" e idp=mod(it,npar); % get the data index in the parameter vector.
$ {- n: r! ~/ i! z1 v if idp==0
8 l' i5 w$ z4 U8 P8 k. V% t& {5 z idp=npar;
$ ]6 V# L* L; A6 Q [' @. O# @/ p end: h# h+ Y1 w0 l6 X
ai=parameter(idp); % set ai and bi.
5 T0 R/ k3 R( Z1 g( \ bi=ai+1;
2 x$ k+ r; l4 h- o. M [ar,br]=reffun(ai,bi); % call outside self-defined function:reffun.
( I& z0 v& n6 X* s3 N2 a4 T8 ^ fprintf(fid,'%-3.1ft%-3.1fn',ar,br); % write result to file.
. _) H0 M: U+ E& }, E3 e [it] % std.output.- t8 n" @; e, l( j4 |3 J
it=it+1; % next count.& V$ \" |1 P1 A1 M
end( s8 m, }0 R9 q' k+ K# U" z) v
%--------------------! b9 F- B. M5 ~% X1 d
fclose(fid);+ W5 b% x5 G& ?% y7 R0 @' r7 O
%--------------------* Y6 G2 P% W: q1 L i- f$ E
save parameter.mat parameter it ai bi ar br idp; % write var to mat-file.8 o7 u7 D2 e! f8 _' e$ @
%--------------------------------------------------------------------------; ^) ~% n2 l6 d/ \3 m8 ~
%**************************************************************************
0 t7 n- W& J; O%***************************** EOF ***********************************/ v4 [7 h" Z6 q0 ]3 E' Z/ }$ E
%**************************************************************************
7 f& s7 y3 I5 s9 O5 O" L1 }! P9 e7 C=======================================================================! t6 v0 N$ c4 {( X# n) A8 ], D
上述脚本调用的外部函数reffun.m,存于~/matlabwork/scriptlib/下。! A, Q( O& e' i! x$ c& o
function [ar,br]=reffun(ai,br)
4 z t! C) X5 j* H, f6 i% this is a outside standart function5 b0 H* l7 H" m8 U% U) d
% coded by yiling1 X- P* V _! p$ f+ d8 ^1 I
ar=ai;% y$ A, @) @) q! E9 G8 c
br=br+1;& D9 e+ }( E I3 y6 u' a. A
end4 A& s; H4 E$ t4 g. G
/ D- Q) l& |) I! B* E" Z! E
========================================================================0 |2 y/ ^9 t G% n! w
另外startwork.m脚本需要请取一个文体格式的参数文件inputpar.txt, 与脚本存在于同一目录~/matlabwork/project// K# I) n5 b6 \- r6 y& D+ D3 P
内容为:; n% o, _4 U, H
1
! z' q0 P2 S/ B% T3 y2 3
, f, M d0 i# u' x8 j8 H4 L. C4 6 75 m5 t$ N4 r2 ]
8
) v; e& f4 y, g( `6 o9, E, H9 O; k/ T e* G
0 11.51 h5 o0 q) P2 z \: U. Y6 \
, J- h, e+ C' p2 y% {7 J: S
========================================================
0 v: G6 V9 \3 D( V
- D) o; e( o" r8 R2 ^! ^5 `% U运行:" B6 n1 ~+ q- e& i
文件存放结构:
' B( W( t6 R- c' u) L不离线的运行情况:) s/ z4 _1 @6 r3 w- ~* h; q+ e& m9 M2 c
(写错了,应是-nodisplay)
* o. m: I& w) w/ q$ [. A* @7 Y, x2 B( X' Y! o" v) P* g
) O+ U% S: c2 Z6 I
- w+ A* t! a/ i7 Y: s+ l============================
4 I% @# W8 G1 A============================
& t/ _, l$ H. Q Q下面我们再次运行,修改startwork.m的count=2999999;使脚本运行时间增大,足使我们退出登陆,并重新登陆。(写错了,应是-nodisplay)) }/ i) \0 D, j' e! ~
( {6 S2 y# }. A% ~* K! k
- H, z3 |; R, w2 M- w2 N
退出登陆
) b9 D. X& E$ Q" Q6 o6 `3 J
9 d+ S4 L- H) w
重新登陆- R# P- T+ h3 R% X
; m, [" E R4 m; J/ w===================================+ ^% l6 v+ {6 u% z
另外补充:0 H5 V7 x" q, b+ P
(1)如果你想中断正在运行的matlab请使用
, n" y/ f+ }8 a: f" r kill -9 进程号" K" {8 z5 f8 x
或 fg %n;Ctrl+C7 @! ?' {. W* M
(2)如果你想在字符环境的matlab中运行脚本,请使用
m1 }4 G6 z# w0 m' t- p! T run 脚本文件名(不含.m扩展名)( ^3 g3 w3 M8 b8 k0 l
(3)如果你想用ssh运行有X界面的matlab,登陆时请使用/ G1 V/ @4 b+ H x3 p" t& C
ssh 主机名 -l 用户名 -X: S+ b5 y( E6 n9 x/ t. k9 v
9 D/ E5 k6 e9 ]) H5 `5 y===========
3 R, x- l% P5 a/ ~0 ]/ o' g/ b; f参考文献:
! H- U) ~9 h w; L( A2 mhttp://www.math.umbc.edu/~gobbert/matlab.html+ n8 \$ j0 o( B& d g
7 b1 l) i* U( H% b) q; ?' b5 P: F# V
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 matlab circumvents any possible alias you might have for Matlab (just to be on the safe side) and the options -nojvm -nodisplay suppress 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 command nohup before the call to Matlab. The file driver.m that 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 "<</kbd>" redirects stdin (namely from file driver.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). & A& U9 y; W- j3 H f/ Q' j
g# c" Z0 ?: e1 Y7 B) b" X* S
! y. I, P) {# a2 V! S! b0 b) x: n; K. k/ [& H" l( g/ z
: J* w2 b" e: I3 [# X; O- E& Y
|