-1

R Pi 3 B+, Raspbian

I would like to run a Python script after the Pi has booted up.

There loads of posts about this like here & here & here. They either dont work or I just plain dont understand them.

To say I'm exasperated is an understatement. Why is such an obvious piece of functionality so cryptic?

From the only answers I could understand I tried the following...

I added the following to rc.local

exec > tmp/rc-local.log 2>&1

To troubleshoot rc.local then added my commands..

sudo pigpiod
/home/pi/moving/sudo python moving_test.py

Have actually tried a number of versions of that last line with & without sudo. With sudo I get

sudo: not found

without I get

python: not found

without sudo or python

/moving_test.py: Permission denied

EDIT:

I should mention that the line sudo pigpiod does execute & the pigio daemon does start.

What's going on here? I'm new to Linux so I wouldnt be surprised if I was missing something really obvious.

2

This works on my Pi3B+ with 'Raspbian Stretch with Desktop'.

I'm directing all output to templog.txt.

/etc/rc.local

#!/bin/sh -e
#
# 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
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

sudo pigpiod
python3 /home/pi/moving_test.py >> /home/pi/templog.txt 2>&1 &

exit 0
2

The three examples you posted are for running bash commands or setting configuration on boot. You want to do two things:

  1. Launch pigpiod
  2. Run your Python program

Launching pigpiod at boot is well documented. You should enable the service like so:

sudo systemctl enable pigpiod

Just do this once, from a terminal, and it will automatically be launched at boot every time.

To launch your Python program at boot, add it to crontab. Ideally you will launch it using the pi user, not with sudo. Are you sure you need to run it with sudo? The pigpiod requires sudo but its python client doesn't. Assuming you don't, open the Pi's crontab:

crontab -e

and add the following:

@reboot python /home/pi/moving_test.py &

(assuming you want Python 2 - but you should be using Python 3 really)

then save and exit. Reboot and it should be run when it boots.

1

Start pigpiod:

I began this answer before @ben_nuttall answered. After reading @ben's answer, I feel part of my answer (below) may still add something. However, I feel @ben's answer on starting the pigpiod service is the best and simplest. For completeness, here is @ben's answer for starting pigpiod:

sudo systemctl enable pigpiod  

Once pigpiod is started such that it becomes part of the normal boot sequence on your RPi, following is an approach for running your Python script at boot time.

Start Python script at boot time using cron:

There are several ways to do this. IMHO, the two "best" ways are 1) systemd, and 2) cron. The tradeoffs (again, IMHO) are that systemd is more comprehensive and technically superior, but more difficult (which only means that you'll need to invest more time learning before you can do useful things with it). I feel that cron is easier, so that's the answer I'll propose below:

  1. From the command line, type
    man cron    

Read the manual (man page) for cron. It's short, and you should have the background.

  1. "Jobs" in cron are scheduled from a file that's called the crontab. You'll edit this file to add your "job". There's also a manual for crontab if you care to read it (you should): man crontab

  2. Start the editor for crontab, and enter the instructions for starting your "job" on the command line of a terminal as follows:
    (NOTE: I've added the $ symbol at the beginning of an input as shorthand for the bash shell prompt)

    $ crontab -e  

Your default editor (likely nano) will start, displaying the default contents of your crontab. Move the cursor to the beginning of the first empty line at the bottom of the editor's display. Enter the following line:

@reboot /usr/bin/python3 /home/pi/moving/moving_test.py > /home/pi/cronjoblog 2>&1  

Save/Write the crontab (^O), then exit the editor (^X) to return to the shell/command line.

Note the use of complete file specifications in this entry. This is due to the fact that your crontab is not executed with the same $PATH that you will have as user pi. It's therefore a good habit to get into, even if it's not always strictly necessary. Note also the redirection of any stderr (error messages) to the file /home/pi/cronjoblog preserves this useful information for any post-op debugging that may be needed. Again, good habit.

  1. Reboot your RPi, and check if the Python script executed. If it did not, inspect the file /home/pi/cronjoblog for error messages that will help isolate the problem.

Occasionally, you will find that a job you've scheduled in your crontab will persistently fail to execute, and the cause will be unclear. The issue may be due to the fact that cron does not maintain awareness of whether programs and services have actually booted before it calls them. This is typically remedied by adding a brief sleep delay to give these services time to boot before they are called. The following adds a 10 second delay to the previous line:

@reboot /bin/sleep 10; /usr/bin/python3 /home/pi/moving/moving_test.py > /home/pi/cronjoblog 2>&1  

cron's lack of awareness is a shortcoming, and the primary reason that systemd is a technically superior approach. Nevertheless, cron remains useful and simple. Try this, and let us know how you get on via comments, or by editing your original question.

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.