简体   繁体   English

在Mac OS X上启动Matlab的后台实例

[英]Starting a Background Instance of Matlab on Mac OS X

In Windows you can use the following command in Matlab to start a new instance of MATLAB which will run in the background (ie you can keep executing commands in your first version of MATLAB). 在Windows中,您可以在Matlab中使用以下命令来启动一个新的MATLAB实例,该实例将在后台运行(即,您可以在第一个MATLAB版本中继续执行命令)。

system('matlab &') 系统('matlab&')

An analogous call in OSX, OSX中的类似调用

system([matlabroot '/bin/matlab &']) 系统([matlabroot'/ bin / matlab&'])

however results in the display of the splash image, then nothing. 但是会导致显示初始图像,然后什么也不显示。 If I take out the ampersand, the new instance opens as expected. 如果我取出与号,则新实例将按预期方式打开。 Unfortunately, this won't work for me, I really need to be able to control the first instance of MATLAB while the second is running. 不幸的是,这对我不起作用,我确实需要能够在第二个MATLAB运行时控制它的第一个实例。

Does anyone know why this discrepancy between the operating systems exists? 有谁知道为什么操作系统之间存在这种差异? By the way, I'm using OSX 10.7, Windows 7 64 bit, and MATLAB R2012a on Mac and R2012b on PC. 顺便说一句,我在Mac上使用OSX 10.7,Windows 7 64位,在PC上使用MATLAB R2012a,在PC上使用R2012b。

As some background, I'm trying to write a generic tester for an interactive command line interface that uses the input() function extensively. 作为某些背景知识,我正在尝试为交互式命令行界面编写一个通用的测试器,该界面广泛使用input()函数。

Edit: I should have mentioned that the command 编辑:我应该提到该命令

/Applications/MATLAB_R2012a.app/bin/matlab & /Applications/MATLAB_R2012a.app/bin/matlab和

works as expected from the OSX terminal. 可以从OSX终端正常工作。 In other words, a new instance of MATLAB opens and new commands can be entered into the terminal. 换句话说,将打开MATLAB的新实例,并且可以在终端中输入新命令。 So this problem seems to be specific to the system() function in OSX matlab. 因此,此问题似乎特定于OSX matlab中的system()函数。

Also, I tried adding that command to a bash script and calling the script from matlab, but had the same problem that I did with putting the command into the system() function. 另外,我尝试将该命令添加到bash脚本中并从matlab调用该脚本,但是与将命令放入system()函数时遇到的问题相同。

Thanks 谢谢

This is a long shot, but it might be happening because when you invoke the new instance of Matlab from Matlab with the system() command on Unix or OS X, the matlab_helper process forks and runs a shell process to run the new application. 这是一个长期的尝试,但是可能会发生,因为当您在Unix或OS X上使用system()命令从Matlab调用Matlab的新实例时, matlab_helper进程会派生并运行一个Shell进程来运行新应用程序。 If you omit the ampersand, the shell blocks and waits for the program to finish, and system() waits for it, so the first Matlab locks up. 如果您省略了&符,shell将阻塞并等待程序完成,而system()等待它,因此第一个Matlab会锁定。 And (here's the speculation part) if you add the ampersand, Matlab launches in the background, and then the forked shell exits, which then causes the new Matlab process to exit because its parent process (the shell) has exited. 并且(这是推测部分),如果添加与号,Matlab将在后台启动,然后分叉的shell退出,这将导致新的Matlab进程退出,因为其父进程(shell)已退出。 (Windows doesn't have the same parent/child process relationships, process launch mechanism, or shells, which would explain the different behavior.) (Windows没有相同的父/子进程关系,进程启动机制或外壳程序,这将解释不同的行为。)

You could try prefixing the command with nohup , which protects processes from getting killed by SIGHUP, which might be what's happening here to your second Matlab process. 您可以尝试在命令前面加上nohup ,以防止进程被SIGHUP杀死,这可能是第二个Matlab进程正在发生的情况。

system(['nohup ' matlabroot '/bin/matlab &'])

You could also try using the OS X open command to launch a new independent instance. 您也可以尝试使用OS X open命令启动新的独立实例。 Something like this. 这样的事情。 You may need to fiddle with the options and path, but -n should be what gives you a new instance. 您可能需要弄乱选项和路径,但是-n应该为您提供了一个新实例。 It should be pointing at /Applications/MATLAB_R2012a.app ; 它应该指向/Applications/MATLAB_R2012a.app ; I'm assuming that's what matlabroot is returning on OS X. 我假设这就是matlabroot在OS X上返回的内容。

system(['open -na ' matlabroot])

You could also try running it from the Java process-launching features from within Matlab instead of with system() . 您还可以尝试从Matlab内的Java流程启动功能system()而不是system()运行它。 Runtime.exec() doesn't block like system() does, and there may be other quirks to system() , like the matlab_helper architecture. Runtime.exec()不阻挡像system()那样,并且可以存在其它的怪癖到system()matlab_helper架构。 Try launching it with java.lang.Runtime from Matlab. 尝试从Matlab使用java.lang.Runtime启动它。

jrt = java.lang.Runtime.getRuntime();
newMatlabProcess = jrt.exec([matlabroot '/bin/matlab']);

You can try the other command line variants above using this mechanism too, and you may need to redirect stdout to /dev/null, since the new processes input and output are buffered in to that newMatlabProcess object. 您也可以使用此机制尝试上述其他命令行变体,并且可能需要将stdout重定向到/ dev / null,因为新进程的输入和输出被缓冲到该newMatlabProcess对象中。

You can use applescript to do this. 您可以使用applescript来执行此操作。 I do something like this: 我做这样的事情:

! osascript -e "tell application \"Terminal\" to do script \"cd `pwd`;matlab -nojvm -nosplash -r 'why'\""

This example opens a new Matlab instance, in the current directory, and runs the command "why". 本示例在当前目录中打开一个新的Matlab实例,并运行命令“ why”。 You can remove the "-nojvm" if you need java in your background Matlab process 如果您在后台Matlab进程中需要Java,则可以删除“ -nojvm”

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM