Search code examples
linuxshellcronraspberry-pi4raspberry-pi-os

Playing a video on Raspberry Pi OS startup


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!


Solution

  • 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.