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(1)							   User Commands							  sleep(1)

NAME
sleep - suspend execution for an interval SYNOPSIS
sleep time DESCRIPTION
The sleep utility will suspend execution for at least the integral number of seconds specified by the time operand. OPERANDS
The following operands are supported: time A non-negative decimal integer specifying the number of seconds for which to suspend execution. EXAMPLES
Example 1: Suspending command execution for a time To execute a command after a certain amount of time: example% (sleep 105; command)& Example 2: Executing a command every so often example% while true do command sleep 37 done ENVIRONMENT VARIABLES
See environ(5) for descriptions of the following environment variables that affect the execution of sleep: LANG, LC_ALL, LC_CTYPE, LC_MES- SAGES, and NLSPATH. EXIT STATUS
The following exit values are returned: 0 The execution was successfully suspended for at least time seconds, or a SIGALRM signal was received (see NOTES). >0 An error has occurred. ATTRIBUTES
See attributes(5) for descriptions of the following attributes: +-----------------------------+-----------------------------+ | ATTRIBUTE TYPE | ATTRIBUTE VALUE | +-----------------------------+-----------------------------+ |Availability |SUNWcsu | +-----------------------------+-----------------------------+ |Interface Stability |Standard | +-----------------------------+-----------------------------+ SEE ALSO
wait(1), alarm(2), sleep(3C), wait(3UCB), attributes(5), environ(5), standards(5) NOTES
If the sleep utility receives a SIGALRM signal, one of the following actions will be taken: o Terminate normally with a zero exit status. o Effectively ignore the signal. The sleep utility will take the standard action for all other signals. SunOS 5.10 1 Feb 1995 sleep(1)
All times are GMT -4. The time now is 05:13 PM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy