0

I have a raspberry pi 4, EDIT: using python3.7, with raspbian OS Devian 10 Buster and I am trying to run a script afeterboot for people counting, it uses a virtual env, after reading various post this is the bash on_reboot.sh I am using:

#!/bin/bash
sleep 3
echo export DISPLAY=:0
source /home/pi/.virtualenvs/cv/bin/activate

python /usr/pi_reboot/contador_final_v2.py -p /usr/pi_reboot/mobilenet_ssd/MobileNetSSD_deploy.prototxt -m /$

After aplying chmod +x I try it and runs perfect, I am using crontab -e for running after boot:

@reboot bash /usr/pi_reboot/on_reboot.sh >>/tmp/log 2>&1

In the log I got the next message: Gtk-WARNING date : cannot open display:

Yes, the script uses a window to show the video, what could I do to fix it? I have put the export DISPLAY=:0 in the bash, but doesn't work. Is crontab the best way to do it? also tried it in rc.local but again, no success.

Edit1: The log I get everytime I turn on the Raspberry is:

export DISPLAY=:0  
Unable to init server: No se pudo conectar: Conexión rehusada
(Frame:886): Gtk-WARNING **: 15:17:23.228: cannot open display: 

Edit2:

Applied Ingo alternative, got this

nov 23 18:08:28 raspberrypi rc.local[476]:   File "/usr/pi_reboot/pyimagesearch/centroidtracker.py", line 2, in <module>
nov 23 18:08:28 raspberrypi rc.local[476]:     from scipy.spatial import distance as dist
nov 23 18:08:28 raspberrypi rc.local[476]: ImportError: No module named scipy.spatial
nov 23 18:08:28 raspberrypi sudo[483]: pam_unix(sudo:session): session closed for user root
nov 23 18:08:30 raspberrypi systemd[1]: systemd-rfkill.service: Succeeded.
nov 23 18:08:32 raspberrypi dhcpcd[456]: wlan0: leased 192.168.1.18 for 86400 seconds
nov 23 18:08:32 raspberrypi avahi-daemon[385]: Joining mDNS multicast group on interface wlan0.IPv4 with address 192.168.1.18.
nov 23 18:08:32 raspberrypi avahi-daemon[385]: New relevant interface wlan0.IPv4 for mDNS.
nov 23 18:08:32 raspberrypi avahi-daemon[385]: Registering new address record for 192.168.1.18 on wlan0.IPv4.
nov 23 18:08:32 raspberrypi dhcpcd[456]: wlan0: adding route to 192.168.1.0/24
nov 23 18:08:32 raspberrypi dhcpcd[456]: wlan0: adding default route via 192.168.1.1
nov 23 18:08:36 raspberrypi CRON[377]: pam_unix(cron:session): session closed for user pi
nov 23 18:08:39 raspberrypi dhcpcd[456]: wlan0: no IPv6 Routers available
nov 23 18:08:39 raspberrypi vncserver-x11[489]: HostedRendezvous: Connection to [EaNEhY-gjcSg-KAyMLF] is from user "<my_email
nov 23 18:08:39 raspberrypi vncserver-x11[489]: Connections: connected: my_email@hotmail.com (desde my_ip::port)
nov 23 18:08:39 raspberrypi vncserver-x11[489]: session started: user pi permissions f
nov 23 18:08:39 raspberrypi vncserver-x11[489]: Connections: authenticated: my_email@hotmail.com (desde my_ip::port), as 
nov 23 18:08:40 raspberrypi vncserver-x11[489]: SPrintConnMgr: Failed to add printer: server-error-service-unavailable
nov 23 18:08:47 raspberrypi systemd[1]: systemd-fsckd.service: Succeeded.
nov 23 18:09:03 raspberrypi systemd-timesyncd[321]: Synchronized to time server for the first time 181.224.249.90:123 (2.debian.poo
nov 23 18:09:03 raspberrypi CRON[378]: (CRON) info (No MTA installed, discarding output)
nov 23 18:09:03 raspberrypi CRON[378]: pam_unix(cron:session): session closed for user root
nov 23 18:09:11 raspberrypi systemd[1]: systemd-hostnamed.service: Succeeded.

what seem curious is at the start, the error in:

from scipy.spatial import distance as dist.
ImportError: No module name scipy.spatial

When I run the bash alone got no problem with the library.

  • Maybe you could increase the delay from sleep 3 to like sleep 10? Maybe the X server didn't start in time... – Unsigned_Arduino Nov 18 '20 at 12:47
  • What does "last raspbian OS" mean if I read this in a year? You should use a systemd Unit file to create a service for the program that starts after graphical.target. You can find examples here on this site. – Ingo Nov 19 '20 at 8:44
  • Oh, my bad, I will edit the raspbian version – Ivan Nov 23 '20 at 16:20
  • @Ingo: Wrt a systemd unit file... I'm not sure I'm seeing a good example. Could you help us narrow that down - or perhaps answer this question yourself as adding a long sleep to cron doesn't seem to resolve the OP's issue. – Seamus Nov 23 '20 at 20:28
  • @Seamus Done. But I'm not sure if it is working with the sourced environment and with unsupported python2. – Ingo Nov 23 '20 at 21:35
1

The typical fix for this is to add sleep to your cron item instead of in your script. It may not make any difference, but it's easy enough to try:

@reboot sleep 30; /usr/pi_reboot/on_reboot.sh >> /tmp/log 2>&1

The Gtk warning suggests that it simply was not available at the stage in the boot process when cron tried to run your script.

EDIT:

There may also be an issue with the PATH. This is caused by the fact that your cron jobs do not run in the same ENVIRONMENT that you have in your login shell. To overcome that, you can add a PATH statement in your crontab, or in your script. To add it in your crontab file, you can do this:

STEP 1:

Determine the PATH in your login shell (the one you run the script from & it works):

$ echo $PATH
/home/pi/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games

STEP 2:

Edit your crontab file: Copy & Paste your echo $PATH result (your result - not mine) into your crontab:

PATH=/home/pi/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/local/games:/usr/games

Better than this shotgun approach would be to locate where the GTk binaries reside, and add only that directory. But this should work for now - you may refine it later if you wish.

  • Already try what you suggest, still no solution, also, I think you miss to put sh after the ' ; ' – Ivan Nov 23 '20 at 16:25
  • @Ivan: Wrt 2nd comment: your script has the #!/bin/bash she-bang line, so you needn't invoke sh. Re no solution: What are you seeing in /tmp/log? Can you post that in an Edit to your question? – Seamus Nov 23 '20 at 20:25
  • I already update the post with the log information – Ivan Nov 23 '20 at 21:07
  • @Ivan: Sorry - I thought surely there must be more than that. The problem must be the ENVIRONMENT... I've edited my answer to show how to change that. Let us know how this works. – Seamus Nov 23 '20 at 21:32
  • @Ivan And if that DOESN'T work - please consider Ingo's answer using systemd – Seamus Nov 23 '20 at 21:41
1

First of all: if you want to run Python 3 scripts, then you must call python3. On default Raspberry Pi OS, calling python, uses version 2, which isn't able to execute python 3 scripts. Please correct the bash script with the python call.

I assume it is running from the command line when logged in as user pi. Try to use a systemd service. Create a Unit file for it with:

rpi ~$ sudo systemctl edit --force --full people-counter.service

In the empty editor insert these statements, save them and quit the editor:

[Unit]
Description=People Counter
After=graphical.target

[Service]
User=pi
WorkingDirectory=/home/pi
Environment=DISPLAY=:0
ExecStart=/bin/bash -c 'source .virtualenvs/cv/bin/activate; /usr/bin/python3 /usr/pi_reboot/contador_final_v2.py -p /usr/pi_reboot/mobilenet_ssd/MobileNetSSD_deploy.prototxt -m /$'

[Install]
WantedBy=graphical.target

Enable the new service with:

rpi ~$ sudo systemctl enable people-counter.service

and reboot. You can find error messages in the journal:

rpi ~$ journalctl -b -e

I'm not sure if this is working with unsupported python, version 2, and with the sourced environment. I don't know something about it.

  • Hello Ingo, already try what you suggest, re edit post with log – Ivan Nov 23 '20 at 23:21
  • @Ivan Sorry, but you really do not follow my example. The error messages are from rc.local[476]:. Please take note that using /etc/rc.local has limitations due to Compatibility with SysV. We have seen many problems here on this site using it. Following the recommendation of the developers from systemd you should avoid using it. And that is exactly what I suggested: use the Unit file as given in my answer! – Ingo Nov 24 '20 at 9:18

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.