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.
Related
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 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.
Note: I'm new to everything so bare with me.
I'm using a RPi 4B with Buster. My goal is to automatically run 2 python scripts at the same time when the pi first boots up. Both scripts are in a virtual environment. The first script is called sensor.py which basically uses an ultrasonic distance sensor to continuously calculate distances between the sensor and an object. The other is an object recognition script from Tensorflow Lite called TFLite_detection_webcam.py that identifies objects from a camera feed. I can't use rc.local for autorunning because the object recognition script uses a picamera feed as an input, which rc.local doesn't support. So my preferred option is using autostart. I was able to successfully get the sensor.py script to autorun by issuing this in the terminal: sudo nano /etc/xdg/lxsession/LXDE-pi/autostart and adding this to it: /home/pi/tflite1/tflite1-env/bin/python3 /home/pi/tflite1/sensor.py. In this case, tflite1-env is the virtual environment being activated. However, I don't know how to get the second script to run. To run it regularly, I would issue the following into the terminal and the camera feed would pop up on the screen as a window.
cd tflite1
source tflite1-env/bin/activate
python3 TFLite_detection_webcam.py --modeldir=TFLite_model
I've tried to get this script to run by adding this to the autostart file: /home/pi/tflite1/tflite1-env/bin/python3 /home/pi/tflite1/TFLite_detection_webcam.py --modeldir=TFLite_model but it doesn't seem to be working. I've tried to run it using shell files, but every time that I run a shell file in the autostart file such as adding ./launch.sh to the bottom, nothing happens. Any help getting the second script to run at the same time as the first upon startup would be greatly appreciated. Thanks in advance.
Use Systemd. Set up Systemd unit files in /etc/systemd/system, e.g.
kitkats-sensor.unit
[Unit]
After=network.target
[Service]
ExecStart=/home/pi/tflite1/tflite1-env/bin/python3 /home/pi/tflite1/sensor.py
WorkingDirectory=/home/pi/tflite1/
User=pi
Group=pi
kitkats-tflite.unit
[Unit]
After=network.target
[Service]
ExecStart=/home/pi/tflite1/tflite1-env/bin/python3 /home/pi/tflite1/TFLite_detection_webcam.py --modeldir=TFLite_model
WorkingDirectory=/home/pi/tflite1/
User=pi
Group=pi
Then enable the unit files with systemctl enable kitkats-tflite and systemctl enable kitkats-sensor (to have them autostart) and systemctl start kitkats-tflite (and sensor) to start them right away.
You can then see them in e.g. systemctl, and their logs are diverted to journalctl.
I'm relatively new to raspberry pi (5 days using it) and I've just finished to run my python script succesfully (called dogcare.py). Now I'm trying to execute this script right after my raspberry is turned on. I've been doing some research and I find different ways to do it:
using /etc/profile
using /etc/rc.local
using crontab
using /etc/init.d
using systemd
But none of these ways are working for me.
Setup enviroment:
Hardware: RaspberryPi 2 Model B
Software: Raspbian or NOOBs (not sure)
Context:
Since for my project I need to run meet.jit.si, I followed this guide http://www.instructables.com/id/Video-Calling-on-Raspberry-Pi-3/?ALLSTEPS and It has a step where sets chromium website to start right after RPi is turned on. (Currently this is working fine)
My python script is using request library in order to use HTTP GET with an external website application I've been working on.
Main problem:
I need to run both events: chromium website with meet.jit.si and my python script when my raspberry is turned on.
Current situation: chromium website is running after my RPi is turned on but my script doesn't.
I'd appreciate any help !
I have done a similar thing with my Raspi 2 as well which involved sending myself an email with the ip address of the pi so I could easily ssh/vnc to it.
My steps involved making a shell script which ran the python program.
#!/bin/sh
cd pythonfiledirectory
sudo python pythonfile.py
cd /
Then I made it executable with the following command:
chmod 777 file.sh
Now edit your crontab to run the file on startup.
In your terminal, type:
sudo crontab -e
Inside of the crontab write:
#reboot sh file.sh
You could add a log file if you wanted to debug and see why it's not working by making a log directory and changing the text you wrote in the crontab to:
#reboot sh file.sh >/logdirectoy/ 2>&1
This is what made it work for me and if it doesn't work try and make sure you can run your .sh file and try the crontab with some other files to debug the problem.
I have 3-4 python scripts which I want to run continuously on a remote server(which i am accessing through ssh login).The script might crash due to exceptions(handling all the exceptions is not practical now), and in such cases i want to immediately restart the script.
What i have done till now.
I have written a .conf file in /etc/init
chdir /home/user/Desktop/myFolder
exec python myFile.py
respawn
This seemed to work fine for about 4 hours and then it stopped working and i could not start the .conf file.
Suggest changes to this or i am also open to a new approach
Easiest way to do it - run in infinite bash loop in screen. Also it's the worst way to do it:
screen -S sessionName bash -c 'while true; python myFile.py ; done'
You can also use http://supervisord.org/ or daemon by writing init.d script for it http://www.linux.com/learn/tutorials/442412-managing-linux-daemons-with-init-scripts
If your script is running on an Ubuntu machine you have the very convenient Upstart, http://upstart.ubuntu.com/
Upstart does a great job running services on boot and respawning a process that died.