need to run my python file when raspberry PI is turned on or plugged in. I'm using Raspberry Pi 3B+

sudo nano /etc/rc.local

this is not Working

# rc.local
 # This script is executed at the end of each multiuser runlevel.
 # Make sure that the script will "exit 0" on success or any other
 # value on error.
 # In order to enable or disable this script just change the execution
 # bits.
 # By default this script does nothing.
 # Print the IP address if it doesn't work ad sleep 30 before all your code 
 _IP=$(hostname -I) || true
 if [ "$_IP" ]; then
   printf "My IP address is %s\n" "$_IP"
   python /home/pi/Code/openCamera.py 
 exit 0
  • 1
    this is not Working - none of it? or just your pythin part of it? Dont' forget, running from rc.local you a) run as root, and b) home folder will be /root and c) current working folder will be ?? / maybe or /root ?? You need to make sure your environment is correct, i.e. user, home folder and current folder if your pythin expects the current working directory to be something specific – Jaromanda X Apr 1 at 9:01
  • personally, I'd be looking at creating a systemd service - much easier to have all that set up – Jaromanda X Apr 1 at 9:04
  • can you please guide me on this..@JaromandaX – user100864 Apr 1 at 9:12

here's how I'd do it

create a shell script in /home/pi - called openCamera.sh

cat > ~/openCamera.sh <<-EOF
cd ~/Code
python openCamera.py

make it executable

chmod u+x openCamera.sh

Create a service for this program

sudo cat > /etc/systemd/system/openCamera.service <<-EOF
Description=Open Camera Service



enable and start the service

sudo systemctl enable openCamera.service
sudo systemctl start openCamera.service
  • pi@raspberrypi:~ $ sudo systemctl enable openCamera.service Failed to enable unit: File openCamera.service: Invalid argument – user100864 Apr 1 at 9:44
  • really? cat /etc/systemd/system/openCamera.service does it look right? – Jaromanda X Apr 1 at 9:46
  • yes, I know it's an error, maybe you did it wrong – Jaromanda X Apr 1 at 9:47
  • what should we do know? – user100864 Apr 1 at 9:48
  • did you run all of the code in the code block after Create a service for this program – Jaromanda X Apr 1 at 9:48

An alternative to systemd that some find easier to use is cron. If you want your script to run at startup, try this:

  1. While logged in as user pi for example, open your crontab file using the nano editor ($ is the bash prompt):

$ crontab -e

  1. In the last line of the crontab file, add the following:
@reboot ( /bin/sleep 30; /usr/bin/python /home/pi/Code/openCamera.py > /home/pi/cronjoblog 2>&1)

Save your file (ctrlo), exit nano (ctrlx), then reboot (sudo reboot) your RPi.

A couple of notes re the above:
1. We sleep for 30 seconds after the system starts the cron daemon to give the system time to get everything else we need started. 30 seconds is an approximation; it might be less, occasionally it may be more (unlikely, but possible). You can experiment with this value to find one that works best for your system.
2. Any errors that are generated while starting your script will be written to a file in your (pi's) home directory: /home/pi/cronjoblog. Check the contents of this file if your script fails to start.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.