Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Non zero exit code for lsof

Tags:

unix

lsof

I am using following lsof command:

lsof -iTCP -a -sTCP:^LISTEN -a -p <pid>

If there is some output this command returns an exit code of 0 but if there is no output the exit code is 1.

echo $?

Can anyone explain why is this happening? Is there a way to avoid it?

like image 487
learner Avatar asked Apr 24 '15 08:04

learner


1 Answers

This is the expected behaviour: you just get 0 if something was found.

From man lsof:

DIAGNOSTICS

Errors are identified with messages on the standard error file.

Lsof returns a one (1) if any error was detected, including the failure to locate command names, file names, Internet addresses or files, login names, NFS files, PIDs, PGIDs, or UIDs it was asked to list. If the -V option is specified, lsof will indicate the search items it failed to list.

It returns a zero (0) if no errors were detected and if it was able to list some information about all the specified search arguments.

like image 143
fedorqui 'SO stop harming' Avatar answered Oct 04 '22 00:10

fedorqui 'SO stop harming'