On Thu, 31 Mar 2011, Raymond Norton wrote:

> I'm trying to get an alert sound file to play via the command line when 
> an alarm is tripped. It works when I am manually fire of the script at 
> the counsel, and I can see the script called in syslog when the alarm is 
> tripped, but do not get any sound then.
>
> When I used mplayer to play the file I would get pulseaudio errors about 
> /var/www not being the home directory.
>
> Any ideas?


Are you on the MLUG list, too?  If not, this is a spectacular coincidence 
because I was just dealing with almost the same issue on that list about 
10 minutes before you sent your message.

My problem was that I wanted to have an alarm bell sound in a certain 
number of seconds, but all I could come up with was this:

sleep 3000 ; c=0 ; while [ $c -lt 1000 ] ; do echo -ne "\a" ; sleep 1 ; let c=$c+1 ; done

It will start ringing in 3000 seconds (50 minutes) and it will ring every 
second (about) until I hit ctrl-c.  It seems a little awkward, though.

Someone told me I could get a better result this way:

at now +50 minutes <<< "mplayer -loop 1000 /usr/share/sounds/purple/alert.wav &>/dev/null"

It's a great solution for me.  I searched for "alert.wav" on my Ubuntu box 
and found the one shown above.  It rings about every second because that's 
about how long the alert.wav takes to play.  It repeats 1000 times (but 
you can change that) and use of the "at" command gives a lot of 
flexibility for starting the process.  I think I'll make a script called 
"alarm" to automate this.

To kill it once it starts ringing, I do this:

pkill mplayer

If I didn't have the "&>/dev/null" in the command, it would produce stdout 
and send it to me as an email message, but I don't want that.  The truth 
is, I'm not sure that i'm doing that exactly right because that's bash 
syntax and I think "at" is using sh.  It is working for me, but that might 
be because I have no stderr.

To see if mplayer works on your system when started by "at", try this to 
start it immediately, ringing only 10 times:

at now <<< "mplayer -loop 10 /usr/share/sounds/purple/alert.wav &>/dev/null"

Carl S. suggested using cron.  I think "at" is a lot like cron, but I'm 
not sure of the differences in terms of which user started the job.

Also check "atq" and "atrm" for times when you decide to stop the mplayer 
job before it starts.

More notes:

(1) If you tell "at" to start a job in "+1 minute", it will start the job 
at the beginning of the next minute, which might be in 5 seconds or 27 
seconds, but it is very unlikely to be in 60 seconds.

(2) If you want to kill all "at" jobs in the queue, you can do this:

for i in $(atq | cut -f1) ; do atrm $i ; done

Mike