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 8627869
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 12, 20262026-06-12T08:23:24+00:00 2026-06-12T08:23:24+00:00

I want a Python script to grep for a particular process, extract its PID,

  • 0

I want a Python script to grep for a particular process, extract its PID, and kill it. Here’s what I’m using:

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.check_output([grepCmd], shell=True)

    # Get PID(s) and kill it/them.
    for i in range(1, len(grepResults), 9):
    pid = grepResults[i]
    killPidCmd = "kill -9 " + pid
    subprocess.call([killPidCmd], shell=True)

    # Exit.
    sys.exit()

However, although it does shutdown my process (Apache Tomcat), I get the following “warnings” in the terminal whenever I run it:

kill -l [exitstatus]
/bin/sh: 1: kill: Operation not permitted

/bin/sh: 1: kill: Illegal number: h
/bin/sh: 1: kill: Illegal number: v
/bin/sh: 1: kill: Illegal number: l
/bin/sh: 1: kill: Operation not permitted

/bin/sh: 1: kill: Illegal number: .
/bin/sh: 1: kill: Illegal number: i
/bin/sh: 1: kill: Illegal number: D
/bin/sh: 1: kill: Illegal number: l
/bin/sh: 1: kill: Illegal number: .
/bin/sh: 1: kill: Illegal number: i
/bin/sh: 1: kill: Illegal number: /
/bin/sh: 1: kill: Illegal number: s
/bin/sh: 1: kill: Illegal number: r
/bin/sh: 1: kill: Illegal number: /
/bin/sh: 1: kill: Illegal number: .
/bin/sh: 1: kill: Illegal number: c
/bin/sh: 1: kill: Illegal number: t
/bin/sh: 1: kill: Illegal number: c
/bin/sh: 1: kill: Illegal number: i
/bin/sh: 1: kill: Illegal number: r
/bin/sh: 1: kill: Illegal number: a
/bin/sh: 1: kill: Illegal number: l
/bin/sh: 1: kill: Illegal number: a
/bin/sh: 1: kill: Illegal number: g
/bin/sh: 1: kill: Illegal number: j
/bin/sh: 1: kill: Illegal number: s
/bin/sh: 1: kill: Illegal number: g
/bin/sh: 1: kill: Illegal number: 
/bin/sh: 1: kill: Illegal number: X
/bin/sh: 1: kill: Illegal number: 
/bin/sh: 1: kill: Usage: kill [-s sigspec | -signum | -sigspec] [pid | job]... or
kill -l [exitstatus]
/bin/sh: 1: kill: Illegal number: n
/bin/sh: 1: kill: Illegal number: i
/bin/sh: 1: kill: Illegal number: /
/bin/sh: 1: kill: Illegal number: s
/bin/sh: 1: kill: Illegal number: r
/bin/sh: 1: kill: Illegal number: /
/bin/sh: 1: kill: Illegal number: .
/bin/sh: 1: kill: Illegal number: c
/bin/sh: 1: kill: Illegal number: t
/bin/sh: 1: kill: Illegal number: e
/bin/sh: 1: kill: Illegal number: 
/bin/sh: 1: kill: Illegal number: h
/bin/sh: 1: kill: Illegal number: h
/bin/sh: 1: kill: Illegal number: n
/bin/sh: 1: kill: Illegal number: /
/bin/sh: 1: kill: Illegal number: o
/bin/sh: 1: kill: Illegal number: .
/bin/sh: 1: kill: Illegal number: e
/bin/sh: 1: kill: Operation not permitted

/bin/sh: 1: kill: Illegal number: n
/bin/sh: 1: kill: Illegal number: a
/bin/sh: 1: kill: Illegal number: o
/bin/sh: 1: kill: Illegal number: e
/bin/sh: 1: kill: Illegal number: x
/bin/sh: 1: kill: Illegal number: c
/bin/sh: 1: kill: Illegal number: t
/bin/sh: 1: kill: Illegal number: a
/bin/sh: 1: kill: Illegal number: m
/bin/sh: 1: kill: Operation not permitted

/bin/sh: 1: kill: Illegal number: m
/bin/sh: 1: kill: Illegal number: .
/bin/sh: 1: kill: Illegal number: t
/bin/sh: 1: kill: Illegal number: s
/bin/sh: 1: kill: Illegal number: z
/bin/sh: 1: kill: Illegal number: a
/bin/sh: 1: kill: Illegal number: v
/bin/sh: 1: kill: Illegal number: t
Killed

Can any Python meastros take a look at the script and figure out why I’m getting all these /bin/sh: 1: kill: Illegal number: <whatever> warnings/errors/ouputs? Any suggestions as to how to fix them? 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-12T08:23:25+00:00Added an answer on June 12, 2026 at 8:23 am
    for i in range(1, len(grepResults), 9):
        pid = grepResults[i]
        killPidCmd = "kill -9 " + pid
        subprocess.call([killPidCmd], shell=True)
    

    Looks like your grepResults is some plain string, and you’re picking single characters from it. That’s why kill complains about strange numbers (which are no numbers but chars) that you’re giving to it. Check the output of this command, then you should see, what’s going wrong:

    grepResults = subprocess.check_output([grepCmd], shell=True)
    

    Insert something like that and check, if pid fits for you. Also make sure, that the criterias (if 'grep' in line) fit and don’t match other processes. Maybe you have to add more if-clauses here.

    import subprocess
    grepCmd = "ps -aef | grep tomcat"
    grepResults = subprocess.check_output([grepCmd], shell=True)
    for line in grepResults.strip().split('\n'):
        if 'grep' in line: 
            continue
        pid = line.split()[1]
    else:
        pid = None
    # print pid
    

    If pid is None, it should exit with error, as there was no running service found. And in case you want to kill more than one pid, of course you have to create some list, or directly kill the pid within this loop.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to search for images using a Python script and pull them off
I want my Python script to be able to run one of its functions
I want a small python script to set the HUDSON_HOME environment variable. When using
I've got a noisy python script that I want to silence by directing its
I have a python script and I want to launch an independent daemon process.
I want my python script to simultaneously accept POST variables and query string variables
I want to run a python script from within another. By within I mean
I have a python script that I want always to run in the background.
I have a Iron Python script that I want to run and then have
I am with a python script. I want to open a file to retrieve

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.