the network frequently fails and I would like to monitor the output. I tried to makeshift a command like:

ping www.google.fr | while read pong; do echo "$(date): $pong"; done 1>/dev/null && 2> ~/ping_err.log

but the STDERR is still redirected to STDOUT instead ping_err.log

note: I want only STDERR in the file (not 2>&1)


  • I can't test it right now, but shouldn't it work if you omit the &&? – danzel Jul 5 at 18:40
  • actually, the && was added but the stderr is still on the screen – Brad Thompson Jul 5 at 18:42

Rather than answer your stderr redirection question, I'll suggest a better (IMHO) way:

In https://github.com/waltinator/net-o-matic.git - Watch for (WiFi) network going down, then do a user-specified thing to fix it.

There's a way to monitor your connection:

  ip monitor address | \
    egrep --line-buffered \
    '^Deleted [[:digit:]]+: [[:alnum:]]+[[:space:]]+inet[[:space:]].* scope global ' | \
    while read line ; do

My net-o-matic script also contains a way to ask once:

function netstate () {
    # Return network state as "UP" or "DOWN"
    #Adjust how you decide net is UP/DOWN
    ip link show | egrep -q 'UP,LOWER_UP.* state UP'
    if [[ $? -eq 0 ]] ; then
        echo "UP"
        echo "DOWN"

Using your code, this seems to work:

$ ping www.google.fr 2>&1 > /dev/null | while read pong; do echo "$(date): $pong" ; done 2>&1 > ping_err.log
[no output]
$ cat ping_err.log

vie jul  5 15:56:09 -03 2019: ping: sendmsg: La red es inaccesible
vie jul  5 15:56:10 -03 2019: ping: sendmsg: La red es inaccesible
vie jul  5 15:56:11 -...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.