通过Matlab系统()连续运行多个远程命令



我想使用matlab系统调用连续执行多个命令。我首先想ssh到一台远程机器上,然后在该机器上运行一个程序。程序启动后,我想在该程序的控制台中输入另一个命令。以下是我想在代码中做的事情:

system('ssh othermachine')
system('command on other machine')
%%WAIT FOR PROGRAM TO START RUNNING ON OTHER MACHINE
system('run command on other machine')

问题是Matlab将挂起第一个系统调用,并且在退出第一个系统的进程之前不会进行下一个系统调用。有办法绕过这个吗?谢谢你的帮助!

序言:您的问题是一般性的,而不仅仅与matlab有关。

当您想通过ssh运行远程命令时,必须在ssh调用中发出这些命令。在(linux(shell中,您将拥有

$ ssh remotemachine command1

对于单个命令。因此,使用matlab system调用,您将获得

>> system('ssh remotemachine command1').

当您希望多个命令按顺序执行时,您可以在shell中编写

$ ssh remotemachine "command1; command2"

也就是说,在matlab中,你会写一些类似的东西

>> system('ssh remotemachine "command1; command2"').

一般来说,在shell脚本(比如script.sh(中对命令进行分组,并在ssh调用中进行管道传输更为优雅

$ cat script.sh | ssh remotemachine

在matlab外壳中,听起来像

>> system('cat script.sh | ssh remotemachine').

您可以添加许多标志来指定您想要的行为(例如,在会话分离/后台执行、输出收集方面……请参见此处(。

最新更新