0

Could someone help me. I need a bash script to play a short sound file, wait for 30 seconds, play another sound file, wait for another 30 seconds.

(I will use it as a reminder to spend 30 seconds on each quadrant of my teeth when using a electric toothbrush.)

mpg123 /usr/share/sounds/My_Sounds/Alarm_Clock_Sound.mp3
sleep 30
mpg123 /usr/share/sounds/My_Sounds/Alarm-sound-buzzer.mp3
sleep 30
mpg123 /usr/share/sounds/My_Sounds/facility-alarm.mp3
sleep 30

But it only plays the first sound file. ??

Doug Smythies
  • 16,146
fixit7
  • 3,399

2 Answers2

6

(I will use it as a reminder to brush each quadrant of my teeth when using a electric toothbrush.)

I have to say, this is one of the strangest reasons I have ever heard (electric toothbrushes have builtin timers these days) :D ... Probably, you'd prefer your computer to give you spoken instructions as well like so:

{
t=$(date +"%s")

while true do n=$(date +"%s") ((n<=(t+30))) && spd-say -w 'Brush Upper Left Quadrant' ((n>(t+30) && n<=(t+60))) && spd-say -w 'Brush Upper Right Quadrant' ((n>(t+60) && n<=(t+90))) && spd-say -w 'Brush Lower Left Quadrant' ((n>(t+90) && n<=(t+120))) && spd-say -w 'Brush Lower Right Quadrant' ((n>(t+120))) && break done }

Or make it less chattering like so:

{
f=(
'Brush Upper Left Quadrant'
'Brush Upper Right Quadrant'
'Brush Lower Left Quadrant'
'Brush Lower Right Quadrant'
'Congratulations, mission accomplished'
)

for i in {0..4} do spd-say -w "${f[$i]}" [ "$i" -lt 4 ] && sleep 30 done }

Or use the timeout command with your player and audio files like so:

{
timeout 30 mpg123 --loop -1 /usr/share/sounds/My_Sounds/Alarm_Clock_Sound.mp3
timeout 30 mpg123 --loop -1 /usr/share/sounds/My_Sounds/Alarm-sound-buzzer.mp3
timeout 30 mpg123 --loop -1 /usr/share/sounds/My_Sounds/Alarm_Clock_Sound.mp3
timeout 30 mpg123 --loop -1 /usr/share/sounds/My_Sounds/Alarm-sound-buzzer.mp3
}

or with sleep and the Bash builtin kill like so:

{
f=(
'/usr/share/sounds/My_Sounds/Alarm_Clock_Sound.mp3'
'/usr/share/sounds/My_Sounds/Alarm-sound-buzzer.mp3'
'/usr/share/sounds/My_Sounds/Alarm_Clock_Sound.mp3'
'/usr/share/sounds/My_Sounds/Alarm-sound-buzzer.mp3'
)

for i in {0..3} do mpg123 --loop -1 "${f[$i]}" &> /dev/null & p="$!" sleep 30 kill "$p" done }

Or play each file once then wait 30 seconds and move on to the next file like so:

{
f=(
'/usr/share/sounds/My_Sounds/Alarm_Clock_Sound.mp3'
'/usr/share/sounds/My_Sounds/Alarm-sound-buzzer.mp3'
'/usr/share/sounds/My_Sounds/Alarm_Clock_Sound.mp3'
'/usr/share/sounds/My_Sounds/Alarm-sound-buzzer.mp3'
)

for i in {0..3} do mpg123 "${f[$i]}" &> /dev/null [ "$i" -lt 3 ] && sleep 30 done }

Raffa
  • 34,963
1

A few years ago I posted a bash script here in Ask Ubuntu that addresses the multiple-timer issue:

However, it allows only one custom sound file used on all the alarms.

Since then I wrote a Javascript version that runs on the Chrome or Firefox browser on any device:

The advantage of this major rewrite is you can run the timers on your smartphone and bring it to your washroom. It runs on Ubuntu of course but dragging your computer to the sink to brush your teeth is not ideal.

The other advantage of Tim-ta is you can have unlimited custom sound files uploaded and pick from them for each timer.