现在的位置: 首页 > 综合 > 正文

linux下后台运行MATLAB

2013年09月13日 ⁄ 综合 ⁄ 共 5472字 ⁄ 字号 评论关闭

后台运行matlab脚本文件的方法:nohup

实现功能:
1. 字符环境下运行matlab.
2.字符环境下运行matlab脚本文件,
脚本文件无图像的屏幕输出。

3.脚本文件需长时间运行,运行结果输出到文件。matlab的标准重定向到另一个文件。

4.客户端退出登陆后,matlab保持运行
,直到脚本程序结束或错误退出时,matlab程序才退出。

实现方法:
采用nohup命令。


具体实现过程:

使用命令:
nohup /usr/matlab/bin/matlab   <fill.m> fill.out &
就可以了,fill.m表示你的文件名. fill.out代表你的输出转到fill.out文件中。比如,matlab中的命令sin(1:10),结果应该输出到command window中的,但是现在转到了fill.out文件中

例如:我在/home/jsh/yangting/文件夹下有个test2.m文件

那么,当matlab在/home/jsh/yangting/文件夹下打开时,

命令为

nohup matlab <test2.m> fill.out &

注意:在nohup运行成功后,不能直接点击关闭按钮,而应该按下任意键,恢复到输入状态,然后按exit命令退出。

使用命令:
nohup
matlab -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运行时的错误重定向文件,将建立于服务器上。

===================================
另外补充:
(1)如果你想中断正在运行的matlab请使用
              kill -9 进程号


         或 fg %n

; Ctrl+C

(2)如果你想在字符环境的matlab中运行脚本,请使用
              run 脚本文件名(不含.m扩展名)

(3)如果你想用ssh运行有X界面的matlab,登陆时请使用
             ssh 主机名 -l 用户名 -X

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 "<
" 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).

无论是linux unix,用 nohup sh 'full command line' 都可以启动程序并让程序在后台运行,独立于终端。

由于nohup启动的时候不会有图形界面,如果是matlab,建议不要用图形界面选项,执行什么命令预先写在m文件里面,如:

nohup sh 'matlab -nodesktop < /data/user/myCommand.m'

这里的<是输入换向,因为nohup会忽略终端的输入。


要注意,如果是ssh上去的,千万不要直接关窗口,而一定要用exit命令和主机解除关系。


用不用"&"在linux里面不重要。在linux里面,还有更加简便的方法。并且还有图形界面

1)可以用screen命令,一般的2.6.12以上的内核都支持。
screen matlab &
这个时候,可以继续用matlab,退出终端用exit就可以了。
如果没有 &。可以到终端,按ctrl-a 挂起,用命令bg将matlab转到后台,注意,这个时候不要忘记最重要的一步,千万人按一键或多按一个回车,然后用exit。要不然,一旦你结束sreen,matlab也就结束了。

2)基于2.6.18以上内核
起先自己一直这么用的,所以也就没有上心。以为linux原本就可以独立终端运行。刚才有看了一下,发现是2.6.16以后的新功能,就是2.6.18以后更加完善了。就是VM机制。当然,需要多核计算机支持。

个终端在缺省时将试图用VM运行你的程序。ssh
name@server以后,可以直接在linux主机上打开matlab,然后可以看到图形界面,你可以完成你要得工作,需要等待的时候。在终端,按
ctrl-z回到终端命令行,用bg命令置于后台。然后,用exit退出终端。这个时候,你会发现matlab还在你的电脑上运行。不过这个只是图形界
面。并且没有实质性的作用。就是x winserver在你的电脑上的一个 VM界面而已。

另外一点,你的tty输出将存在主机的你的目录下。matlab的屏幕输出就看不见了。所以,务必将结果用标准输出,写入文件。

3)matlab
server.
这个针对性比较强,你的电脑需要安装matlabserver支持相关的工具箱,然后可以让server上的其他电脑替你完成你的运算。server的特
殊命令集在工具箱里都有定义,一般的运算和你在本地没有区别。关于server的问题,如果感兴趣很高兴再讨论。

 

 

例子 如下

 

/usr/bin/nohup /usr/local/matlab/bin/matlab -c /root/matlab_for_linux/crack/lic_standalone.dat -nodisplay < /usr/local/matlab/work/music_recommend_code_ver4.0/test.m 1> running.log 2>running.err &

抱歉!评论已关闭.