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
TIMETRANS(1p)						User Contributed Perl Documentation					     TIMETRANS(1p)

NAME
timetrans - Converts time into time SYNOPSIS
timetrans [units-options] [-count] DESCRIPTION
timetrans converts time from one type of unit to another. If any of the units options are specified, then timetrans will convert those time units into the number of seconds to which they add up. If given the count option, timetrans will convert that number of seconds into the appropriate number of weeks, days, hours, minutes, and seconds. The converted result is printed out. Units options cannot be specified in the same execution as the count option, and vice versa. timetrans is intended for use with DNSSEC-Tools, for calculating a zone's expiration time. OPTIONS
Units Options The converted value of each unit is totaled and a single result printed. -seconds seconds Count of seconds to convert to seconds. -minutes minutes Count of minutes to convert to seconds. -hours hours Count of hours to convert to seconds. -days days Count of days to convert to seconds. -weeks weeks Count of weeks to convert to seconds. Count Option The specified seconds count is converted to the appropriate number of weeks, days, hours, minutes, and seconds. -count seconds Count of seconds to convert to the appropriate set of units. Other Options timetrans has the following miscellaneous options. -Version Displays the version information for timetrans and the DNSSEC-Tools package. EXAMPLES
Example 1: Converting 5 days into seconds $(42)> timetrans -days 5 432000 Example 2: Converting 2 weeks into seconds $(43)> timetrans -w 2 1209600 Example 3: Converting 8 days and 8 hours into seconds $(44)> timetrans -d 8 -hours 8 720000 Example 4: Converting 1 week, 1 day, and 8 hours into seconds $(46)> timetrans -w 1 -days 1 -h 8 720000 Example 5: Converting 14 weeks, 4 days, 21 hours, 8 minutes, and 8 seconds into seconds $(47)> timetrans -w 14 -d 4 -h 21 -m 8 -s 8 8888888 Example 6: Converting 720000 seconds into time units $(48)> timetrans -c 720000 1 week, 1 day, 8 hours Example 7: Converting 1814421 seconds into time units $(49)> timetrans -c 1814421 3 weeks, 21 seconds Example 8: Converting 8888888 seconds into time units $(50)> timetrans -c 8888888 14 weeks, 4 days, 21 hours, 8 minutes, 8 seconds COPYRIGHT
Copyright 2004-2012 SPARTA, Inc. All rights reserved. See the COPYING file included with the DNSSEC-Tools package for details. AUTHOR
Wayne Morrison, tewok@tislabs.com SEE ALSO
zonesigner(8) Net::DNS::SEC::Tools::timetrans.pm(3) perl v5.14.2 2012-06-21 TIMETRANS(1p)
All times are GMT -4. The time now is 02:11 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy