Rasberry PI Crontab - ERROR during Auto Start Python Computer Vision (Camera) Application - GDK Critical, GDK warnings, X server error

Challenge :
Running a python script at Raspberry reboot automatically gives errors , especially in Computer vision applications where the camera startup is needed and OpenCV is being used.

Pre-requisite :
A minimal knowledge of cron tab is needed. You can find many tutorials and also information in raspberry official site about using crontab. using "crontab -e" or  "sudo crontab -e" based on your settings and need configured in boot settings (sudo raspi-config).

What will you see in this blog :
I faced couple of errors while running the Computer vision (using web camera/ USB camera) python programs on Raspberry PI reboot, I will share those errors, mistakes and solutions.
Below script will help you to get rid of errors like GDK Critical, GDK warnings, X server error

Approach Taken 
  • Write a bash script correctly by setting the needed paths, and X server settings, handling MATPLOT lib issues.
  • Add the reboot directive in crontab with sleep ( you can see this on many other blogs too)
  • Check for common mistakes.

Step 1 : Writing the script to run the python file from inside it.
The script should be as below (lets say the scriptname is cam1start.sh)
1. export display =:0 will solve X server errors
2. MPLBACKEND=Agg will resolve MATPLOT lib related display errors
3. Put the file name and python or python3 and your file as in the last line below.







Step2 : Cron tab changes
As you find in many blogs, open the relevant crontab (based on your user settings on logon)
in my case it was "crontab -e", then add the @reboot directive followed by sleep ( to wait until all networks/interfaces are up and ready)
It should look as below











Step3 : Common mistakes and precautions
  • Do not forget to add a bootlog file as added above, so that you can see if there are some other errors.
  • Do not forget to put two times "Enter" or next line in the crontab file. As it looks for nextline character to read a command.
  • If your python file is using some path variables, make sure they are absolute paths or correctly pointing using "os" module.

Comments