There are several ways to interpret your question, so I will try to answer both of them:
Network Service Check
If your goal is to verify that a specific port is serving a specific application protocol, I would verify this by running the appropriate client.
To test Apache and Tomcat, I would use a GET specific URL and check the result code. For example:
- name: check if apache is running command: curl -sf http://webserver/check_url
And similarly for Tomcat.
To test MySQL, I would use the MySQL client:
- name: check if mysql is running command: mysql -h dbhost -P dbport -e 'select 1'
Checking the process to which the socket belongs
If you really wanted to see which process keeps a particular port open, I think you could combine ss and grep , but that seems weird and unnecessary. Something like:
- name: check if httpd has port 80 open shell: ss -tp state listening sport = :80 | grep httpd
If you want to check for a specific process id, you can do something similar with lsof :
- name: check that pid {{apache_pid}} is listening on port 80 shell: lsof -p 1036 -P | grep 'TCP \*:80'
But then again, I do not necessarily find these options particularly useful. The service checks in the previous section seem more appropriate.
larsks
source share