【问题标题】:handling system(command) call timeout in Linux [closed]在Linux中处理系统(命令)调用超时[关闭]
【发布时间】:2016-06-29 09:45:54
【问题描述】:

我正在尝试使用 linux 的系统 (cmd) 执行命令(从远程服务器获取密钥)。我的问题是当远程服务器无法访问时,对于少数 IP,它只是等待而不返回,这反过来又使“system()”永远挂起。我想处理这种情况。我正在考虑一种方法让我的 system() 等待一段时间,如果命令没有返回,那么 system() 命令必须报告一个不成功的状态。

我的命令看起来像这样:

int status = system("<<<URL of the remote server>>>");
//the above command must wait for a fixed duration before coming out if no response.

【问题讨论】:

  • 您可以使用 cURL insted 使用系统从服务器获取数据,因为它提供了设置超时的选项

标签: c linux shell signals fork


【解决方案1】:

您可以使用timeout(1) 命令来执行此操作...

int status = system("timeout 60 whatever-command-you-want-to-run");
if(status != 0) {
    // Uh, oh! Either something went wrong, or the command time out after 60 seconds
}

但是,正如 cmets 中所提到的,您最好使用面向网络的解决方案来解决您的情况中相当潜在的问题,例如 requests。

【讨论】:

  • 当然有一个unix程序可以做到这一点……
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-10-10
  • 2014-03-03
  • 1970-01-01
  • 2021-07-27
相关资源
最近更新 更多