Reputation: 14603
One way to wait for a RETURN keypress is to call std::ignore(). However, when I use nohup std::ignore() always returns immediately. Does there exist an alternative way to wait for a keypress, or just return that will work with nohup?
Upvotes: 0
Views: 170
Reputation: 1594
When you use nohup
to run a command, it connects the standard input of your command to /dev/null so there's no way to wait for any kind of keypress.
When you attempt a read from /dev/null, it automatically responds with EOF.
I guess this could be checked using the following code (untested):
#include <unistd.h>
...
if (isatty(fileno(stdin))) ...
This should work, as the standard guarantees that std::cin
is associated with stdin.
Upvotes: 3
Reputation: 67782
Does there exist an alternative way to wait for a keypress
Where from?
The keypress has to come from somewhere. You're expecting to get it from the controlling terminal, and then you deliberately disconnected your process from that terminal.
Yes, there exist alternatives:
or just return that will work with nohup?
std::ignore()
already does return, and you're complaining about that. Is waiting for a keypress the only thing that will work for you?
Upvotes: 1
Reputation: 14603
i solved the problem with:
std::this_thread::sleep_for(std::chrono::system_clock::duration::max());
Upvotes: 0