user1095108
user1095108

Reputation: 14603

nohup and std::ignore() not working together

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

Answers (3)

IanM_Matrix1
IanM_Matrix1

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

Useless
Useless

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:

  1. don't disconnect from the terminal if you want to get input from there
  2. get your keypress event from X instead, assuming you have a graphical environment running
  3. create a new terminal and get your keypress from there. This probably means running an xterm in practise though, so it's not trivial.

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

user1095108
user1095108

Reputation: 14603

i solved the problem with:

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

Upvotes: 0

Related Questions