1

I hacked a perfectly simple yet effective alarm script found at http://milkteafuzz.com/j/2012/02/22/a-simple-bash-alarm-clock/ to accept user input when command is run at terminal that could be one of 3 params: alarm date-time, alarm sound file, or alarm title-msg to display. If no input is read then script prompts user for date-time for alarm using default sound file and alarm title-msg to display.

When alarm goes off I have a read command prompting user to hit the 'Enter' key to stop the alarm.

I want to have the script, when run, run in the background but still allow the user to stop the alarm (of course), but I cannot seem to implement the solution.

Here is what I have atm (omitting functions for brevity).:

echo ""

#'if' user input $1 is supplied
if [[ -n "$1" ]] ; then
    if [[ "$1" =~ [0-2]{0,1}[0-9][:]{1}[0-9]{1,2} ]] ; then     #'if' input $1 matches date-time format
        date="$1" #; echo "\$date: ${date}"
    elif [[ "$1" =~ ^.*\.(mp3|ogg)$ ]] ; then   #'if' input $1 has mp3/ogg ext
        fChkFile "$1"   #funct call
    else    #'else' it is assumed input $1 is alarm title-msg to display when alarm goes off 
        msg="$1" #; echo "\$msg: ${msg}"
    fi
fi

#'if' user input $2 is supplied
if [[ -n "$2" ]] ; then
    fInput23 "$2"   #funct call
fi

#'if' user input $3 is supplied
if [[ -n "$3" ]] ; then
    fInput23 "$3"   #funct call
fi

if [[ -z "$date" ]] ; then      #'if' date-format IS null then not detected from user input and so prompt user for one
    printf "What time should the alarm sound? "
    read date
    if [[ -z "$date" ]] ; then      #'if' "$date" IS still null then abort
        notify-send "Aborting. A date-time format is required and none was detected."
        exit 67
    fi
fi

if [[ -z "$msg" ]] ; then       #'if' title-message IS null then assign default
    msg="The time has arrived to sit up and take notice..." #; echo "\$msg: ${msg}"
fi

if [[ -z "$sndF" ]] ; then      #'if' sound file IS null then assign default
    sndF="/media/multiMediaA_intHdA720Gb/music/theStrokes_isThisIt/07_lastNite.mp3" #; echo "\$sndF: ${sndF}"
fi

echo -e "\n\t\033[1;36mOkay! Will alert you on:\033[1;32m" $(date --date="$date")"\033[0m\n"

sleep $(( $(date --date="$date" +%s) - $(date +%s) ));
notify-send "$msg" -t 0
#while true; do
    /usr/bin/mpg123 -q -l 0 "$sndF" &
    sleep 1
    read -ep $'\n\033[1;33mHit Enter key to stop the alarm.\033[0m\n' killAlm
    #almPid=$(ps aux|grep "\/usr\/bin mpg123"|cut -d' ' -f3) #; echo "\$almPid: ${almPid}"
    almPid=$(ps aux|grep "\/usr\/bin mpg123"|sed -r 's/^[^ \t]*[ \t]*([0-9]{3,5})[ \t]*.*$/\1/') #; echo "$almPid"
    kill "$almPid"
#done

The script as is runs fine and dandy, but I don't really like the idea of tying up a terminal for each alarm set (my memory is like a sieve and I tend to use multiple alarms at any given time). How might I run the script, accept input (date-time var at least) and then run in the BG, thus freeing up the terminal, but prompting the user to stop an alarm when one goes off?

EDIT: Using screens works alright as mentioned in comment(s) below. But for some reason there are, all of a sudden (first time, since trying 'screen' solution), hung mpg processes that won't clear and so

kill "$almPid"

fails because it tries to clear a previous process now hung. So I added a loop to cycle thru all mpg pids killing each one in succession, which, although surely isn't pretty, seems to work. Good thing I use audacious for streaming music, so cycling thru to kill mpg pids doesn't break anything, unless of course there are multiple alarms sounding off at the same time I guess.

    #almPid=$(ps aux|grep "\/usr\/bin mpg123"|sed -r 's/^[^ \t]*[ \t]*([0-9]{3,5})[ \t]*.*$/\1/') #; echo "$almPid"
    for p in $(ps aux|grep "\/usr\/bin mpg123"|sed -r 's/^[^ \t]*[ \t]*([0-9]{3,5})[ \t]*.*$/\1/') ; do
        # echo "\$p: $p"
        kill "$p"
    done

2 Answers 2

2

try with "screen" command

exemple here

3
  • Also using a terminal emulator with tabs :) Although screen is more versatile.
    – Graeme
    Jan 20, 2014 at 20:40
  • the screen command is a kewl(ish) idea. I am superficially familiar with screen but never used it before. It is installed already so I went ahead and played with it and it works good; better than having a single terminal open per alarm (I use terminator, so single tab per alarm actually).
    – nanker
    Jan 20, 2014 at 21:15
  • <Pause: 5 min edit timeout> But I had to add a 'for' loop since (I don't know why) there are hung mpg pids that won't clear (not sure if screen related or not) to kill the running mpg pid. Not ideal I am sure but works for now till I read/think of something better. Single tab for 'screen' command running multiple alarms works.
    – nanker
    Jan 20, 2014 at 21:22
0

I don't think there is any way to force the calling shell to bring the script into the foreground. Usually you would do this manually with fg, press enter, then use CRTL-Z to put it back into the background.

If the alarm is the only background job (or the most recent one) fg on its own is enough. If not you can see the job numbers of background processes with jobs. Supply the job number as an argument to fg.

Check out the 'JOB CONTROL' section of man bash for more information.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.