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!
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?
I would either dot them in within the shell I'm currently in, or I include them in my scripts with '. ~/bin/resleep'. Since I just wrote this I haven't made extensive use of it outside of one main script. The reason I couldn't use a variable is that if I am in a different shell, the variable is not available. I don't think (though I could be wrong) that variables can be made global to all shell sessions.
Quote:
Originally Posted by peterro
I'm assuming you're putting these functions in something like a .profile. Couldn't you just set a variable with what you're using /tmp/.dtime for?
That seems an awkward thing to do in shell. It'd also make it difficult to run two instances of sleep. I'd write it in C:
Run it and hit ctrl-C, and it will show a prompt telling you how much is left then ask for an offset in seconds. A positive number of seconds will add time, a negative number will subtract time. It calculates all this time relative to the program start, so a 'resleep 90', ctrl-c, then adding 25 will have it end precisely 115 seconds after it started even though some seconds may have been spent waiting for you to type. If you hit ctrl-C again instead of entering an offset, it just quits.
Last edited by Corona688; 09-25-2009 at 06:49 PM..
Thanks very much for the feedback. UUoC! I didn't know what that was until a little Google help... I'll demoggify the original script in a few minutes.
The reason I didn't use a variable is that the problem I'm trying to solve is when sleep is used by a process that doesn't have a terminal (scheduled from crontab or backgrounded and the parent shell terminated). Unless there's a way to set up variables that can be accessed from any shell on the system... Sometimes when I schedule a job with cron with the execution length controlled by sleep, I want to be able to reset the execution length. I'm sure there is a much cleaner way to do this than a temp file though.
---------- Post updated at 01:58 PM ---------- Previous update was at 01:41 PM ----------
Corona688:
Man that's pretty cool! (I only looked at the code, haven't tried it yet) You're right that it would probably be done better in C, but I'm still working on learning C. But thank you very much for taking the time to actually code something in C as an example for a better approach. That's what makes the people of unix.com so great!
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)
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)
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)
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)
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)
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)
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)
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)