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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 17, 20262026-05-17T19:26:49+00:00 2026-05-17T19:26:49+00:00

I have a Python program that runs a cell model continuously. when I press

  • 0

I have a Python program that runs a cell model continuously. when I press “A” or “B” certain functions are called – cell divide, etc. when the “esc” key is pressed the simulation exits. Is there a way for the program to exit and then restart itself when “esc” is pressed?

  • 1 1 Answer
  • 2 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-17T19:26:50+00:00Added an answer on May 17, 2026 at 7:26 pm

    Yes. This is probably what you want

    The-Evil-MacBook:~ ivucica$ cat test.py
    #!/usr/bin/env python
    import sys
    import os
    print sys.argv[0] + " with argcount " + str(len(sys.argv))
    if len(sys.argv) < 2 or sys.argv[1] != "2":
        print "doing recursion"
        os.system(sys.argv[0] + " 2");
    else:
        print "not doing recursion"
    
    exit(0)
    The-Evil-MacBook:~ ivucica$ ./test.py
    ./test.py with argcount 1
    doing recursion
    ./test.py with argcount 2
    not doing recursion
    The-Evil-MacBook:~ ivucica$ 
    

    So when you want the program to restart itself, just call its sys.argv[0] using os.system() and immediately call exit(some_return_code_here) (zero means ‘no error’). You might want to pass an extra argument so it knows it’s a restarted instance, but are by no means required to do so; I did so above to prevent endless loop. If you have other mechanisms to prevent endless loops, then use those.

    Please also note: for the above code, you need to run the program directly; python test.py did not do the trick for me (for obvious reasons). Also, above will probably work only under UNIX systems.

    Note, also, that system() is blocking. If you need the original program to complete shutdown upon launching new program, easiest way would be to send the new one into background (thus “unblocking” system()). Just modify the line like this:

        os.system(sys.argv[0] + " 2 &");
    

    Note the “&” which tells the shell to send the new process into background.

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

Sidebar

Related Questions

I have a python script that runs a program, which generates few .exe files
I have a Python program that runs a series of experiments, with no data
I have a Python program that spawns many threads, runs 4 at a time,
I have a program that runs in Python 2 and Python 3, but there
I have a java program that runs another (Python) program as a process. Process
I have a Python 3.2 program that runs like this: import platform sysname =
I have a program that runs in Python, without the console present (which is
I have a Python program that executes a large MySQL statement. How do I
I have a python program that takes the md5 & sha1 hash values of
I have a python program that must work on Windows and Linux. There are

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.