i want to detach command either with 'command &' in the end, or with 'nohup command &', but it stops right after detaching it.
command is little specific, if it receives eof on input, it breaks so /dev/null as input will lead to end and solution that usualy works:
$ command < /dev/null > /dev/null 2>&1 &
not working…
is there other device in unix/linux, that can replace /dev/null and behave like empty input, but not sending eof.
(by the way, command is very useful multicasting tool emcast, I can try patching it my self, or find patched version for this purpose… but it seems that problem can be solved outside)
I'm adding this EDIT to make my question more clear. I made this C program of 2 lines that works perfectly: program name is "donothing"
#include <unistd.h>
int main() { while (1) { sleep(10); } return 0; }
and it is what I am looking for, some device/program, that doing nothing, but leave its stdout open. Both ("command & … disown" and "nohup command &") works.
$ donothing | mycommand >/dev/null &
$ disown %1
works well, so now question is only: what unix device/program behaves like my 'donothing'.
Best Answer
For your command to detect
eof
, it has to read from stdin. So presumably it is expecting some input. So it sounds like what you need is not an empty input (/dev/null
is exactly meant for that), but input that never comes.It can be simulated with a pipe where nobody is ever going to write on the other end like:
Or to avoid having to run that extra
sleep
command, it could be done with a named pipe:Here using an intermediary file descriptor to work around the fact that the shell connects stdin to
/dev/null
implicitely when you start a command with&
(unless you add an explicit stdin redirection like our<&3
here).On Linux (and probably on Linux only), you can also do:
/dev/fd/1
where fd 1 is connected to a pipe, on Linux, behaves like a named pipe. That is, when you open it in read mode, you get the reading end of the pipe.So above, fd 0 will be connected to the reading end of a pipe whose other end is on the fd 3 of
the-command
. Becausethe-command
is not going to write anything on its fd 3, anyread
attempt on fd 0 will block (or a non-blocking read will return with there's nothing to read yet, or a select/poll will return nothing to read either asthe-command
is probably doing if it's doing anything else than waiting for input that never comes).