I have an issue.
my project is about to view streaming video and control motors or sound by web page (on local IP). I am using Raspberry PI Web Camera Interface. My python scripts only run when click the button (The buttons are on the web page). My sound button works well when I run my script on thonny or terminal but it doesn't work on boot.
Here is my code; ses.py
#!/usr/bin/env python3
from gpiozero import Button
from signal import pause
import time,sys
import subprocess
button = Button(21) # This button is not button on the web page
def play_music():
subprocess.run(["mpg321"],"/home/pi/003.mp3"])
subprocess.run('raspi-gpio set 21 op dh',shell=True)
try:
button.when_pressed = play_music
pause()
except KeyboardInterrupt:
sys.exit()
By the way buttons which is on the web page run .sh file and this .sh file run the python scripts
I write sudo python3 /home/pi/ses.py & > /home/pi/Desktop/log.txt 2>&1 in /etc/rc.local to start on boot.
the log file is empty.
My web gui is here, i have no real button. the buttons what i write about is on this photo. a .sh file run When i click the Ses-2 button, and this .sh file run my ses.py
I write sudo python3 /home/pi/ses.py & > /home/pi/Desktop/log.txt 2>&1 in /etc/rc.local to start on boot.
I guess you did not get that memo, but rc.local has been deprecated for a few years now. I think you may still be able to use it, but it's probably more trouble than it's worth... but here's a guide to help you get started if you're determined to use it.
N.B. This answer uses the CLI/terminal, so if you're not comfortable with that, you can stop reading now.
Other than rc.local you have two other mainstream alternatives for starting your script:
create a systemd "unit"
use cron
cron is generally easier than systemd. I'm all about instant gratification, so let's use that:
a cron job is typically one single line in your crontab file. That line includes a schedule, and a command. cron runs your command at the scheduled time - pretty straightforward.
to create your cron job, you must edit your crontab file. This is done like so:
$ crontab -e
when you open your crontab for the first time the system will prompt you to select an editor. If you're unsure which one to use, choose nano (pico?)
if you need root privileges (and it seems you do) to run your Python script, you should open root's crontab; the only thing to remember w/ scripts run by cron is do not use sudo in your script.:
$ sudo crontab
let's set up the schedule :
In your crontab editor, enter the following at the bottom of the crontab... the #reboot schedule will cause your command to be executed each time your RPi is booted.
#reboot
now that you've completed the schedule, let's enter the command:
#reboot /usr/bin/python3 /home/pi/ses.py > /home/pi/Desktop/log.txt 2>&1
save your crontab & exit the editor; you are ready to test:
$ sudo reboot
Some other things:
cron has some shortcomings relative to systemd. One is that cron does not know whether or not all of the resources needed to execute your script are ready when it is started (cron is actually started by systemd, but that's a longer story).
If you see any evidence in your /home/pi/Desktop/log.txt file that a resource was unavailable when cron tried to run it, the solution is simple: sleep. Edit your cron job to sleep a while before running the command:
#reboot sleep 10; /usr/bin/python3 /home/pi/ses.py > /home/pi/Desktop/log.txt 2>&1
another "shortcoming" is that cron does not run your jobs in the same environment that you have in your login or interactive shell. This can cause some surprises, but a bit of diligence in using full path specifications will generally keep things on track.
finally, note that I have removed your & background invocation in your command. I did this because all cron jobs run in the background... however, I'm not familiar with your application so other adjustments may be required.
So - that's it - end of answer... look forward to hearing your feedback.
SOLVED
I solved the problem. I created a .desktop file with this code sudo nano /etc/xdg/autostart/ses.desktop then i wrote these lines;
[Desktop Entry]
Name=ses
Exec=/usr/bin/python3 /home/pi/ses.py
and
reboot
i have a file for my discord bot which i run on boot and now i also made a react.js app and i also want this to run on boot. But when i put:
cd /home/pi/kickzraptor-site && npm run start
also in my .bashrc file, only the first one gets run because its an infinite loop i think. How can i run both on startup? Thanks! (This is the line already at the bottom of my bashrc file)
echo Running bot.py scipt...
sudo python3 /home/pi/bot.py
Fastest way (not recommended) is to add & at the end of the command so that the program doesn't block for further processes. sudo python3 /home/pi/bot.py &
Recommended way is to create a systemd service that runs during or after the boot is completed (depending on its configuration). This method is also good for error handling and it provides more ability on the program.
I would like to run a python script directly on startup of the raspberry pi. I can execute the script by calling /home/pi/scripts/script.py. Now I tried to add the script to /etc/rc.local or /etc/profile or start it with systemd. For all cases the script is only executed after a connection to the pi via SSH and a login to the pi.
Therefore I would like to know who I could execute the script on boot (startup) without having to do a login?
You were on the right track with systemd and unit files.
First read this:
[https://www.digitalocean.com/community/tutorials/understanding-systemd-units-and-unit-files]
You probably used multi-user.target as wanted-by but that is too late.
You could use
sudo systemd-analyze critical-chain
to see which .targets might be better for your need.For example basic.target looks good.
You can of course also state in the unit file explicitly that your unit (.service) needs to be loaded before for example ifup#eth0.service
Use sudo systemctl list-units or sudo systemctl list-unit-files to see everything that in this respect is going on.
I'm using the pi_video_looper to loop videos on raspberry pi start. Because I want to use more raspberry pis, and change the videos some times, i changed the program so it would get the videos from a NAS server.
The problem is, that it woun't mount properly. So every time I restart the pi i have to stop the video looper from another computer (sudo supervisorctl stop video_looper), mount the pi again (sudo mount -a) and restart the video looper (cd /home/pi/pi_video_looper; sudo ./install.sh).
I tried to write a python script that would simply write the commands into terminal (with os.system). This almost works. The only thing is, that the install.sh doesn't work properly when I start it from "home". So I need a command that would do the same as cd /home/pi/pi_video_looper so I can execute install.sh directly.
Does anyone have any ideas?
I have really tried looking around but all the examples I find do not work for me.
I have a python script in a Raspberry Pi that starts using CRON....all good!
I would like to be able to debug the ouput that the script prints in screen from time to time so the "screen" command needs to be used to create a session and ensure that I can login and logoff using putty as needed.
I also add the "&" so it will run in background.
So currently I did:
1) Open cron
sudo crontab -e
2) Add the following line
#reboot python /home/pi/python/monitor.py &
This works correctly, I just need to know how to make a "screen" session open before launching the python script.
Thank you very much for your help!