Page 1 of 1

Need help -- exit from script

Posted: Thu Jan 28, 2016 6:28 pm
by mmkw43
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

Re: Need help -- exit from script

Posted: Thu Jan 28, 2016 6:30 pm
by mmkw43
I realize shutdown.exe is a windows thing, didn't mean to show that

Re: Need help -- exit from script

Posted: Thu Jan 28, 2016 6:40 pm
by gordon77
From python l use this to shutdown

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

For reboot I would try sudo reboot

Re: Need help -- exit from script

Posted: Thu Jan 28, 2016 6:47 pm
by rurwin
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.

Re: Need help -- exit from script

Posted: Thu Jan 28, 2016 6:56 pm
by mmkw43
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?

Re: Need help -- exit from script

Posted: Thu Jan 28, 2016 10:24 pm
by rurwin
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.

Re: Need help -- exit from script

Posted: Fri Jan 29, 2016 12:59 am
by mmkw43
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

Re: Need help -- exit from script

Posted: Fri Jan 29, 2016 4:25 am
by stderr
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.

Re: Need help -- exit from script

Posted: Fri Jan 29, 2016 9:45 am
by DougieLawson

Code: Select all

import sys
sys.exit()
will exit a python program.

Re: Need help -- exit from script

Posted: Fri Jan 29, 2016 6:15 pm
by mmkw43
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

Re: Need help -- exit from script

Posted: Fri Jan 29, 2016 7:32 pm
by mmkw43
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.

Re: Need help -- exit from script

Posted: Fri Jan 29, 2016 9:21 pm
by stderr
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")

Re: Need help -- exit from script

Posted: Sat Jan 30, 2016 1:10 am
by mmkw43
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

Re: Need help -- exit from script

Posted: Sat Jan 30, 2016 2:30 am
by stderr
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.

Re: Need help -- exit from script

Posted: Sat Jan 30, 2016 10:05 am
by rurwin
Are there any threads that could still be running? That prevents Python from stopping.

Re: Need help -- exit from script

Posted: Sat Jan 30, 2016 2:02 pm
by mmkw43
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.

Re: Need help -- exit from script

Posted: Sat Jan 30, 2016 2:06 pm
by rurwin
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