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: [email protected] (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: [email protected] (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.
2 Answers 2
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– Ivan2020年11月23日 16:25:15 +00:00Commented Nov 23, 2020 at 16:25 -
@Ivan: Wrt 2nd comment: your script has the
#!/bin/bash
she-bang line, so you needn't invokesh
. Re no solution: What are you seeing in/tmp/log
? Can you post that in an Edit to your question?Seamus– Seamus2020年11月23日 20:25:28 +00:00Commented Nov 23, 2020 at 20:25 -
I already update the post with the log informationIvan– Ivan2020年11月23日 21:07:22 +00:00Commented Nov 23, 2020 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– Seamus2020年11月23日 21:32:03 +00:00Commented Nov 23, 2020 at 21:32
-
@Ivan And if that DOESN'T work - please consider Ingo's answer using
systemd
Seamus– Seamus2020年11月23日 21:41:15 +00:00Commented Nov 23, 2020 at 21:41
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 logIvan– Ivan2020年11月23日 23:21:21 +00:00Commented Nov 23, 2020 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– Ingo2020年11月24日 09:18:05 +00:00Commented Nov 24, 2020 at 9:18
sleep 3
to likesleep 10
? Maybe the X server didn't start in time...sleep
to cron doesn't seem to resolve the OP's issue.