I currently have 2 bash scripts:
1) tomcat.sh
#!/bin/bash
case "$1" in
'start')
/home/testuser/start.sh
;;
'status')
/home/testuser/status.sh
;;
esac
2) status.sh
#!/bin/bash
COUNT="$( ps -ef | grep tomcat| wc -l )"
echo ${COUNT}
if [ "${COUNT}" -eq 2 ]
then
echo "Tomcat is running."
else
echo "Tomcatis not running"
fi
When I check status via these two methods:
./tomcat.sh status: ${COUNT} echos a value of 4.
status.sh: ${COUNT} echos value of 2.
I'm not sure why there is a discrepancy. I'm expecting both values from echo to match since they are essentially executing status.sh. Am I missing something?
EDIT: Added in the actual search values I'm using.
man pgrep./app.sh statusand./status.shyields a different echo value. Shouldn't both be the same since it's essentially running the scriptstatus.sh?ps | greppipeline is timing-dependent, so it can vary unpredictably based on apparently irrelevant things. But that should only add one to the match count, not two. Does the name of the outer script ("app.sh") match the app name as well?pgrep(which exists specifically to avoid this problem) or using a pattern for the app name that doesn't match itself (the standard trick is to put square brackets around one character in the name, e.g.grep "[a]ppname"). Either of those should give you saner results. If not, you'll have to try capturing the output rather than just counting lines, and see what processes are being found.