Score:0

Shell Scripting - Multiple Process ID's instead of one

cn flag

I'm writing a simple bash script to shutdown tomcat, and if it doesn't stop gracefully then check if the tomcat's PID still exists and kill it.

I pass the tomcat name as a variable to the script as below. In some instances I pass two or three names of tomcat, which is why the use of FOR LOOP below

./shutdown.sh tomcat1

Content of the Shutdown.sh script

#!/bin/bash
for name in "$@"
do
    bash /opt/$name/bin/shutdown.sh   
done

sleep 30

for name in "$@"
do
   process_id=`ps -ef | grep $name | grep -v grep | awk '{ print $2 }'`

   if [ $process_id ] 
   then
       kill -9 $process_id
   fi

done

echo " Script Execution completed"

When tomcat shutdowns gracefully there is no issue. But when tomcat doesn't stop, I'm having issues.

Below peice of code when ran directly on command prompt gives me the correct process ID(62457) of tomcat. But the same peice of in shell script is giving me three process ID's(62610,62611,62457).

process_id=`ps -ef | grep $name | grep -v grep | awk '{ print $2 }'`

can you let me know why I'm getting three process ID's in the script compared to just one ?

Any other easier suggestion to KILL ?

Josh Zhang avatar
ph flag
Instead of looping through possible PID's why don't you just pipe the results into `xargs kill -9`? IE: `ps -ef | grep $name | grep -v grep | awk '{ print $2 }' | xargs kill -9`
Raj K avatar
cn flag
I haven't tested it yet, but wondering. In scenario where tomcat shutdown gracefully and there is no PID to KILL, basically no input to "xargs" command. will the xargs command complain of the syntax error ?
Josh Zhang avatar
ph flag
If there is no PID's for `xargs kill -9` you will actually just get an error from `kill` since it would be the same as running the command without any PID's.
Brandon Xavier avatar
us flag
Your process_id command will include the `./shutdown.sh` command itself. You could add another grep in the pipeline like `. . .| grep -v shutdown.sh | grep -v grep . . .` or do something like ` . . . | egrep -v 'grep|shutdown.sh' . . .`
John Mahowald avatar
cn flag
Does your operating system have a generic init script or service manager system? Properly doing it in shell is not as trivial as it seems.
Raj K avatar
cn flag
@BrandonXavier ` | grep -v shutdown.sh | grep -v grep ` worked. Thank you.
Piotr P. Karwasz avatar
by flag
If you set `CATALINA_PID` when starting Tomcat, then `shutdown.sh 30 -force` does exactly the same thing as your script (but more properly). Cf. the [source code](https://github.com/apache/tomcat/blob/76da8deac37071e8a891580d74ba1322c7a2da2c/bin/catalina.sh#L482).
mangohost

Post an answer

Most people don’t grasp that asking a lot of questions unlocks learning and improves interpersonal bonding. In Alison’s studies, for example, though people could accurately recall how many questions had been asked in their conversations, they didn’t intuit the link between questions and liking. Across four studies, in which participants were engaged in conversations themselves or read transcripts of others’ conversations, people tended not to realize that question asking would influence—or had influenced—the level of amity between the conversationalists.