Search code examples
bash

How to completely hide nohup messages?


I try to completely hide nohup messages from my terminal. My nohup usage looks like this:

if eval 'nohup grep "'$arg1' '$arg2' [A-Za-z]\+" /tmp/dict.txt >& /dev/null &'; then

but in console I get one nohup message in Polish:

nohup: zignorowanie wejścia i przekierowanie standardowego błędu do standardowego wyjścia

which means "ignoring standard input redirecting standard error to standard output"

Is there any chance to hide every nohup message?


Solution

  • The trick I use is to run nohup in a sub-shell and redirect the standard error output of the sub-shell to /dev/null:

    if (nohup grep "'$arg1' '$arg2' [A-Za-z]\+" /tmp/dict.txt & ) 2>/dev/null
    then : …whatever…
    else : …never executed…
    fi
    

    However, the exit status of that command is 0, even if the grep fails to do anything (mine wrote grep: /tmp/dict.txt: No such file or directory in nohup.out, for example), but the exit status was still 0, success. The one disadvantage of the notation used here is that the job is run by the sub-shell, so the main process cannot wait for it to complete. You can work around that with:

    (exec nohup grep "'$arg1' '$arg2' [A-Za-z]\+" /tmp/dict.txt ) 2>/dev/null &
    

    This gives you the job control information at an interactive terminal; it won't in a non-interactive script, of course. You could also place the & outside the sub-shell and after the error redirection in the first command line, without the exec, and the result is essentially the same.