Edited for updated version (stations-list and stations displayed). Thanks @sng for the tips!
Hey guys! I wrote a tiny bash script to use your machine as a radio alarm clock with PyRadio. I’m completely new to bash scripting or scripting at all, so this represents my level of skills atm . Feel free to expand it or pimp it up!
Save script as ‘raclock’ or any other name you like to ~/user/bin or any other directory specified in .bash_profile and make it executable.
The script only demands two (edit: now 3 ) inputs by the user: the wakeup time and the radio station you want to play. In updated version you’re asked to choose the stations-list as well. After that ‘rtcwake’ asks for your sudo pw and shuts down your machine right away into standby (mem) and wakes it up again at the time you specified. There are other shutdown- and idle modes that might work better for your usecase. Check out documentation for ‘rtcwake’!
It’s supposed to work with PulseAudio. Changes can be made in the script. Settings in PyRadio should be preconfigured in the config-file, like the stations-list or volume level at startup. The rest should be comprehensible from the comments. Else, feel free to ask!
Have fun!
P.S.: As I move forward with my humble bash scripting attempts I will put overhauled versions of this script into a new post in this thread. There are some features I miss like notifications for wrong input etc. Perhaps anyone may hint me in the right direction. Cheers!
#!/bin/bash
# 'raclock' submited in MaBox-Forum by user Faehnchen for free use.
echo -e "\n\n\033[1;32m\t A tiny radio alarm clock for 'PyRadio' in bash \n\t Press Ctr+C if you want to cancel! \033[0m \n"
sleep 1
echo -e "\n\033[1;34m\t Hey $USER! Now it's `date +"%Y-%m-%d %H:%M"`. \n\t When do you want to be woken up? \033[0m \n"
read -p "enter: hh:mm (today) / tomorrow hh:mm / YYYY-MM-dd hh:mm) > " time
echo -e "\n\033[1;34m Choose the stations-list for PyRadio to start with! \033[0m \n"
pyradio -ls | sed 1d
echo -e " "
read -p "number of stations-list > " list
echo -e "\n\033[1;34m Please enter the number of your favourite PyRadio station! \033[0m \n"
pyradio -s $list -l | sed -e 1,2d -e 's/|.*//'
echo -e " "
read -p "station number > " station
echo -e "\n\033[1;32m\t Excellent choice. Sleep well, $USER! \033[0m \n"
# Shuts down your device into memory mode (standby) right away. Check out 'man rtcwake' for other options!
sudo rtcwake -m mem -l -t $(date -d "$time" +%s)
# Unmutes the PulseAudio default output in case it was manually muted before shutdown. Check 'man pactl' to set a specific output channel (sink).
pactl set-sink-mute `pactl get-default-sink` 0
sleep 3
# Sets the PulseAudio volume to a certain level. Change to your liking!
pactl set-sink-volume 0 70%
sleep 5
pyradio -s $list -p $station
exit
Cool that you like it! Very good ideas, I’ll try but as I said I’m totally new to scripting. Right now there’s also no warning if you confirm the questions with enter without putting in any data (rtcwake does that just after sudo). But Ill have to learn some more about if-thens to fix that.
Would also be cool when you’re asked to choose the stations list and stations, you could also skip the options and it would simply play the defaults from Pyradio config.
Just a small change, so that if you only have one playlist, it won’t ask you to select it
if (( "$(pyradio -ls | sed 1d | wc -l)" > 1 ));then
echo -e "\n\033[1;34m Choose the stations-list for PyRadio to start with! \033[0m \n"
pyradio -ls | sed 1d
echo -e " "
read -p "number of stations-list > " list
fi
[ -z "$list" ] && list=1
echo -e "\n\033[1;34m Please enter the number of your favourite PyRadio station! \033[0m"
echo -e "\033[1;34m Leave empty for random station \033[0m \n"
pyradio -s $list -l | sed -e 1,2d -e 's/|.*//'
#!/bin/bash
# 'raclock' submited in MaBox-Forum by user Faehnchen for free use.
echo -e "\n\n\033[1;32m\t A tiny radio alarm clock for 'PyRadio' in bash \n\t Press Ctr+C if you want to cancel! \033[0m \n"
sleep 1
echo -e "\n\033[1;34m\t Hey $USER! Now it's `date +"%Y-%m-%d %H:%M"`. \n\t When do you want to be woken up? \033[0m \n"
read -p "enter: hh:mm (today) / tomorrow hh:mm / YYYY-MM-dd hh:mm) > " time
if (( "$(pyradio -ls | sed 1d | wc -l)" > 1 ));then
echo -e "\n\033[1;34m Choose the stations-list for PyRadio to start with! \033[0m \n"
pyradio -ls | sed 1d
echo -e " "
read -p "number of stations-list > " list
fi
[ -z "$list" ] && list=1
echo -e "\n\033[1;34m Please enter the number of your favourite PyRadio station! \033[0m \n"
pyradio -s $list -l | sed -e 1,2d -e 's/|.*//'
echo -e " "
read -p "station number > " station
echo -e "\n\033[1;32m\t Excellent choice. Sleep well, $USER! \033[0m \n"
# Shuts down your device into memory mode (standby) right away. Check out 'man rtcwake' for other options!
sudo rtcwake -m mem -l -t $(date -d "$time" +%s)
# Unmutes the PulseAudio default output in case it was manually muted before shutdown. Check 'man pactl' to set a specific output channel (sink).
pactl set-sink-mute `pactl get-default-sink` 0
sleep 3
# Sets the PulseAudio volume to a certain level. Change to your liking!
pactl set-sink-volume 0 70%
# wait a few seconds for the internet connection to keep up
sleep 5
pyradio -s $list -p $station
exit
Sorry, for the delayed answer, wasn’t around the forum for a while.
For starters, thank you for trying my script!
How was the situation?
Did you type in the password, waited for shutdown and rtcwake just reported wakeup and mpv/pyradio began to play?
One suggestion:
If your wakeup time is 08:00 in the morning and now it’s 23:55, you still have to type in ‘tomorrow 08:00’. Else the date-command sets a time in the past (today 08:00) and rtcwake thinks it overslept and skips ‘mem’.
You could also investigate which shutdown modes rtcwake supports on your machine. See ‘man rtcwake’ for more options. Perhaps xsexxions-errors gives some clues: less ~/.xsession-errors , but I myself also didn’t found much documentation. Perhaps we have some IT-peeps who could tell more.
All in all it’s a hobby-script. It’s very basic, but since then I learned some more bash scripting and I will definetly do a updated version with more failsafes more time formats and cleaner code. Just don’t reckon too soon with it, because work eats me up right now and, as mentioned, I’m just a hobbyist.
Caution!!
The version in the top post doesn’t comply with my current bash-scripting standards and mirrors my abilities when I was excited and just started out. A problem of not the script but mpv-webradio stream is that it can crash occasionally. Please don’t rely on this (very basic) script to be waken up reliably every time! I want to apologize, if it happened to you.
Due to work and other time consuming activities I couldn’t yet improve the script to a level I could publish it with a quiet conscience. But I began to write it completely new recently, so hopefully in a while I will be able to give a updated, more userfriendly version. I’m also happy about feedback and suggestions, since beyond this forum in real life there’s no one I can have a meaningful conversation about scripting (or even Linux and computers in general).
Unfortunately I can’t edit the original post anymore, so @napcok, would it be ok to reopen it or open a new thread for that and delete the old when it is the time?