1

I am having trouble getting what seems like a simple task to work. I have created a python file that I want to begin on startup. I have followed the steps for rc.local and init.d found here.

To test this out I have made a practice python script to play a video. But the ultimate goal is have a modified version of this program begin on startup using a PIR sensor. The practice script is just to figure out how to get it working.

Here is my practice script:

#! usr/bin/python3

from omxplayer.player import OMXPlayer
from pathlib import Path
from time import sleep

VIDEO_PATH = Path("Videos/Scott_eye.mp4")

player = OMXPlayer(VIDEO_PATH, args=['--no-osd', '--no-keys', '-b'])

Here is what I added to rc.local just before exit 0:

sudo python3 /home/pi/videotest.py &

I also followed the steps for init.d from the link above.

I tested the practice script out in the command line by typing python3 /Videos/videotest.py to confirm it works.

Any help would be appreciated.

3
  • take the video player out of the equation ... log a system message instead ... the graphic subsystem may not be running when your script executes, so the video player will not start
    – jsotola
    Mar 12 at 17:03
  • That link is one of the worst collections of obsolete, incomplete and JUST PLAIN WRONG hints I have seen.
    – Milliways
    Mar 12 at 22:21
  • systemd would be the best way to go. You might find the follow article helpful: blog.usedbytes.com/2019/11/run-at-startup-without-rc.local
    – ukBaz
    Mar 14 at 21:23
4

As frequently stated here (and other places) rc.local has been deprecated in Linux, and it is not recommended for use. Numerous problems in the use of rc.local have been reported here; you should consider another approach.

In general, there are two approaches: systemd and cron. This answer shows the cron approach.

Your question shows the following : sudo python3 /home/pi/videotest.py &. Based on this I'll assume that root privileges are, or will be, required to run your script. If you find they are not required, you shouldn't use them - but more on that below:

1. Create/modify the crontab file:

We'll use "root's crontab" since you've indicated your script requires root privileges. From the command line:

sudo crontab -e

This will open root's crontab for editing. Before doing so, you may be prompted to designate which editor to use. If prompted to select an editor, choose nano unless you're familiar with another one in the list.

If you have determined that root privileges are unnecessary, open your (user pi) crontab as follows:

crontab -e

2. Edit the crontab to start your script at boot time

You should now be in your editor with a view of the current version of the crontab. Add the following line to the bottom (last line) of the file, then save and exit the editor:

@reboot /bin/sleep 10; /usr/bin/python3 /home/pi/videotest.py >> /home/pi/mycronlog.txt 2>&1

Here's what happens:

  • at boot time, cron tries to execute this job

  • cron will sleep for 10 seconds - this gives the system time to get its resources started

  • after 10 seconds, cron will call python3 to run your script located at /home/pi/videotest.py

  • any output from your script is re-directed to the "log" file at /home/pi/mycronlog.txt; 2>&1 ensures that stdout and stderr both go to the designated "log" file.

3. reboot and test

Check the contents of your "log" file at /home/pi/mycronlog.txt for any error messages the script may have thrown, and adjust fire as necessary. Let us know if you have other questions, or something you don't understand.

3
  • Isn't there @boot?
    – manarinian
    Mar 17 at 7:29
  • so your solution may not work
    – manarinian
    Mar 17 at 8:07
  • And that means it will omly run when you reboot not on startup
    – manarinian
    Mar 17 at 8:10
1

Make sure your operanting system (verison would be helpful) inited by sysvinit (/etc/init.d scripts). Newer systems usually use systemd to manage services. rc.local is supported by systemd and may be enabled via following command:

sudo systemctl enable rc-local.service
2
  • Thanks. I'm currently running Raspbian GNU/Linux 10 (buster). When I ran your suggestion it stated "The unit files have no installation config ... This means they are not meant to be enabled using systemctl."
    – Scott Goss
    Mar 12 at 16:05
  • 1
    On this site, you can assume it's Raspberry Pi OS (Raspbian) if the OS is not specified. It's been using systemd for several years now. Mar 17 at 12:18
1

Warning DO NOT DO THIS

  1. sudo nano .bashrc
  2. Go to the bottom of the .bashrc file and include the path of the python script and save the changes & exit
  3. Reboot the raspberry-pi and your script starts as soon as the Raspberry pi turn's on
2
  • 1
    .bashrcis NOT for running scripts and DOES NOT run on startup
    – Milliways
    Mar 18 at 6:28
  • It does run on the start-up because it's a service....I run my python scripts following the same protocol, maybe you should re-check yourself & come back again later.
    – Krishna
    Mar 18 at 8:59
0

The problem may be that you need the desktop up and running BEFORE launching a video file. I've had a heck of a time getting an audio program to autolaunch for this reason (Pulseaudio only loads when the desktop does).

Try creating a file /home/pi/.config/autostart/[name].desktop with the following content:

[Desktop Entry]
Type=Application
Name=[call_it_something]
Exec= python3 /home/pi/videotest.py

If that doesn't work, to see an error log, install a different terminal:

sudo apt-get install xterm -y

Then, in the .desktop file, change the Exec line to:

Exec=xterm -hold -e 'python3 /home/pi/videotest.py'

(Note the apostrophes that were also added). Then when you reboot, you should get a terminal window showing any i/o and also any error messages.

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.