0

For some reason, I would like to launch a script just before exiting my current one. I found one simple way to do it:

script1.py:

#**
#some code
#**
p=subprocess.Popen(["python", "script2.py"])

With this, if I run script1, it will launch script2 at the end just before exiting.

However if I run script1 within a screen (screen python script1.py), the screen will terminate before script2 has even a chance to start.

Moreover, since script2 is very long, I would like not to wait that it ends before exiting script1. Is there a way to do it? Maybe by launching script2 in another screen? If so how can I do this?

1 Answer 1

1

You can create the second script and then wait for it to complete

p=subprocess.Popen(["python", "script2.py"])
p.wait()

or more elegantly you can use os.execv that will replace your python interpreter by the new one (the PID will stay unchanged, you'll keep the console).

os.execv("/usr/bin/python", ["/usr/bin/python", "script2.py"])
Sign up to request clarification or add additional context in comments.

3 Comments

I like your os.execv idea, but if I understood the function it won't execute anything after the subprocess call in script1? For example if I have things like print "script2 has been launched" just after the subprocess call and before the exiting of script1, they won't be executed?
That's it, the message will not be printed: Your process will die and be replaced by the new one, nothing after this system call will be executed.
Thanks, that's exactly what I'm looking for, I'll just run before this the few things I wanted to run after and that'll do it!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.