Beta

×

Welcome to the Slashdot Beta site -- learn more here. Use the link in the footer or click here to return to the Classic version of Slashdot.

Thank you!

Before you choose to head back to the Classic look of the site, we'd appreciate it if you share your thoughts on the Beta; your feedback is what drives our ongoing development.

Beta is different and we value you taking the time to try it out. Please take a look at the changes we've made in Beta and  learn more about it. Thanks for reading, and for making the site better!

BASH SCRIPTING: My Crude Function 'resleep'

eno2001 (527078) writes | more than 4 years ago

GNU is Not Unix 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

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`"
}

cancel ×

6 comments

Sorry! There are no comments related to the filter you selected.

Nice (1)

Philip K Dickhead (906971) | more than 4 years ago | (#29541825)

It's been years. How you been?

Re:Nice (1)

eno2001 (527078) | more than 4 years ago | (#29541863)

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"?

Re:Nice (1)

Philip K Dickhead (906971) | more than 4 years ago | (#29542165)

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

Anybody see JS7a?

Re:Nice (1)

eno2001 (527078) | more than 4 years ago | (#29542775)

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.

Re:Nice (1)

gmhowell (26755) | more than 4 years ago | (#29562593)

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.

Time Units (1)

ratboy666 (104074) | more than 4 years ago | (#29588897)

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 (days). Fractional works - 1.5d is one and a half days.

Good luck with the utility!

Check for New Comments
Slashdot Login

Need an Account?

Forgot your password?
or Connect with...

Don't worry, we never post anything without your permission.

Submission Text Formatting Tips

We support a small subset of HTML, namely these tags:

  • b
  • i
  • p
  • br
  • a
  • ol
  • ul
  • li
  • dl
  • dt
  • dd
  • em
  • strong
  • tt
  • blockquote
  • div
  • quote
  • ecode

"ecode" can be used for code snippets, for example:

<ecode>    while(1) { do_something(); } </ecode>