Script to find the process ID and force stop process under Linux

Source: Internet
Author: User
Tags stop script

The stop script shutdown.sh for Tomcat under Linux often fails, causing the Tomcat process not to shut down. Therefore, you can only manually find the process ID and then use the KILL command to force the stop. Check it every time, and then kill the process. I feel a bit of trouble, so I write this action in the script. First, the ideaThis script is actually 2 steps, get the process ID first, then kill the process.
(1) method to get the process IDThis can be obtained with the awk command.
 
   
  
  1. ps -ef | grep 你的进程 | grep -v grep | awk ‘{print $2}‘
This grep is filtered out with-V, and the 2nd parameter is the process ID, using the awk command.
(2) method of killing processThis will kill you directly-it's OK.
 
   
  
  1. kill -9 你的进程id
Second, the script codeThe code is as follows (/root/tomcat-instance/shutdown_sp.sh):
  
 
  1. sp_pid=`ps -ef | grep sp-tomcat | grep -v grep | awk ‘{print $2}‘`
  2. if [ -z "$sp_pid" ];
  3. then
  4. echo "[ not find sp-tomcat pid ]"
  5. else
  6. echo "find result: $sp_pid "
  7. kill -9 $sp_pid
  8. fi
Note: when using, you need to replace the first line of Sp-tomcat with the one you wish to kill.
Description: It may be said that the script will error when there are multiple process IDs for the lookup process.                             In fact, there is a space between them when there are multiple process IDs. When the kill command kills more than one process at a time, the process ID needs a space. So you don't need a For loop to kill the process.

Script to find the process ID and force stop process under Linux

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.