1

I need to run a script to play a video when I turn on my Raspberry Pi 4. I'm using crontab to run my script which opens a video with mpv. When I run the script normally, it works fine and the video is being played. The problem is, when i boot the raspberry, the script automatically runs but mpv doesn't .

here is my script:

#!/bin/bash
VIDEOPATH="/home/pi/Desktop/my-movie.mkv"
SERVICE="mpv"


while true; do 
  echo "playing "+$VIDEOPATH
  $SERVICE --fs --start=00:00:00 $VIDEOPATH
done

I added this line to crontab:

@reboot /home/pi/Desktop/my-script.sh

I'm totally stuck in this. Any help saves my life!

Ghonche Yqr
  • 305
  • 1
  • 10
  • `When I turn on my Raspberry Pi 4` When exactly? After your desktop environment has loaded? If so, my guess is that a better approach would be making a simple `systemd` service instead with appropriate `After`/`Requires` dependencies on whatever service launches your desktop environment. – parttimeturtle Aug 18 '21 at 22:51
  • Yes. That's exactly what i meant. Thanks for the suggestion, I tried systemd but when i start the service manually, I expect the video to play, but nothing happens. – Ghonche Yqr Aug 22 '21 at 07:05

2 Answers2

2

The most practical solution I found to run GUI programs on startup is using Autostart. I created a .desktop file at /etc/xdg/autostart directory:

sudo nano /etc/xdg/autostart/display.desktop

when display would be a custom name for my script. I added following lines the display.desktop :

[Desktop Entry]
Name=Play a video
Exec=mpv --fs --start=00:00:00 path-to-my-video

Saved the file and reboot the Pi.

sudo reboot

As soon as my Pi boots up, my GUI program automatically start as well.

Ghonche Yqr
  • 305
  • 1
  • 10
0

Please update your script:

#!/bin/bash
source ~/.bash_profile
VIDEOPATH="/home/pi/Desktop/my-movie.mkv"
SERVICE="mpv"


while true; do 
  echo "playing "+$VIDEOPATH
  $SERVICE --fs --start=00:00:00 $VIDEOPATH
done
Dudi Boy
  • 4,551
  • 1
  • 15
  • 30