Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8611667
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T04:25:57+00:00 2026-06-12T04:25:57+00:00

Normally, one shuts down Apache Tomcat by running its shutdown.sh script (or batch file).

  • 0

Normally, one shuts down Apache Tomcat by running its shutdown.sh script (or batch file). In some cases, such as when Tomcat’s web container is hosting a web app that does some crazy things with multi-threading, running shutdown.sh gracefully shuts down some parts of Tomcat (as I can see more available memory returning to the system), but the Tomcat process keeps running.

I’m trying to write a simple Python script that:

  1. Calls shutdown.sh
  2. Runs ps -aef | grep tomcat to find any process with Tomcat referenced
  3. If applicable, kills the process with kill -9 <PID>

Here’s what I’ve got so far (as a prototype – I’m brand new to Python BTW):

#!/usr/bin/python

# Imports
import sys
import subprocess

# Load from imported module.
if __init__ == "__main__":
    main()

# Main entry point.
def main():
    # Shutdown Tomcat
    shutdownCmd = "sh ${TOMCAT_HOME}/bin/shutdown.sh"
    subprocess.call([shutdownCmd], shell=true)

    # Check for PID
    grepCmd = "ps -aef | grep tomcat"
    grepResults = subprocess.call([grepCmd], shell=true)

    if(grepResult.length > 1):
        # Get PID and kill it.
        pid = ???
        killPidCmd = "kill -9 $pid"
        subprocess.call([killPidCmd], shell=true)

    # Exit.
    sys.exit()

I’m struggling with the middle part – with obtaining the grep results, checking to see if their size is greater than 1 (since grep always returns a reference to itself, at least 1 result will always be returned, methinks), and then parsing that returned PID and passing it into the killPidCmd. Thanks in advance!

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-12T04:25:59+00:00Added an answer on June 12, 2026 at 4:25 am

    you need to replace grepResults = subprocess.call([grepCmd], shell=true) with grepResults = subprocess.check_output([grepCmd], shell=true) if you want to save the results of the command in grepResults. Then you can use split to convert that to an array and the second element of the array will be the pid: pid = int(grepResults.split()[1])'

    That will only kill the first process however. It doesn’t kill all processes if more then one are open. In order to do that you would have to write:

    grepResults = subprocess.check_output([grepCmd], shell=true).split()
    for i in range(1, len(grepResults), 9):
      pid = grepResults[i]
      killPidCmd = "kill -9 " + pid
      subprocess.call([killPidCmd], shell=true)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

using CodeIgniter normally one has to specify the controllers in the config/routes.php file. This
I come from a background where I normally create one file per class. I
One of my applications prevents windows from shutting down if it is running. The
Where one should normally take JSTL/EL libraries? Does Tomcat distribution should include them? I
Normally one would build a related model instance through its parent object: @child =
I have this code that where I would normally use one line: if (tableView
Will there normally only be one instance of MainForm? I need to hold it
Normally I would have one junit test that shows up in my integration server
Normally I'm pretty competent with CSS but I just can't figure this one out...
i have a django template - one that is normally loaded via a standard

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.