【问题标题】:Calling matlab with timeout function from a shell script从 shell 脚本调用带有超时函数的 matlab
【发布时间】:2019-10-02 17:01:43
【问题描述】:

我有一个 matlab 代码管道,其中一个脚本在某些情况下会无限期挂起。目前,我有一个 shell 脚本来调用管道的每一部分。但是,我想“超时”一段可能无限期挂起的 matlab 代码。

如果直接在终端窗口中键入以下代码,则可以完美运行。但是,当此代码是 shell 脚本中的一行时,它不会执行 matlab 脚本。

timeout --kill-after=25 25 matlab -nodisplay -nosplash -nodesktop -r "run('testing.m'); exit;"

从 shell 脚本执行时,在达到 timeout 和 kill 选项之前不会发生任何事情。还有,

matlab -nodisplay -nosplash -nodesktop -r "run('testing.m'); exit;"

在 shell 脚本中运行没有问题。

【问题讨论】:

  • 考虑使用 '--foreground': 'timeout --foreground --kill-after =10 25 matlab ...' 如果 testing.m 需要与终端交互,它可能会有所帮助。
  • 另请注意,kill-after 超时通常比实际超时小得多 - 如果 TERM 信号不会导致进程被杀死,这提供了一个安全网。
  • 成功了,谢谢!也感谢您的建议。
  • 很高兴为您提供帮助,欢迎来到 Stack Overflow。如果此答案或任何其他答案解决了您的问题,请将其标记为已接受。

标签: linux bash matlab shell timeout


【解决方案1】:

根据上面的评论,添加 --foreground 解决问题。

很可能,“testing.m”需要访问 TTY/TTY 信号。默认情况下,超时会将命令作为子进程运行,w

timeout --foreground --kill-after=10 25 matlab -nodisplay -nosplash -nodesktop -r "run('testing.m'); exit;"

来自超时手册页: --foreground when not running timeout directly from a shell prompt, allow COMMAND to read from the TTY and get TTY signals; in this mode, children of COMMAND will not be timed out

【讨论】:

    猜你喜欢
    • 2011-09-30
    • 2012-01-23
    • 2016-08-27
    • 2018-12-27
    • 2012-04-14
    • 2012-03-08
    • 2016-03-13
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多