I am trying to make a watchdog bash script which has to check if the port is open, based on the exit status, otherwise should start the daemon. Problem is I can't manage to avoid the script outputting any information by redirecting STDOUT and STDERR.
nc -zv 1.2.3.4 55 | grep " open " >/dev/null 2>&1
or
nc -zv 1.2.3.4 55 | grep " open " 2>&1 >/dev/null
or
nc -zv 1.2.3.4 55 | grep " open " &>/dev/null
returns anyway
'FQDN_hostname_or_domainname [1.2.3.4] 55 (?) open'
Nevertheless, this combo works with other commands, such as netstat. Is it something about netcat, or maybe about bash syntax? Please let me know what I am getting wrong.
(nc -zv 1.2.3.4 55 2>/dev/null) | grep ..., but that would be very surprising. What exact command are you running now? – terdon Jun 21 '16 at 14:20netcat -zvw1 1.2.3.4 55 | grep "open"– Ricardo Beschieru Jun 22 '16 at 06:39nc -zv 1.2.3.4 55 2>&1 | grep openwork? – terdon Jun 22 '16 at 07:15ncandnetcat. My bad. Still is a mystery why output fromnetcatdoesn't get piped to grep. – Ricardo Beschieru Jun 22 '16 at 09:49netcat -zvw1 1.2.3.4 55 2>&1 | grep "open". The thing is that some output is printed to stderr and some to stdout. If you just runcommand | grep foo, only stout is passed togrep. If you also want to grep stderr, you need2>&1or&|. – terdon Jun 22 '16 at 09:53netcat -zvw1 1.2.3.4 55 2>&1 | grep "open"does work, sonc -zvw1 1.2.3.4 55 2>&1 | grep "open". Turns out there is no difference betweeen nc and netcat in this case. Thanks! – Ricardo Beschieru Jun 22 '16 at 10:48