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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T01:03:16+00:00 2026-06-13T01:03:16+00:00

I have a subprocess running from a python script and I would like to

  • 0

I have a subprocess running from a python script and I would like to kill this process if the user terminates python (ctrl+D or ctrl+Z or quit()). Is there a function I can define in my python script that will run automatically right before the python script is exited?

  • 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-13T01:03:18+00:00Added an answer on June 13, 2026 at 1:03 am

    You can use the atexit module to register functions to be run.

    import atexit
    
    def bar():
       print "World"
    atexit.register(bar)
    
    #atexit.register also can be used as a decorator since it returns
    # the function:
    @atexit.register
    def foo():
       print "Goodbye"
    

    These functions will be run unless the script exits with os._exit (which it shouldn’t) or unless the script causes python to encounter a serious error (e.g a Segmentation Fault) which doesn’t usually happen, but can if you’re using C extensions that are buggy.

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

Sidebar

Related Questions

I would like to be able to spawn a process in python and have
I have a python function that makes a subprocess call to a shell script
I have an instance of the Popen class created through subprocess.Popen. I would like
i have a python script which keeps crashing on: subprocess.call([pdftotext, pdf_filename]) the error being:
I have the following python code: import pty import subprocess os=subprocess.os from subprocess import
From this guide , I have successfully run the sample exercise. But on running
I have a super simple python script as defined here import multiprocessing from multiprocessing
I'm running a Perl script through the subprocess module in Python on Linux. The
i have created a running process which listens for input: listen = Popen([home/user/listen], stdout=PIPE,
Here is what I have: process = subprocess.Popen([cmd.exe, /c appcfg.py, upload_data, --config_file=..\\bulkloader.yaml, --filename= +

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.