how to keep a python script always running on raspberry pi? - python

I am making an image processing solution where the Raspberry Pi processes the image using a python script, and sends the result to an Arduino board. I want to keep this system always running, as Raspberry Pi has a Linux kernel, so is there any way to keep the python script on the pi always running?

It's not clear if the images will be stored on a folder or how your script will receive the images, but here are 2 options:
Setup a cron job to run your script every X seconds/minutes, process all images available and send them to the arduino board
Run your script in the background as a service, there are some cool instructions on the following link:
http://blog.scphillips.com/posts/2013/07/getting-a-python-script-to-run-in-the-background-as-a-service-on-boot/

If you keep terminal running forever, then you can simply use python's While condition
while True:
*do some thing*
and keep the program running as long as u need you can also use time module if you want it to sleep for few sec or mins

Related

How to close a program on a raspberry pi, in python

What code can be used to close a program running on RPI3?
I am running a
smart magic mirror using my Raspberry Pi, and I want to then close it. How can I?
It depends how you are accessing the Pi. If you are running it headless (without a separate monitor) then you need to use ssh to log in from another computer (ssh username#ip-address in the terminal window). You can then use other methods such as process killing to force the program to stop running on the Pi.
Alternatively, if you have access to the where you started the program on your Pi (maybe a window, or a terminal window where the Pi is running your mirror program, then you could exit or use a common interrupt such as Ctrl+C.

Keep program running on droplet

I've created a simple program in python3 with selenium to monitor a website. The program works as intended when I run it on my own computer, but as I need to keep it running 24/7, I've put it on a droplet from digital ocean. The program works when I am connected to via ssh, but from the log file I can tell that the program stops as soon as I close the connection. I'm guessing it has something that the problem is that the program is running on the same thread as the connection. My question is: How do I make sure the program runs in the background so it will keep running even after I close the coonection to the droplet?
Try using screen.
It allows you to run programs in separate windows so they run completely independent from each other, and also should keep your program running while not connected.
More info about screen: https://www.gnu.org/software/screen/

Is there a way to leave python code running when the computer is shutdown?

I have a python script that checks the temperature every 24 hours, is there a way to leave it running if I shut the computer down/log off.
Shutdown - no.
Logoff - potentially, yes.
If you want to the script to automatically start when you turn the computer back on, then you can add the script to your startup folder (Windows) or schedule the script (Windows tasks, cron job, systemd timer).
If you really want a temperature tracker that is permanently available, you can use a low-power solution like the Raspberry Pi rather than leaving your pc on.
The best way to accomplish this is to have your program run on some type of server that your computer can connect to. A server could be anything from a raspberry pi to an old disused computer or a web server or cloud server. You would have to build a program that can be accessed from your computer, and depending on the server and you would access it in a lot of different ways depending the way you build your program and your server.
Doing things this way means your script will always be able to check the temperature because it will be running on a system that stays on.
Scripts are unable to run while your computer is powered off. What operating system are you running? How are you collecting the temperature? It is hard to give much more help without this information.
One thing I might suggest is powering on the system remotely at a scheduled time, using another networked machine.
You can take a look at the following pages
http://www.wikihow.com/Automatically-Turn-on-a-Computer-at-a-Specified-Time
http://lifehacker.com/5831504/how-can-i-start-and-shut-down-my-computer-automatically-every-morning
Additionally once it turn on, you can perform a cronjob, for execute your python code by a console command >> python yourfile.py . What is the Windows version of cron?

Raspberry Pi python script in Sl state

i got a python script to collect data from a few connected sensors. This script runs in background from starting the PI. The script is working fine but irregular it fells in 'Sl' state. If i restart the PI it works again for a few days, but then it happens again.
Is there a way to monitor the state of the script (kill it and start it again if this happens) or any idea why this happen?
You have a few options (somewhat related):
Run your script as per normal, but have another script (bash works good) that checks for the state of your script. If it's stalled, kill it, and restart. This second script can be called from a regular cron job.
Change your python script into a linux service (see here for an example), and either monitor this service with a second script (similar to 1), OR, do a service restart at regular intervals with a cron job.

Run terminal command as startup reacts different from manually (linux raspberry pi)

Here's a short description of what I have:
I have to raspberry pi's in a local net work. I one of them I have a .py script named watchdog.py that starts a stream and then uses a sshpass command to the other pi to display the video stream.It also has some signaling LEDs a some push buttons for control
the problem is:
If I open a terminal and run the watchdog.py script in the GUI everything runs as it should be. So I thought of running it as a service as boot and installed upstart and made it run as a service (successfully I think). The thing is. If I boot the pi and then press the button to start the streams,they wont play on the other Pi, the LEDs ligh up and all the buttons work. And even the CPU load behaves the same way, but i still don't video nor audio. I have thought of trying automatically open a terminal (LXterminal) widow and run the python scrip on that window. but I didn't want the streaming raspberry pi also booting into gui (tough I guess I would mind if that makes the whole thing work).This little thing i making the whole project useless.
What are you using to play the streams? Depending on how you boot up the second Raspberry it might not have started some daemons for audio/video playback?!
You should (if you're not already doing) write a log (import logging ;)) and write a logfile which you can track for errors.
answer moved from OP's question itself:
I found a way that seems to work so far. instead of running the python script as a service I tried running it as cron job at reboot, and it worked. now it all works straight from reboot and I have Audio and video.

Categories

Resources