Follow Slashdot blog updates by subscribing to our blog RSS feed

 



Forgot your password?
typodupeerror
×
GNU is Not Unix

Journal eno2001's Journal: BASH SCRIPTING: My Crude Function 'resleep' 6

How many times have you used the 'sleep' command interactively only to realize that the amount of time you initially specified wasn't enough and you wanted to reset it? Or how many times have you had something sleeping for quite a while but you wanted to know where it was in the sleep countdown? Never? Good you can ignore this. I've found myself in these situations quite a few times when recording a TV show or ripping a live stream from the web. So I wrote the 'resleep' functions (which wrap around 'sleep') below which can then either be included in scripts where you'd normally use sleep by itself, or dotted into your environment and run as commands. It's not polished and I'm sure the logic isn't clean, but it's working for me so far. One thing I still need to add is the ability to pass a time reference to the wrapped 'sleep' command itself so that the "time units" can be something other than "1 second". For example it would be nice to be able to set it to 30 minutes and then three time units would be 90 minutes. But that will come later... For now, enjoy, pick it apart, call me an idiot or tell me how this could be accomplished with 'sleep' if I'd RTFM ( I did).

# 'resleep' is a resettable sleep countdown timer. You specify the initial
# count of time units (in this case seconds). Then if you need more time
# you echo the new time unit count to /tmp/.dtime which resets the count
# to whatever you specify.

function resleep()
{
echo $1 > /tmp/.dtime
dur=$1

while ((dur > 0))
do
# cat /tmp/.dtime
    sleep 1
    dur=`cat /tmp/.dtime`
    countdown=$(($dur-1))
    echo $countdown > /tmp/.dtime
done

rm /tmp/.dtime
}

# The 'setsleep' function is used to reset your time unit count. Example:
# 'setsleep 30' will reset the count to 30 seconds.
# If the countdown is over an error message is presented.
function setsleep()
{
if ! [ -e /tmp/.dtime ]
then
    echo "No countdown file present"
    return
fi

echo "Current countdown: `cat /tmp/.dtime`"
echo $1 > /tmp/.dtime
echo "Reset countdown: `cat /tmp/.dtime`"
}

# The 'getsleep' function is used to see where you are in the countdown.
# If the countdown is over an error message is presented.
function getsleep()
{
if ! [ -e /tmp/.dtime ]
then
    echo "No countdown file present"
    return
fi

echo "Current countdown: `cat /tmp/.dtime`"
}

This discussion has been archived. No new comments can be posted.

BASH SCRIPTING: My Crude Function 'resleep'

Comments Filter:
  • It's been years. How you been?

    • by eno2001 ( 527078 )

      Doing alright. My last journal entry here was August 2008. I'm still over on Multiply for the most part now. But there's less discussion about computer related stuff there. I cross-posted this anyway. So how are things here at the "dot"?

      • Well you all got raptured. We're left behind...

        Anybody see JS7a?

        • by eno2001 ( 527078 )

          That's kind of the sad thing. I'm not 100% sure who is who on Multiply other than the ones who kept the same name, or have big enough presence to shine through any UID. ;) It's becoming harder and harder to find people to talk scripting with who also don't mind just talking about other stuff. Oh well, eventually there will be another "rapture" and communities will shift again. I think this is my fifth or sixth time since 1989.

          • by gmhowell ( 26755 )

            I'm not 100% sure who is who on Multiply other than the ones who kept the same name, or have big enough presence to shine through any UID. ;)

            It's ok that not everyone is as awesome as I.

  • Good to hear from you!

    I assume you are using a "GNU" userland. Use the "date" command:

    date +%s

    gives time in seconds since the epoch.

    date +%s -d 20:00

    gives time in seconds since the epoch for 8pm.

    date -d "+5 minutes"

    is the date/time 5 minutes from now.

    Basically, use "date" to interpret the current date in seconds and the desired sleep-to time. Have "date" output results in seconds, subtract, and that gives you the desired sleep time.

    Of course sleep supports units as well - 10 (or 10s) m (minutes) h (hours) d

You knew the job was dangerous when you took it, Fred. -- Superchicken

Working...