Just run:
watch -x pgrep -c -f tomcat
To regularly get a count of processes whose command line contains tomcat
.
Or
watch -x pgrep -c -x tomcat
For the processes whose name (not command line; like in the output of ps -e
wrt ps -ef
) is tomcat.
Or:
watch -x pgrep -c -u tomcat
For the count of processes running as user tomcat
(as it's not clear what column of the output of ps -ef
you intend to match).
With -x
, at least for procps-ng
' implementation of watch
, we skip running a shell but execute the command directly.
Note that pgrep -f tomcat
will not report itself (even though its command line contains tomcat
), but it would count the watch
command and without -x
added to watch
, possibly the sh -c 'pgrep -f tomcat'
that watch
invokes to parse that command line if your sh
implementation doesn't optimise out the fork for the last command.
watch -x pgrep -cf '[t]omcat'
Would avoid that as the [t]omcat
pattern doesn't match itself.
(without -x
, that would have to be:
watch 'pgrep -cf "[t]omcat"'
as [x]
is also a shell globbing operator so needs to be escaped both for your shell and the shell that watch
runs)
Of course, it would also match on a process that runs vi tomcat.conf
or atomcatalog
. Matching processes based on command line is very brittle. With pgrep
you can add more options to refine the filter and limit the risk of identifying the wrong processes.
On modern Linux systems, you should be able to rely on control groups, or query systemd
for that information if tomcat
has properly been setup as a service.