to get the current process, I used this command:
ps x | grep [full_path_to_tomcat] | grep -v grep | cut -d ' ' -f 1
However, you must be careful. It works on my setup, but it may not work everywhere ... I have two tomcat installations, one of which is / usr / local / tomcat on port 8080 and / usr / local / tomcat _8081 on port 8081. I need to use '/ usr / local / tomcat /' (with a trailing slash) as full_path, because otherwise it will return 2 different pids if tomcat_8081 also works.
This explains what this command does:
1) ps x gives you a list of running processes sorted by pid, tty, stat, time running and command.
2) Applying grep [full_path_to_tomcat] to it, it will find the template [full_path_to_tomcat] in this list. For example, starting ps x | grep /usr/local/tomcat/ ps x | grep /usr/local/tomcat/ may result in the following:
13277 ? Sl 7:13 /usr/local/java/bin/java -Djava.util.logging.config.fil e=/usr/local/tomcat/conf/logging.properties [...] -Dcatalina.home=/usr/local/tomca t [...] 21149 pts/0 S+ 0:00 grep /usr/local/tomcat/
3) Since we get 2 entries instead of one due to grep /usr/local/tomcat/ matching the pattern, delete it. -v is the inverted match flag for grep, which means that it will select only lines that do not match the pattern. So in the previous example, using ps -x | grep /usr/local/tomcat/ | grep -v grep ps -x | grep /usr/local/tomcat/ | grep -v grep ps -x | grep /usr/local/tomcat/ | grep -v grep will return:
13277 ? Sl 7:13 /usr/local/java/bin/java -Djava.util.logging.config.fil e=/usr/local/tomcat/conf/logging.properties [...] -Dcatalina.home=/usr/local/tomca t [...]
4) Cool, now we have the pid that we need. However, we need to remove the rest. To do this, use cut . This command removes partitions from a FILE or standard output. The -d option is the separator, and the -f is the field you need. Excellent. Therefore, we can use a space ('') as a separator and get the first field that matches pid. Running ps x | grep /usr/local/tomcat/ | grep -v grep | cut -d ' ' -f 1 ps x | grep /usr/local/tomcat/ | grep -v grep | cut -d ' ' -f 1 ps x | grep /usr/local/tomcat/ | grep -v grep | cut -d ' ' -f 1 will return:
13277
That's what you need. To use it in a script, it is simple:
#replace below with your tomcat path tomcat_path=/users/tomcat/apache-tomcat-8.0.30 pid=$(ps x | grep "${tomcat_path}" | grep -v grep | cut -d ' ' -f 1) if [ "${pid}" ]; then eval "kill ${pid}" fi