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!

# '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?

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?

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.

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:

#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <signal.h>
#include <limits.h>

void catch(int c)        {       }

int main(int argc, char *argv[])
{
        unsigned long start=time(NULL), len;

        if(argc != 2)
        {
                fprintf(stderr, "usage: %s 60\n", argv[0]);
                return(1);
        }

        len=atol(argv[1]);

        if((len == LONG_MIN) || (len == LONG_MAX) || (len < 0))
        {
                fprintf(stderr, "Bad input value, must be positive integer\n");
                return(1);
        }

        signal(SIGINT, catch);

        while( (time(NULL) < (start+len)))
        if(sleep((start+len) - time(NULL)))
        {
                int off=INT_MAX;
                signal(SIGINT, SIG_DFL);
                while(off == INT_MAX)
                {
                        printf("\r%d seconds remain, offset:",
                                (start+len)-time(NULL));

                        if(scanf("%d", &off) != 1)
                        {
                                fflush(stdin);
                                off=-1;
                        }
                }

                printf("%d%+d seconds, %d remain\n", len, off,
                        (start+len+off)-time(NULL));
                len += off;

                signal(SIGINT, catch);
        }

        return(0);
}

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.

UUOC (there and in other places).

 read dur < /tmp/.dtime

Rather than repeating /tmp/.dtime every time you refer to the file, set a variable and use that when needed:

timefile=/tmp/.dtime     ## but see below
...
read dur < "$timefile"

What happens if you have two scripts running? One will mess up the file for the other.

You should create a unique filename for each instance, e.g.:

timefile=/tmp/.dtime$$

cfajohnson:

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. :slight_smile:

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! :slight_smile: