Sponsored Content
Top Forums Shell Programming and Scripting Wrapping 'sleep' with my 'resleep' function (Resettable sleep) Post 302356482 by deckard on Friday 25th of September 2009 04:35:50 PM
Old 09-25-2009
Wrapping 'sleep' with my 'resleep' function (Resettable sleep)

This is a very crude attempt in Bash at something that I needed but didn't seem to find in the 'sleep' command. However, I would like to be able to do it without the need for the temp file. Please go easy on me if this is already possible in some other way:

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? I've found myself in these situations quite a few times when recording a TV show or some other multimedia. 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 positive 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 unit of time (in seconds, minutes, hours) to the wrapped 'sleep' command itself so that my "time units" count can be something other than one second as it is now. 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... So here it is in all it's ugliness...

SCRIPT EDIT: Demoggified per cfajohnson's comments. Thanks!

Code:
# '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()
{
timefile=/tmp/.dtime$$
echo $1 > $timefile
dur=$1

while ((dur > 0))
do
  sleep 1
  read dur < $timefile
  countdown=$(($dur-1))
  echo $countdown > $timefile
done

rm $timefile
}

# 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()
{
timefile=/tmp/.dtime

if ! [ -e $timefile ]
then
  echo "No countdown file present"
  return
fi

read dur < $timefile
echo "Current countdown: $dur"
echo $1 > $timefile
read dur < $timefile
echo "Reset countdown: $dur"
}

# 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()
{
timefile=/tmp/.dtime

if ! [ -e $timefile ]
then
  echo "No countdown file present"
  return
fi

read dur < $timefile
echo "Current countdown: $dur"
}

As a sidenote, I couldn't think of a good way to do this without using the temp file, although I would have preferred to avoid it. The problem is being able to write to a variable while it's in use. Is there some way to do this without the temp file?

Last edited by deckard; 09-28-2009 at 04:09 PM..
 

9 More Discussions You Might Find Interesting

1. UNIX for Dummies Questions & Answers

sleep

what is the purpose of the sleep command? (5 Replies)
Discussion started by: Anna
5 Replies

2. Shell Programming and Scripting

Sleep under one second

If I want a script to sleep for less than a second, would I use a decimal? In other words, if I wanted my script to sleep for 1/4 of a second, would I say, SLEEP .25 ?? (5 Replies)
Discussion started by: Scoogie
5 Replies

3. UNIX for Dummies Questions & Answers

Sleep less than 1 second

Does anyone know a way to sleep less than 1 second? Sometimes when I write scripts that iterates a loop many times it would be nice to slow things down, but sometimes 1 second is too much. (9 Replies)
Discussion started by: bjorno
9 Replies

4. Shell Programming and Scripting

Help with sleep function

Hey everyone, just entering the linux world, I need some help with a shell script i'm trying to write, the purpose is to check every 10 minutes what was the last time a certain file was modified, and if there is a connection to the server at this moment send an email with the date of the... (2 Replies)
Discussion started by: moshe88
2 Replies

5. Programming

C Sleep function hangs @ __kernel_vsyscall ()

This is the gdb backtrace. ^C Program received signal SIGINT, Interrupt. 0xffffe424 in __kernel_vsyscall () (gdb) bt #0 0xffffe424 in __kernel_vsyscall () #1 0xb7e56a70 in __nanosleep_nocancel () from /lib/libc.so.6 #2 0xb7e568bb in __sleep (seconds=0) at sleep.c:138 #3 0x080496d5 in... (6 Replies)
Discussion started by: dragonpoint
6 Replies

6. Shell Programming and Scripting

How the Sleep function will work?

Hi All, I am new to Unix , there i am facing one problem with sleep command. that is .. in while loop i have defined sleep function .. my condition is like this while #i knew this is infinite loop do sleep 200 echo "hello " done. this condition will never become .. true... (3 Replies)
Discussion started by: mandlysreedhar
3 Replies

7. Shell Programming and Scripting

Sleep while i > 0

Hi, I have a script that runs a process at the beginning and I want to sleep/wait until this process is finished and then continue with the rest of the script. I am trying with this, but it is not working: process=`ps -ef | grep "proc_p01 -c" | grep -v grep | wc -l` if ; do sleep 10 done... (7 Replies)
Discussion started by: apenkov
7 Replies

8. Programming

Sleep function not detected

Hello Im using geany to write my c codes. Below is my code to make the internal LED of beaglebone flashing. But i cant seem to use the sleep or delay to make the program wait for a couple of miliseconds. I've included all include files that i can find but none of it solve the problem. Any help is... (1 Reply)
Discussion started by: HellRyder
1 Replies

9. Programming

Doubt with signals and sleep function

Hi , I have a doubt with signals and sleep function. In a program i have this while(1) { //do some work sleep(1); }And in a thread i have something like this union sigval data; char message; char msg; data.sival_int=0; while(1) { ... (4 Replies)
Discussion started by: bacesado
4 Replies
IO::Async::Timer::Countdown(3pm)			User Contributed Perl Documentation			  IO::Async::Timer::Countdown(3pm)

NAME
"IO::Async::Timer::Countdown" - event callback after a fixed delay SYNOPSIS
use IO::Async::Timer::Countdown; use IO::Async::Loop; my $loop = IO::Async::Loop->new; my $timer = IO::Async::Timer::Countdown->new( delay => 10, on_expire => sub { print "Sorry, your time's up "; $loop->stop; }, ); $timer->start; $loop->add( $timer ); $loop->run; DESCRIPTION
This subclass of IO::Async::Timer implements one-shot fixed delays. The object implements a countdown timer, which invokes its callback after the given period from when it was started. After it has expired the Timer may be started again, when it will wait the same period then invoke the callback again. A timer that is currently running may be stopped or reset. For a "Timer" object that repeatedly runs a callback at regular intervals, see instead IO::Async::Timer::Periodic. For a "Timer" that invokes its callback at a fixed time in the future, see IO::Async::Timer::Absolute. EVENTS
The following events are invoked, either using subclass methods or CODE references in parameters: on_expire Invoked when the timer expires. PARAMETERS
The following named parameters may be passed to "new" or "configure": on_expire => CODE CODE reference for the "on_expire" event. delay => NUM The delay in seconds after starting the timer until it expires. Cannot be changed if the timer is running. A timer with a zero delay expires "immediately". remove_on_expire => BOOL Optional. If true, remove this timer object from its parent notifier or containing loop when it expires. Defaults to false. Once constructed, the timer object will need to be added to the "Loop" before it will work. It will also need to be started by the "start" method. METHODS
$expired = $timer->is_expired Returns true if the Timer has already expired. $timer->reset If the timer is running, restart the countdown period from now. If the timer is not running, this method has no effect. EXAMPLES
Watchdog Timer Because the "reset" method restarts a running countdown timer back to its full period, it can be used to implement a watchdog timer. This is a timer which will not expire provided the method is called at least as often as it is configured. If the method fails to be called, the timer will eventually expire and run its callback. For example, to expire an accepted connection after 30 seconds of inactivity: ... on_accept => sub { my ( $newclient ) = @_; my $watchdog = IO::Async::Timer::Countdown->new( delay => 30, on_expire => sub { my $self = shift; my $stream = $self->parent; $stream->close; }, ); my $stream = IO::Async::Stream->new( handle => $newclient, on_read => sub { my ( $self, $buffref, $eof ) = @_; $watchdog->reset; ... }, on_closed => sub { $watchdog->stop; }, ) ); $stream->add_child( $watchdog ); $watchdog->start; $loop->add( $watchdog ); } Rather than setting up a lexical variable to store the Stream so that the Timer's "on_expire" closure can call "close" on it, the parent/child relationship between the two Notifier objects is used. At the time the Timer "on_expire" closure is invoked, it will have been added as a child notifier of the Stream; this means the Timer's "parent" method will return the Stream Notifier. This enables it to call "close" without needing to capture a lexical variable, which would create a cyclic reference. Fixed-Delay Repeating Timer The "on_expire" event fires a fixed delay after the "start" method has begun the countdown. The "start" method can be invoked again at some point during the "on_expire" handling code, to create a timer that invokes its code regularly a fixed delay after the previous invocation has finished. This creates an arrangement similar to an IO::Async::Timer::Periodic, except that it will wait until the previous invocation has indicated it is finished, before starting the countdown for the next call. my $timer = IO::Async::Timer::Countdown->new( delay => 60, on_expire => sub { my $self = shift; start_some_operation( on_complete => sub { $self->start }, ); }, ); $timer->start; $loop->add( $timer ); This example invokes the "start_some_operation" function 60 seconds after the previous iteration has indicated it has finished. AUTHOR
Paul Evans <leonerd@leonerd.org.uk> perl v5.14.2 2012-10-24 IO::Async::Timer::Countdown(3pm)
All times are GMT -4. The time now is 03:05 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy