mmkw43
Posts: 554
Joined: Tue Dec 24, 2013 6:18 pm

Need help -- exit from script

Thu Jan 28, 2016 6:28 pm

I try to figure things out myself before posting but getting a lot of conflicting info in my reading about shutdown from within a python script and it makes me nervous to 'try" something when it comes to shutdown. The goal is to offer 3 choices of restart applcation, reboot or shutdown -- all with a button push and associated function. And I'm reading about os.execv, shutdown.exe and so on. Concerned about open windows and whether they matter etc. There really isn't any decent tutorial on this.

I know I can use os.system and run commands but want to do this right and be safe and want to understand what I'm doing.

I'm starting my application from the command line and using Tkinter. -thanks for any help

mmkw43
Posts: 554
Joined: Tue Dec 24, 2013 6:18 pm

Re: Need help -- exit from script

Thu Jan 28, 2016 6:30 pm

I realize shutdown.exe is a windows thing, didn't mean to show that

gordon77
Posts: 5077
Joined: Sun Aug 05, 2012 3:12 pm

Re: Need help -- exit from script

Thu Jan 28, 2016 6:40 pm

From python l use this to shutdown

path = 'sudo shutdown -h now '
os.system (path)

For reboot I would try sudo reboot

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Need help -- exit from script

Thu Jan 28, 2016 6:47 pm

All of the applications that are running are shut down fairly abruptly. Most applications will be OK with this, but you will lose unsaved work. If one of them happens to be writing to the SD when it happens then that file may be corrupted.

mmkw43
Posts: 554
Joined: Tue Dec 24, 2013 6:18 pm

Re: Need help -- exit from script

Thu Jan 28, 2016 6:56 pm

my other post didn't show -- anyway, yes I'm overthinking this. And after going through a corrupt SD card I'm cautious.

reboot and shutdown I figured would be simple enough and I guess for restart I could simply run an exit command and then restart my application py file in terminal again?

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Need help -- exit from script

Thu Jan 28, 2016 10:24 pm

That's the easy way. You can put it inside a bash script that restarts it if the exit value is a certain number. The crazy "what on Earth is the lunatic doing now" method is to os.execv yourself.

mmkw43
Posts: 554
Joined: Tue Dec 24, 2013 6:18 pm

Re: Need help -- exit from script

Fri Jan 29, 2016 12:59 am

I have a feeling I may get stuck on this tomorrow -- sitting here trying to figure how I exit, without exiting the terminal. I want to stop the running application and restart it. Found this -- raise SystemExit ? (stops program? -- but terminal stays open?) then I can run my command to restart ? --thank you

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Need help -- exit from script

Fri Jan 29, 2016 4:25 am

mmkw43 wrote:I have a feeling I may get stuck on this tomorrow -- sitting here trying to figure how I exit, without exiting the terminal. I want to stop the running application and restart it.
Before you seemed to want to restart the computer, now you just want to restart your application? I don't understand why you want to do this, but you could have a bash loop and use that to restart your python program which you simple quit in the normal way.

User avatar
DougieLawson
Posts: 39304
Joined: Sun Jun 16, 2013 11:19 pm
Location: A small cave in deepest darkest Basingstoke, UK
Contact: Website Twitter

Re: Need help -- exit from script

Fri Jan 29, 2016 9:45 am

Code: Select all

import sys
sys.exit()
will exit a python program.
Note: Any requirement to use a crystal ball or mind reading will result in me ignoring your question.

Criticising any questions is banned on this forum.

Any DMs sent on Twitter will be answered next month.
All non-medical doctors are on my foes list.

mmkw43
Posts: 554
Joined: Tue Dec 24, 2013 6:18 pm

Re: Need help -- exit from script

Fri Jan 29, 2016 6:15 pm

Ok thanks. The application has a setup mode and a run mode -- it will be easier to quit and start again to get back to setup without going back and making a *&^%load of changes. -- one of those "after the fact" things. --thanks

mmkw43
Posts: 554
Joined: Tue Dec 24, 2013 6:18 pm

Re: Need help -- exit from script

Fri Jan 29, 2016 7:32 pm

Have tested all the options which work fine but I'm out to lunch on what's happening with terminal -- sys.exit() for example, seemingly quits the application but I can't figure out how to get a new command prompt in terminal. (which says to me the script is still running?)
I know the prompt reappears when it finishes the task at hand?

I may give up on restarting this but I need to --

quit the app running that I started in terminal
keep terminal open and wait a bit I suppose,
then give a command to restart it.

I may just have to bite the bullet and rewrite some things. Probably best.

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Need help -- exit from script

Fri Jan 29, 2016 9:21 pm

mmkw43 wrote:Have tested all the options which work fine but I'm out to lunch on what's happening with terminal -- sys.exit() for example, seemingly quits the application but I can't figure out how to get a new command prompt in terminal.
I just tested this running python 3.x from a script file that is marked as executable and it returns to a normal prompt:

#!/usr/bin/python3

import sys

print("hello world")
sys.exit("This is a sys.exit")
print("end of world")

mmkw43
Posts: 554
Joined: Tue Dec 24, 2013 6:18 pm

Re: Need help -- exit from script

Sat Jan 30, 2016 1:10 am

Ok -- will look at this tomorrow, maybe I need like you did, to run a task like print just to get to the prompt. I'm also running tkinter but doing a root.destroy before calling sys.exit() -- thanks

stderr
Posts: 2178
Joined: Sat Dec 01, 2012 11:29 pm

Re: Need help -- exit from script

Sat Jan 30, 2016 2:30 am

mmkw43 wrote:Ok -- will look at this tomorrow, maybe I need like you did, to run a task like print just to get to the prompt. I'm also running tkinter but doing a root.destroy before calling sys.exit() -- thanks
If I just run that, it doesn't print the "end of the world" part, if I comment sys.exit out, it does. If you are running tkinter, I didn't test anything like that, all I tested was from the prompt in the way I said.

If there is other stuff running, maybe that is preventing it from ending correctly, although you'd think that sys.exit means what it says. I would remove bits until it did work, like the tkinter part.

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Need help -- exit from script

Sat Jan 30, 2016 10:05 am

Are there any threads that could still be running? That prevents Python from stopping.

mmkw43
Posts: 554
Joined: Tue Dec 24, 2013 6:18 pm

Re: Need help -- exit from script

Sat Jan 30, 2016 2:02 pm

Yes there are. 2 in fact -- thanks. And I know (at least from my studies) that stopping a thread is pretty much impossible -- It's no big deal anyway, I'm starting to think that just reboot and shutdown will be good enough -- reboot will go straight to my setup portion of the application, was just wanting to save the 2 minutes but it may have it's advantages.


On another subject, am I going to be able to autostart terminal in Raspian? yes/no? (I'll figure out how do do it) Eventually, I'm wanting to make the PI boot up and go straight to my program.

Appreciate the help.

User avatar
rurwin
Forum Moderator
Forum Moderator
Posts: 4258
Joined: Mon Jan 09, 2012 3:16 pm
Contact: Website

Re: Need help -- exit from script

Sat Jan 30, 2016 2:06 pm

You can't stop a thread, but a thread can stop.

You can set a variable in the main thread that tells the others to terminate. Although that does mean that they have to check it from time to time and if they are stuck in a blocking system call, that could take a while.

The other way you can do it is to send yourself a SIGINTR signal -- that's exactly the same as hitting Ctrl-C. Or if you wanted to be a little less brutal, send a different signal, but you will need to understand how Python handles signals: https://docs.python.org/3/library/signal.html

Return to “Python”