I have this line inside my shell script:
tomcatPID=$(ps -ef | grep tomcat | grep $TOMCAT_ACCOUNT | grep -v grep | grep -v restart | awk '{print $2}')
I made sure that the $TOMCAT_ACCOUNT part is okay. What I have problems with is the grep -v. That works in the terminal, but when I put it in the shell script, it ignores that grep and gives me results that include it.
The question is: Why is grep -v being ignored inside a shell script but on the terminal it works fine?
For example:
Let's say I'm working under become application, so am at application@servername and in the terminal I run:
tomcatPID=$(ps -ef | grep tomcat | grep application | grep -v grep | grep -v restart | awk '{print $2}')
Output: 42345 which only the processID for the tomcat that is running my application.
If I create file.sh and inside I have the exact same:
tomcatPID=$(ps -ef | grep tomcat | grep $TOMCAT_ACCOUNT | grep -v grep | grep -v restart | awk '{print $2}')
and then run it using: ./file.sh
output: 42345 6534 which are the processID of my tomcat and the processID of the grep that I'm running. Hence, the grep -v grep to avoid getting that second processID but it gets ignored inside the .sh file.
tomcatPID=$(pgrep tomcat)instead. Readman pgrep.| awk '{print $2}'and then addecho "$tomcatPID"so we can see exactly what it is matchingtomcatPID=$(ps -fu "$TOMCAT_USER" | awk '/[t]omcat/ && !/[r]estart/ {print $2}')maybe