Raspberry pie startup self startup opencv program script and error analysis

1. Make test script

First, we need to make a script to test whether the self startup is effective.

[email protected] :~ $ nano /home/pi/start.sh

The contents of start.sh are as follows:

/usr/bin/python3 /home/pi/GeometryOperations.py

Here, you must pay attention to the use of absolute paths, otherwise there is a probability that the load will fail.

Then add an execution file to the script file

[email protected] :~ $ chmod 777 start.sh

Test the script function

[email protected] :~ $ ./start.sh

If you observe that the program is running normally, you can continue to the next step

2. Add self start

Modify rc.local file

[email protected] :~ $ sudo nano /etc/rc.local

Find exit 0 in the text. The code added before will be executed at startup. Add a line of code before exit 0:

su pi -c “exec /home/pi/start.sh &”

CTRL + O to save, Ctrl + X to exit and restart.

[email protected] :~ $ sudo reboot

After restart, the program can run.


If the raspberry pie does not respond after restart, you can check the status of RC local through systemctl

[email protected] :~ $ sudo systemctl status rc-local

After you go in, you can see the status of each step after you start up and check where the error will be reported.

4. Possible error reports

Process:454 ExecStart=/etc/rc.local start (code=exited,status=1/FAILURE)

This error is probably due to the script file
if you just start, you must write the file path as an absolute path, otherwise an error will be reported.

error: (-2:Unspecified error) Can’t initialize GTK backend in function ‘cvInitSystem’

This error will be reported if the self startup program contains code with window operation such as OpenCV

The reason is caused by the following sentence

su pi -c “exec /home/pi/start.sh &”

If your command needs to run for a long time (such as an endless loop) or cannot exit after running, you must ensure that the “& amp;” symbol is added at the end of the command to make the command run in its background

If the code contains window interface operations similar to the following

cv2.imshow(‘Cap’, frame)

It will conflict with the background operation, so that the back end of GTK cannot be initialized and an error will be caused
the solution is to delete all the operations related to the interface and window, and the program can run well!

Read More: