1

I have decided to make a radio from my old laptop. I installed Puppy linux and set special features, like: after waking up, it turns on my favorite online radio (openFM) and switches off the screen.

Sometimes it loses the connection to the internet and the music stops, but when the Laptop reconnects to the internet, the music doesn't start again, I have to restart the program (openFM).

I want to write a script which checks if music is currently playing and, when there is no music playing, it should restart openFM.

I don't know how to detect if music is playing or not. I tried:

/proc/asound/card*/pcm*/sub*/status

but it always contains:

state: RUNNING

Do you have any other ideas?

  • 3
    Why not detect when the internet connection is dropped and write a script that relaunches openFM each time the internet is down? – terdon May 26 '16 at 15:07
0

I once found a script (I don't remember where all I have is my bash_history).

This is not the solution, it's a workaround. I know it's not exactly what you asked for but it may be useful.

It uses the microphone to detect silence.

This is my script:

while :;
    do
    rec -t raw /dev/null rate 32k silence 1 0.1 4% 1 1.0 15%;
    clementine -t;
    sleep 1;
done
  • The 1.0 represents the silence time (1 second) change it to something longer to make sure it's not a music pause.
  • The 15% represents the silence threshold use smaller values for quiet places, higher values for noisy places.
  • clementine -t is the command I use to toggle my music player state aka clementine, change this with the command you use to restart openFM.
  • sleep 1: make a small delay to give things some time.

You can read more here: http://linux.die.net/man/1/rec and here: End sox recording once silence is detected

You can also just play making pings to a host you know that will always be online until it fails, then keep trying more pings until it works again. at this point you know the connection was gone but is back again so you should restart openFM. This can be done with 2 loops inside a third one.

#!/bin/bash
while :;
    do

    while ping -c 1 8.8.8.8; # do we have connection?
        do
        sleep 5;
    done
    # the connection seems to be gone...

    until ping -c 1 8.8.8.8; # do we have connection?
        do
        sleep 5;
    done
    # the connection seems to be back again...

    # restart here your service

done

Note the -c 1 means make only one ping (then we call it again). It doesn't have to be google's DNS who you ping to, change the sleep times as you like.

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.