Code:
[xxx@yyy ~]$ netstat -an
As unprivileged user list all network connections and disable name resolution (that's good)
grep the output for the arbitrary string "8009" (no explanation so here that could be a port or inode number or part of a name of a socket)
count the output lines
Code:
[xxx@yyy ~]$ ps -ef
As unprivileged user full-format list all processes (a lot of people haven't heard of 'pgrep' or the "-C" arg of 'ps')
grep the output for the arbitrary string "apache" (that could be any part of a user or process or file name or argument)
count the output lines
If you want to count all TCP/8009 ports that aren't in use (listening) or closing down (any *_WAIT states) you could use 'lsof -Pwln -iTCP:8009 -sTCP:ESTABLISHED|wc -l'.
Counting process names "/usr/sbin/httpd" could be 'pgrep httpd|wc -l' or in BASH: 'PIDS=($(\ps --no-header -C httpd -o pid)); echo ${#PIDS[@]};'.