You are not logged in.
HI,
I listen to internet radio by using mplayer wit the following script (stubru1)
#!/bin/bash
mplayer http://mp3.streampower.be/stubru-high.mp3 -cache 1024My connection (wireless) is not very stable, and sometimes the cache empties and the radio stops playing. What I want to do is that mplayer automatically restarts when that happens. Basically I want to start the same bash script again when it fails. I have been googling, found a link (http://stackoverflow.com/questions/6968 … if-it-dies), and then created the following script (stubru2)
#!/bin/bash
until stubru1; do
echo " Respawning.." >&2
sleep 1
doneHowever, I tested this with my microwave (which kills my wireless
), and it doesn't work. The cache becomes empty, and then mplayer quits with the message "Exiting... (End of file)". This is apparently not an exit code 2.
Any ideas how to go further? Any ideas how to restart a script after mplayer exits with the message "Exiting... (End of file)"?
Thanks!
Rogier
Last edited by anadyr (2014-02-28 19:10:28)
Offline
That's a strange "until" line. Would be simple to use instead:
while true; doOffline
Check what the exit code actually is. Run the mplayer command in a terminal, make a hot pocket (e.g. use the microwave), then when mplayer quits enter `echo $?` to see the exit code it gives when that happens.
Then use that exit code in your script. There is no need for two scripts:
while [[ $(mplayer http://whatever.whatever -flags >/dev/null 2>&1) -eq ## ]]; do sleep 1; donePut the exit code number you recieve from the test in place of the ##.
EDIT: if you really want to see all of mplayers output for any reason, you could revise it slightly:
ret=0
while [[ $ret -ne ## ]]; do
mplayer http://whatever.wherever -flags
ret=$?
sleep 1
doneLast edited by Trilby (2014-02-27 14:11:51)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Thanks for the answer.
However, "Exiting... (End of file)" gives exit code is 0. Manually closing mplayer also gives exit code 0. This would mean, in theory, that I cannot close mplayer, because when I do, it restarts? In practice, however, the script that you suggest fails. As for bash scripting, I am a newbie...
#!/bin/bash
while [[ $(mplayer http://mp3.streampower.be/stubru-high.mp3 >/dev/null 2>&1) -eq 0]]; do sleep 1; done./stubru2: line 2: syntax error in conditional expression: unexpected token `;'
./stubru2: line 2: syntax error near `;'
./stubru2: line 2: `while [[ $(mplayer http://mp3.streampower.be/stubru-high.mp3 >/dev/null 2>&1) -eq 0]]; do sleep 1; done'
Offline
Sorry, there should be a space between the number and the closing brackets.
If the exit code is zero, you'd have to be a bit more creative. Run in a loop, but to stop mplayer, you'd first have to kill the script (so mplayer doesn't restart), then kill mplayer.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
However, "Exiting... (End of file)" gives exit code is 0. Manually closing mplayer also gives exit code 0. This would mean, in theory, that I cannot close mplayer, because when I do, it restarts?
Exit code 0 means the program finished succesfully. If you replace "##" in Trilby's example then you will not be able to close mplayer because it will launch itself again.
The issue you have is that just by losing your wireless connection, mplayer will successfully close because it thinks it reached the end of the file.
AFAIK you won't be able to get around that. You might have a better chance to write something that monitors your wireless connection while playing
and once the connection drops it keeps checking until it comes back and then initiates mplayer again.
#!/bin/bash
while [[ $(mplayer http://mp3.streampower.be/stubru-high.mp3 >/dev/null 2>&1) -eq 0]]; do sleep 1; doneIIRC you need to put make sure that there is a space after "[[ " and before " ]]".
Below is some idea what you could do to get it to do what you want. It will not work out of the box, you'll need to figure out several things yourself, but I think the general structure should be ok.
#!/bin/bash
ret=0
while [[ $ret -eq 0 ]];
do
ret=$(mplayer http://mp3.streampower.be/stubru-high.mp3 >/dev/null 2>&1)
sleep 1;
if [ $ret -eq 0 && $(check if your wireless dropped) ];
then
ret=0
else
ret=1
fi
sleep 1;
while [ $ret -eq 0 && (your wireless didn't come back yet ] ;
do
sleep 1;
done
doneOffline
I'd try if adding "-loop 0" to mplayer itself would work.
Help me to improve ssh-rdp !
Retroarch User? Try my koko-aio shader !
Offline
I fixed it! I used the command as suggested by Trilby and put it in a script ~/.fluxbox/scripts/stubru:
#!/bin/bash
while [[ $(mplayer http://mp3.streampower.be/stubru-high.mp3 >/dev/null 2>&1) -eq 0 ]]; do sleep 1; doneI call this script indirectly from the fluxbox menu:
[exec] (studio brussel) {aterm -title "radio" -geometry 50x2 -e ~/.fluxbox/scripts/stubru}
As you can see I call aterm directly, which then becomes the parent process of my looping mplayer. When I stop aterm the music stops playing as well. I can now stream music AND use the microwave!
Offline