So I’ve come across this problem quite a few times. Normal way to do this is:
ps -ef | grep tomcat
This works most of the times. If tomcat is running, it gives between 1 and 2 lines back but if not, it gives anywhere between 0 and 1 lines back. A much cleaner use of the above command would be with
ps -ef | grep tomcat | wc -l
However, this doesn’t solve the actual problem as along with the tomcat process, it also gives you the process of command
Here’s the command to solve this problem. You can use either of the two below commands:
ps -ef | grep tomcat | grep -v "grep tomcat" | wc -l
ps -ef | grep tomca[t] | wc -l
The first command explicly says that once you get a list of all processes containing the word tomcat, ignore lines containing words
"grep tomcat". And then the usual, pipe it to word count and output the number of lines.
The second one, however, tricks the grep into using a regular expression and ignoring itself. This is because the actual output containing
"grep tomca[t]" will have the square brackets which obviously won’t match the actual regular expression.