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
sleep(3)						     Library Functions Manual							  sleep(3)

Name
       sleep - suspend execution for interval

Syntax
       unsigned
       sleep(seconds)
       unsigned seconds;

Description
       The  current process is suspended from execution for the number of seconds specified by the argument.  The actual suspension time may be up
       to 1 second less than that requested, because scheduled wakeups occur at fixed 1-second intervals, and an arbitrary amount  longer  because
       of other activity in the system.

       The  routine  is  implemented  by  setting  an  interval  timer and pausing until it occurs.  The previous state of this timer is saved and
       restored.  If the sleep time exceeds the time to the expiration of the previous timer, the process sleeps only until the signal would  have
       occurred, and the signal is sent 1 second later.

Return Values
       The  value  returned by is the unslept amount(the requested time minus the time actually slept). This return value may be non-zero in cases
       where the caller had an alarm set to go off earlier than the end of the requested time, or where was interrupted due to a caught signal(see
       ENVIRONMENT below).

Environment
       POSIX
       SYSTEM_FIVE
       When  your  program  is compiled in POSIX or System V mode, the will be terminated by any caught signal. The function will return following
       execution of the signal's catching routine.

See Also
       setitimer(2), sigpause(2)

																	  sleep(3)
All times are GMT -4. The time now is 10:04 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy