As a etude, I wrote a quick-and-dirty bash script to set a radio alarm a few months ago. It sets a cron/at-job to start streaming an internet radio at as specified time (I use mplayer), and records the job id in a file for easy undoing. However, as it stands the logic to turn off a running alarm is simply to kill of the most recent couple of mplayer instances. This is potentially a problem if you're watching a video at the time the alarm goes off, or running a batch job converting audio or video files...
So I thought I'd create a designated virtual user for running this script, and, instead of killing the most recent mplayer instances, kill all and only those invoked by this user. I thus created a user radiowecker
and invoke the script with sudo -u radiowecker /var/lib/radiowecker/wecker $1
. However, this doesn't seem to do the job: While the at
-job does show up as radiowecker
's, the mplayer instances it spawns are filed under my UID. How do I ensure the child processes are also filed as radiowecker
's?
if [ $2 ];
then stream="$2"
else stream=http://mp3stream1.apasf.apa.at:8000;
fi
if [ $1 ]; then
# with argument, set new radio alarm using 'at' and log the at-job-id
remove_log_when_playing="rm ~/.local/bin/weckerlogs/${*} "
play_radio="mplayer $stream -cache 1024"
and="&&"
show_running="touch ~/.local/bin/alarm_running"
printf "$remove_log_when_playing && $show_running && $play_radio" | at "${*}" \
&& echo $(atq | sort -nr | { read first last ; echo $first ; }) >> ~/.local/bin/weckerlogs/"${*}"
else
if [[ $(pgrep mplayer) && -e ~/.local/bin/alarm_running ]]; then
rm ~/.local/bin/alarm_running
# turn off running mplayer, assumed to be called from an earlier alarm
for i in 0 1; do
for id in $(pgrep mplayer)
do WECKER=$id
done
kill $WECKER
done
else
# turning off an alarm in the future has its own tool
echo "No active mplayer instances found."
echo "To turn off a future alarm, instead"
echo "use 'wecker-aus' <time>!"
echo "Currently set alarms:"
ls ~/.local/bin/weckerlogs/
fi
fi
turn off future alarm:
#/bin/bash
log=~/.local/bin/weckerlogs/"${*}"
atrm $(cat "$log")
rm "$log"