0

I've written a script that first checks whether a local network stream is up, then if this is the case it will start vlc. It will then keep monitoring the stream, if it goes down it will close vlc and wait for the stream to return before restarting vlc (see below).

If I run the script manually this works.

I'd like this script to run automatically on startup (using Raspbian). The issue I seem to be having is that regardless of how I get the script to run on startup I see that it's briefly connecting to the network stream but it isn't opening vlc (at least not visibly on the desktop).

Any suggestions gratefully appreciated.

#!/bin/bash

export DISPLAY=:0

# Remove previous test stream grab
rm /home/pi/snapshot.png

myip="192.168.0.67"

STATUS=0
while [  $STATUS == 0 ]; do

    # Test stream by taking image grab
    ffmpeg -i rtsp://$myip:8080/h264_ulaw.sdp -frames 1 /home/pi/snapshot.png

    # Check to see if image grab has worked
    file="/home/pi/snapshot.png"
    if [ -f "$file" ]
    then
        echo "$file found."
        # Start stream if up
        vlc rtsp://$myip:8080/h264_ulaw.sdp --fullscreen &

        STATUS_INNER=0
        while [  $STATUS_INNER == 0 ]; do
            rm /home/pi/snapshot.png
            ffmpeg -i rtsp://$myip:8080/h264_ulaw.sdp -frames 1 /home/pi/snapshot.png
            if [ -f "$file" ]
            then
                # Do nothing
                echo "Keep playing."
                sleep 10
            else
                echo "Stream stopped, trying to restart."
                pkill vlc
                STATUS_INNER=1
            fi
        done
        # STATUS=1
    else
        echo "$file not found."
        sleep 5
    fi
done

Currently trying to run using /etc/rc.local as below

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

exec 1>/tmp/rc.local.log 2>&1  # send stdout and stderr from rc.local to a log file
set -x                         # tell sh to display commands before execution

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

/home/pi/./vlc_autorun.sh

exit 0
12
  • 1
    How are you running this on startup?
    – Fred
    Commented Oct 6, 2019 at 8:27
  • I tried a few ways: adding to init.d, editing /home/pi/.bashrc and editing /etc/rc.local (no guarantee that I did these correctly).
    – llionevans
    Commented Oct 7, 2019 at 16:39
  • Try adding export DISPLAY=:0 to the start of the script. Try launching the script from /etc/rc.local with the command sudo -u pi bash myscript.sh.
    – Fred
    Commented Oct 7, 2019 at 20:02
  • Ok, I've updated the question above with the most up to date script and a copy of /etc/rc.local. I had noticed that when I was doing the screengrab it had a relative path and the check to see if screengrab had worked was absolute. But even with this fixed it still doesn't work. I can see that snapshot.png is being created and removed over and over, so the script is running.
    – llionevans
    Commented Oct 7, 2019 at 21:32
  • raspberrypi.org/forums/viewtopic.php?t=159544 suggests starting the script in a screen session to catch any output. pi.bek.no/internetRadio suggests that VLC will refuse to run as root, and currently your rc.local script does run as root. Try changing /home/pi/./vlc_autorun.sh to sudo -u pi bash /home/pi/./vlc_autorun.sh.
    – Fred
    Commented Oct 7, 2019 at 21:38

1 Answer 1

0

It now works! I'm not really sure which change solved it, the only changes in the scripts below are minor tweaks that shouldn't actually change anything to that which I posted last time.

The only thing I can imagine is that when I first tried changing the command in /etc/rc.local to sudo -u pi bash /home/pi/./vlc_autorun.sh that this coincided with when the stream server crashed and this was in fact the solution i needed?

In any case, massive thanks to Fred for the pointers that got me on the right path. This should now work as a completely unmanned client which loads up a network stream if it's available, waits and periodically checks if it isn't available, and gracefully tries to re-establish a connection if it loses the feed at any point. Hope it's a help to someone else.

/home/pi/vlc_autorun.sh

#!/bin/bash

export DISPLAY=:0

# Remove previous test stream grab
rm /home/pi/snapshot.png

myip="192.168.0.67"

STATUS=0
STATUS_INNER=0
while [  $STATUS == 0 ]; do

# Test stream by taking image grab
ffmpeg -i rtsp://$myip:8080/h264_ulaw.sdp -frames 1 /home/pi/snapshot.png

# Check to see if image grab has worked
file="/home/pi/snapshot.png"
if [ -f "$file" ]
then
    echo "$file found."
    # Start stream if up
    vlc rtsp://$myip:8080/h264_ulaw.sdp --fullscreen &

    STATUS_INNER=0
    while [  $STATUS_INNER == 0 ]; do
        rm /home/pi/snapshot.png
        ffmpeg -i rtsp://$myip:8080/h264_ulaw.sdp -frames 1 /home/pi/snapshot.png
        if [ -f "$file" ]
        then
            # Do nothing
            echo "Keep playing."
            sleep 10
        else
            echo "Stream stopped, trying to restart."
            pkill vlc
            STATUS_INNER=1
        fi
    done
    # STATUS=1
else
    echo "$file not found."
    sleep 5
fi
done

/etc/rc.local

#!/bin/sh -e
#
# rc.local
#
# This script is executed at the end of each multiuser runlevel.
# Make sure that the script will "exit 0" on success or any other
# value on error.
#
# In order to enable or disable this script just change the execution
# bits.
#
# By default this script does nothing.

exec 1>/tmp/rc.local.log 2>&1  # send stdout and stderr from rc.local to a log file
set -x                         # tell sh to display commands before execution

# Print the IP address
_IP=$(hostname -I) || true
if [ "$_IP" ]; then
  printf "My IP address is %s\n" "$_IP"
fi

#/home/pi/./vlc_autorun.sh
#lxterminal -e "/home/pi/./vlc_autorun.sh"
sudo -u pi bash /home/pi/./vlc_autorun.sh

exit 0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.