User avatar
jadro
Posts: 210
Joined: Sun Oct 02, 2016 1:20 pm
Location: Croatia

Self restart Python script

Wed Aug 09, 2017 1:31 pm

Hi,

Can I restart Python script from same script with Python code and how if yes?
If I have a.py started, can I have if state to restart (kill and start again) same a.py script?

Thanx,
Jadro
Oracle back-end database developer
SmartHome IoT & Microprocessor enthusiast and hobbyist

Tyrael_Archangel
Posts: 23
Joined: Mon Feb 29, 2016 10:47 pm

Re: Self restart Python script

Wed Aug 09, 2017 2:15 pm

you can use something like this to restart a python script from within itself

Code: Select all

os.execv('/AbsolutePathtoScript/a.py', lst_args)  
I use this within a script that handles upgrading the rest of my code to restart itself if it needs to upgrade itself.

User avatar
jadro
Posts: 210
Joined: Sun Oct 02, 2016 1:20 pm
Location: Croatia

Re: Self restart Python script

Wed Aug 09, 2017 2:25 pm

Hi,

thanx, I will try this solution.

Jadro
Oracle back-end database developer
SmartHome IoT & Microprocessor enthusiast and hobbyist

User avatar
jadro
Posts: 210
Joined: Sun Oct 02, 2016 1:20 pm
Location: Croatia

Re: Self restart Python script

Wed Aug 09, 2017 2:53 pm

I try this:

Code: Select all

import os
os.execv('/home/pi/myFolder/RestartMySelf.py', [''])
and get error:
SError: [Errno 13] Permission denied

I dont need any permission to that script
Oracle back-end database developer
SmartHome IoT & Microprocessor enthusiast and hobbyist

User avatar
Paeryn
Posts: 1612
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Self restart Python script

Wed Aug 09, 2017 4:01 pm

jadro wrote:
Wed Aug 09, 2017 2:53 pm
I try this:

Code: Select all

import os
os.execv('/home/pi/myFolder/RestartMySelf.py', [''])
and get error:
SError: [Errno 13] Permission denied

I dont need any permission to that script
Did you set the execute permission bit for /home/pi/myFolder/RestartMySelf.py?

Code: Select all

chmod +x /home/pi/myFolder/RestartMySelf.py
She who travels light — forgot something.

Tyrael_Archangel
Posts: 23
Joined: Mon Feb 29, 2016 10:47 pm

Re: Self restart Python script

Wed Aug 09, 2017 5:00 pm

you may also need to add

Code: Select all

#!/usr/bin/python
to the very top of your script (first line), I can't remember if this is required or not for it to work but it can't hurt

User avatar
Paeryn
Posts: 1612
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Self restart Python script

Wed Aug 09, 2017 5:17 pm

Tyrael_Archangel wrote:
Wed Aug 09, 2017 5:00 pm
you may also need to add

Code: Select all

#!/usr/bin/python
to the very top of your script (first line), I can't remember if this is required or not for it to work but it can't hurt
Yes it is. If the program isn't a binary executable then you need a shebang line at the top so the system knows what interpreter to run.
She who travels light — forgot something.

Martin Frezman
Posts: 1021
Joined: Mon Oct 31, 2016 10:05 am

Re: Self restart Python script

Wed Aug 09, 2017 5:51 pm

Paeryn wrote:
Wed Aug 09, 2017 5:17 pm
Tyrael_Archangel wrote:
Wed Aug 09, 2017 5:00 pm
you may also need to add

Code: Select all

#!/usr/bin/python
to the very top of your script (first line), I can't remember if this is required or not for it to work but it can't hurt
Yes it is. If the program isn't a binary executable then you need a shebang line at the top so the system knows what interpreter to run.
It is only required *if* you intend to run your script "directly".

The way I read the most recent post(s) from the OP, it looks like he may have been used to running the script via a command line like: $ python3 /path/to/my/script.py

which, of course, works fine. But then when he tries to run it with the os.execv() thing, he left out the leading "python3". Thus the confusion...
If this post appears in the wrong forums category, my apologies.

User avatar
jadro
Posts: 210
Joined: Sun Oct 02, 2016 1:20 pm
Location: Croatia

Re: Self restart Python script

Wed Aug 09, 2017 6:56 pm

Hi,

I add #!/usr/bin/python

and put my script in this folder /home/pi/myPiConfig/Test/restartMySelf.py

and also add chmod +x /home/pi/myPiConfig/Test/restartMySelf.py

and now it works but with error: Quit with error [Errno 2] No such file or directory !!!!!!! Why?!?!?! This is my directory!!!

This is my code:

Code: Select all

#!/usr/bin/python

import os
import time

try:
    while True:
            print "wait for 5 sec..."
            time.sleep(5)
            print "Restarting..."
            os.execv('/home/pi/myPiConfig/Test/restartMySelf.py',  [''])
except KeyboardInterrupt:
    print " Quit"
except Exception as e:
    print " Quit with error " + str(e)
Oracle back-end database developer
SmartHome IoT & Microprocessor enthusiast and hobbyist

User avatar
Paeryn
Posts: 1612
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Self restart Python script

Wed Aug 09, 2017 8:29 pm

jadro wrote:
Wed Aug 09, 2017 6:56 pm
Hi,

I add #!/usr/bin/python

and put my script in this folder /home/pi/myPiConfig/Test/restartMySelf.py

and also add chmod +x /home/pi/myPiConfig/Test/restartMySelf.py

and now it works but with error: Quit with error [Errno 2] No such file or directory !!!!!!! Why?!?!?! This is my directory!!!

This is my code:

Code: Select all

#!/usr/bin/python

import os
import time

try:
    while True:
            print "wait for 5 sec..."
            time.sleep(5)
            print "Restarting..."
            os.execv('/home/pi/myPiConfig/Test/restartMySelf.py',  [''])
except KeyboardInterrupt:
    print " Quit"
except Exception as e:
    print " Quit with error " + str(e)
Have you got the filename / path correct including capitalisation? Your earlier post referred to the filename as RestartMySelf.py and now you refer to it as restartMySelf.py
She who travels light — forgot something.

User avatar
jadro
Posts: 210
Joined: Sun Oct 02, 2016 1:20 pm
Location: Croatia

Re: Self restart Python script

Wed Aug 09, 2017 9:43 pm

I change the file name when create a new test map. And yes, i copy paste whole path from folder explorer and it does not working. I replace first latter from name and now it is restartMySelf.py
Oracle back-end database developer
SmartHome IoT & Microprocessor enthusiast and hobbyist

User avatar
jadro
Posts: 210
Joined: Sun Oct 02, 2016 1:20 pm
Location: Croatia

Re: Self restart Python script

Fri Aug 11, 2017 10:30 am

Here is my code that restarts but when restart it does not print any line of printable string:

Code: Select all

#!/usr/bin/python

import os
import time
import sys

print "Here I'am"
print os.path.abspath(__file__)

try:
    while True:
            print "wait for 5 sec..."
            time.sleep(5)
            print "Restarting..."
           # os.execv('/home/pi/myPiConfig/Test/restartMySelf.py',  [''])
            os.execv(sys.executable, [sys.executable] +  ['restartMySelf'])
except KeyboardInterrupt:
    print " Quit"
except Exception as e:
    print " Quit with error " + str(e)
Result is this:
------------------------------------------------------------------------------------------------------------------------------------------------------
Python 2.7.9 (default, Sep 17 2016, 20:26:04)
[GCC 4.9.2] on linux2
Type "copyright", "credits" or "license()" for more information.
>>> ================================ RESTART ================================
>>>
Here I'am
/home/pi/myPiConfig/Test/restartMySelf.py
wait for 5 sec...
Restarting...

>>> ================================ RESTART ================================
>>>
------------------------------------------------------------------------------------------------------------------------------------------------------
and no message is here like Here I'am or so...
Why?
Thanx,
Jadro
Oracle back-end database developer
SmartHome IoT & Microprocessor enthusiast and hobbyist

User avatar
Paeryn
Posts: 1612
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Self restart Python script

Fri Aug 11, 2017 11:33 am

That looks like you're running it from within IDLE. If so then it more than likely isn't working due to how IDLE runs the interpreter. The moment you replace the executable when calling execv() you loose the connection it had to IDLE and the window.
She who travels light — forgot something.

User avatar
jadro
Posts: 210
Joined: Sun Oct 02, 2016 1:20 pm
Location: Croatia

Re: Self restart Python script

Fri Aug 11, 2017 11:39 am

Yes, it runs from IDLE for now.
How to do this in IDLE to show print message again within this self script?
How to cll this script right way?
Oracle back-end database developer
SmartHome IoT & Microprocessor enthusiast and hobbyist

User avatar
Paeryn
Posts: 1612
Joined: Wed Nov 23, 2011 1:10 am
Location: Sheffield, England

Re: Self restart Python script

Fri Aug 11, 2017 11:51 am

I don't think you can force a restart like that under IDLE as you have no way of inserting the hooks that IDLE needs.

Do you really need to reload your program from scratch rather than having a controlled loop?
She who travels light — forgot something.

User avatar
jadro
Posts: 210
Joined: Sun Oct 02, 2016 1:20 pm
Location: Croatia

Re: Self restart Python script

Fri Aug 11, 2017 11:55 am

Good question... maybe in some situations...
Thanx for the answers.
Jadro
Oracle back-end database developer
SmartHome IoT & Microprocessor enthusiast and hobbyist

Return to “Python”

Who is online

Users browsing this forum: elParaguayo and 16 guests