nohup 和 std::ignore() 不能一起工作

nohup and std::ignore() not working together

等待 RETURN 按键的一种方法是调用 std::ignore()。但是,当我使用 nohup std::ignore() 时总是立即 returns。是否存在等待按键的替代方法,或者只是 return 可以与 nohup 一起使用?

当您使用 nohup 到 运行 一个命令时,它会将您的命令的标准输入连接到 /dev/null 因此没有办法等待 any 一种按键。

当您尝试从 /dev/null 读取时,它会自动响应 EOF。

我想这可以使用以下代码(未经测试)进行检查:

#include <unistd.h>
...
if (isatty(fileno(stdin))) ...

这应该有效,因为标准保证 std::cin 与标准输入相关联。

我解决了这个问题:

std::this_thread::sleep_for(std::chrono::system_clock::duration::max());

Does there exist an alternative way to wait for a keypress

来自哪里?

按键必须来自某个地方。您期望从控制终端获取它,然后您故意断开您的进程与该终端的连接

是的,还有其他选择:

  1. 如果您想从那里获取输入,请不要断开与终端的连接
  2. 改为从 X 获取按键事件,假设您有图形环境 运行
  3. 创建一个新终端并从那里获取按键。这可能意味着 运行 实际上是一个 xterm,所以它不是微不足道的。

or just return that will work with nohup?

std::ignore() 已经 做了 return,而你在抱怨这个。等待按键是唯一适合您的方法吗?