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

  • Home
  • SEARCH
  • 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 6752101
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T13:00:19+00:00 2026-05-26T13:00:19+00:00

I have a script that repeatedly runs an Ant buildfile and scrapes output into

  • 0

I have a script that repeatedly runs an Ant buildfile and scrapes output into a parsable format. When I create the subprocess using Popen, there is a small time window where hitting Ctrl+C will kill the script, but will not kill the subprocess running Ant, leaving a zombie that is printing output to the console that can only be killed using Task Manager. Once Ant has started printing output, hitting Ctrl+C will always kill my script as well as Ant. Is there a way to make it so that hitting Ctrl+C will always kill the subprocess running Ant without leaving a zombie behind?

Also of note: I have a handler for SIGINT that performs a few cleanup operations before calling exit(0). If I manually kill the subprocess in the handler using os.kill(p.pid, signal.SIGTERM) (not SIGINT), then I can successfully kill the subprocess in situations where it would normally zombify. However, when you hit Ctrl+C once Ant has started producing output, you get a stacktrace from subprocess where it is unable to kill the subprocess itself as I have already killed it.


EDIT: My code looked something like:

p = Popen('ls')
def handle_sig_int(signum, stack_frame):
    # perform cleanup
    os.kill(p.pid, signal.SIGTERM)
    exit(0)
signal.signal(signal.SIGINT, handle_sig_int)

p.wait()

Which would produce the following stacktrace when triggered incorrectly:

File "****.py", line ***, in run_test
  p.wait()
File "/usr/lib/python2.5/subprocess.py", line 1122, in wait
  pid, sts = os.waitpid(self.pid, 0)
File "****.py", line ***, in handle_sig_int
  os.kill(p.pid, signal.SIGTERM)

I fixed it by catching the OSError raised by p.wait and exiting:

try:
    p.wait()
except OSError:
    exit('The operation was interrupted by the user')

This seems to work in the vast majority of my test runs. I occasionally get a uname: write error: Broken pipe, though I don’t know what causes it. It seems to happen if I time the Ctrl+C just right before the child process can start displaying output.

  • 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-05-26T13:00:19+00:00Added an answer on May 26, 2026 at 1:00 pm

    Call p.terminate() in your SIGTERM handler:

     if p.poll() is None: # Child still around?
         p.terminate() # kill it
    

    [EDIT] Since you’re stuck with Python 2.5, use os.kill(p.pid, signal.SIGTERM) instead of p.terminate(). The check should make sure you don’t get an exception (or reduce the number of times you get one).

    To make it even better, you can catch the exception and check the message. If it means “child process not found”, then ignore the exception. Otherwise, rethrow it with raise (no arguments).

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

Sidebar

Related Questions

I have a script that checks responses from HTTP servers using the PEAR HTTP
I have a script that works fine on my test server (using IIS6). The
I have a ruby script repeatedly executing the following the INSERT statement using the
I have a Bash script that repeatedly copies files every 5 seconds. But this
I'm trying to create a script that runs in the background automatically and cycles
I have a script that parses the filenames of TV episodes (show.name.s01e02.avi for example),
I have a script that retrieves objects from a remote server through an Ajax
I have a script that takes a table name and generates a control file
I have a script that renders graphs in gnuplot. The graphs all end up
I have a script that successfully encrypts a credit card. I need it to

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.