How to check whether a socket is listening or not?
You can get some information by trying to connect, pass nothing and accept nothing before disconnecting.
socat -u OPEN:/dev/null UNIX-CONNECT:/run/php/php7.0-fpm.sock
There are at least four possible outcomes:
If the socket does not exist then the error will be
No such file or directory
and the exit status will be1
.If you have no write access to the socket then the error will be
Permission denied
and the exit status will be1
. In this case you cannot tell if there's a process listening.If you have write access to the socket and there is no listening process then the error will be
Connection refused
and the exit status will be1
.If you have write access to the socket and there is a process listening then the connection will be established. The command will send nothing (like
cat /dev/null
), it will not try to receive anything (because of-u
), so it will exit almost immediately. The exit status will be0
.The connection gets established, briefly but still. The listening process may be configured to accept just one connection, serve it and exit; or to accept one connection at a time. In such case the probing connection will saturate the limit; this is undesirable. However in practice I expect vast majority of listening processes to be able to serve multiple connections and gracefully deal with clients who disconnect ruthlessly.
Notes:
- You need to parse stderr to tell apart cases that generate exit status
1
. - The procedure tells nothing about what process is listening.