linux下後台運行MATLAB

來源:互聯網
上載者:User

後台運行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 &

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

Tags Index: