running python script (with GUI wxpython) as a service - python

I have a python script with GUI (using wxpython). I want to run it continuously on my (k)ubuntu system as a service. In case it exits due to some exception, I need it to restart automatically. I tried upstart but it immediately stops the service as soon as it starts.
Is there a super simple way to do this? (I tried restarting the python script within itself, tried simple shell scripts with infinite loops. But need something robust and reliable.)
Any help is greatly appreciated.

I know you said you tried shell scripts with infinite loops, but did you try using an "outer" Python script that runs perpetually as a service; it could just catch the exceptions and restart the Python GUI script if an exception were to occur.
Something like:
import myGUI
while True:
try:
myGUI.runGUICode() # make sure the execution stays in this loop
except:
pass # or do some recovery, initiallization, and logging here

Related

How would I code something in python to automatically close an app upon opening?

I've been coding python for some time now, and I'd like to code a program that would automatically close an app when it opens. For example, I would like a to code something that would automatically close steam or something similar when it opens. How would I do this? I've looked over the internet and can't find my answer. Is this even something to do in python?
You could let python run in the background and check every couple of seconds if a process exists. A list of running processes shows you the name of the program that you want to close. You cannot use a PID because it changes every time you restart the program. See this thread to close a process. If you're running Linux, you can easily run python in the background like this: python main.py &
Or you can create a Linux service to run python: https://medium.com/codex/setup-a-python-script-as-a-service-through-systemctl-systemd-f0cc55a42267

Keep python script running (restart if exits)

I'm running a python script from the boot of the system (tried both init.d and systemd). If something goes wrong - it quits, writes problem in a log, but I need to restart it manually. Is there a way to do this check with native tools (e.g. not checking "ps -A" and restart)?
Since you're already using a service manager like systemd to run your script at boot, I would configure that service manager to automatically restart the script when it crashes. (This is a big part of what service managers are for!)
According to the systemd.service man page, you can add something like this to your service file:
Restart=on-failure
Perhaps you can use a try and except block to output the error and restart the software without quitting?
try:
print("a")
except Exception as e:
print(e)
This will stop at the first problem and call the except block. You can input a restart code inside the except block, and run the try block again.

Python script freezing program

I am new here but I recently have been messing around with python and Qt. My situation is that one of the scripts I call does a lot of OS commands and basically waits for a response. When I call this script it runs fine and acts accordingly except in my main program the screen is frozen until I exit out of the cmd. I think this is because mt script just waits there for a response, is there anyway to make it so that even though the script is running and executing(waiting for response with cmd) the user can still use other aspects of the main program?
As mentioned in comments, you will need to use threading. Threading allows multiple functions to executed at the same time. Check out this link Python threading.
You'll just have to run your side script on a different thread.

how to use systemd to run a python script forever and restart if it dies halfway on raspberry pi 3?

I have read that upstart is obsolete in favor of systemd for raspberry pi 3.
My question is how do I run a python script :
a) forever unless I manually kill it
b) can restart if it dies due to some exception or stop running automatically without any human intervention
my python script itself is already using modules like schedule and while True loops to keep running certain jobs every few seconds.
I am just worried that it will die/stop (which it did) after some indeterminate amount of time.
If it stops, all I want is for it to restart.
Currently, I run the script by double clicking it to open in Python IDLE (2.7) and then run module.
What is the best way to run and open a python script and let it run continuously non-stop and then have it auto restart when it dies / stops for whatever reason?
See this picture where it suddenly stops by itself at 5 plus am
I think you should take a look at Python Supervisor. Supervisor will manage the restart in the event of a crash or even machine re-starts.
http://supervisord.org/
An easier method might be the handle the failure within your script. If it is failing due to some exception, wrap the offending code in a try:except block and handle it gracefully within the script.
That said, this post has the information you need to use systemd to execute a BASH script:
https://unix.stackexchange.com/questions/47695/how-to-write-startup-script-for-systemd
Within your script, you can easily run a python script and catch its return value (when it returns failure in your case) and react appropriately.
Something like this:
#!/bin/bash
python ~/path/to/my/script/myScript.py
if [ $? -ne 0 ] ; then #handle the failure here.
If that won't work either, you can create a script whose sole job is to call the other script and handle its failures, and use systemd to call that script.

End Python Script when running it as boot script?

I am using Debian and I have a python script that I would like to run during rc.local so that it will run on boot. I already have it working with a test file that is meant to run and terminate.
The problem is that this file should eventually run indefinitely using Scheduler. It's job is to do serial reads, a small amount of processing on those reads, and inserts into a MySQL database. However, I am nervous about then not being able to cancel the script to get to my login prompt if changes need to be made since I was unable to terminate the test script early using Ctrl+C (^C).
My hope is that there is some command that I am just missing that will accomplish this. Is there another key command that I'm missing that will terminate the python script and end rc.local?
Thanks.
EDIT: Another possible solution that would help me here is if there is a way to start a python script in the background during boot. So it would start the script and then allow login while continuing to run the script in the background.
I'm starting to think this isn't something that's possible to accomplish so other suggestions to accomplish something similar to what I'm trying to do would be helpful as well.
Thanks again.
Seems like it was just a dumb mistake on my part.
I realized the whole point of this was to allow the python script to run as a background process during boot so I added the " &" to the end of the script call like you would when running it from the shell and viola I can get to my password prompt by pressing "Enter".
I wanted to put this answer here just in case this would be something horribly wrong to do, but it accomplishes what I was looking for.
Making scripts run at boot time with Debian
Put your script in /etc/init.d/. So, if your script is in a file called my_script, it should be located at /etc/init.d/my_script.
Run update-rc.d my_script defaults as root.
Don't forget to make your script executable and include the shebang. That means the first line of the script should be #!/usr/bin/python.

Categories