Search code examples
bashcentos7pidfuser

fuser command in centos print an unwanted output in bash script


I'm using centOS 7, and I'm trying to check what is the pid of a process using port X/tcp with "fuser" command, through a bash script, like so:

  some_pid=$(fuser X/tcp) > /dev/null
  another_pid=$(fuser Y/tcp) > /dev/null
  ...
  if [[ -z "$some_pid" ]]; then ...

Although I don't want anything to be printed to the console, the results are:

X/tcp:
Y/tcp:
...

When I try to run fuser with -s, then the if statment returns false, because the result of the fuser is empty, for some reason.

How can I avoid the unwanted printing? Why when I run fuser in silent mode it returns empty?


Solution

  • In the fuser man page you can see

    fuser outputs only the PIDs to stdout, everything else is sent to stderr.

    That is you need to get rid of the stderr output.

    some_pid=$( fuser X/tcp 2> /dev/null )