[prog] making statements wait x number of seconds

John Clarke johnc+linuxchix at kirriwa.net
Thu Sep 2 12:18:39 EST 2004


On Wed, Sep 01, 2004 at 09:30:25 -0400, aec wrote:

> This has not been covered as of yet in my book so I thought
> one of you might give me a pointer..

As Kelly has already pointed out, the function you're looking for is
sleep (also usleep and nanosleep for shorter delays).

A useful command for finding functions and commands is 'apropos'.  You
need to have some idea of what you're looking for because you need to
give it a word to search for, but it's not that hard to figure out what
to look for.  You also need to have all the man pages installed and run
makewhatis to build the database, but your distro probably installed
the necessary cron scripts to do that for you.

Here's how you could use it to find the function you're after.  First,
you could try 'apropos wait'.  That'll give you a pretty long list and
none of them do what you want, so I won't reproduce the list here.  I
know the right word to search for is 'sleep', but I'll assume for now
that you don't know that.  You want something that'll wait for some
number of seconds, so why not try 'apropos seconds':

    [johnc at dropbear ~]$ apropos seconds           
    Tcl_Sleep            (3)  - delay execution for a given number of milliseconds
    Tcl_Sleep [Sleep]    (3)  - delay execution for a given number of milliseconds
    curl_getdate         (3)  - Convert an date in a ASCII string to number of seconds since January 1, 1970
    sleep                (3)  - Sleep for the specified number of seconds
    time                 (2)  - get time in seconds
    usleep               (1)  - sleep some number of microseconds

That's much better.  There are two matches there that wait for a
certain amount of time, and both have the word 'sleep' in the
description.  Let's see if there are any more:

    [johnc at dropbear ~]$ apropos sleep
    Tcl_Sleep            (3)  - delay execution for a given number of milliseconds
    Tcl_Sleep [Sleep]    (3)  - delay execution for a given number of milliseconds
    Time::HiRes          (3pm)  - High resolution alarm, sleep, gettimeofday, interval timers
    apmsleep             (1)  - go into suspend or standby mode and wake-up later
    nanosleep            (2)  - pause execution for a specified time
    sleep                (1)  - delay for a specified amount of time
    sleep                (3)  - Sleep for the specified number of seconds
    usleep               (1)  - sleep some number of microseconds
    usleep               (3)  - suspend execution for microsecond intervals

Now you have three functions that do what you want: sleep(3), usleep(3)
and nanosleep(2).  The number in parentheses is the man section -- 1 is
shell commands, 2 is linux system calls, 3 is C/POSIX calls, etc.  If
there are matching entries in more than one section, man will usually
show you the one in the lower numbered section, but you can override
that by giving man the section number, e.g. man 3 sleep.



Cheers,

John
-- 
X is a bletcherous piece of crap which should have been aborted before
it ever tore its way out of the MIT womb and into slimed its way onto
poor, innocent computers.  Blech.  The only thing worse then X is every
other GUI in common usage today.            -- Chris Rovers


More information about the Programming mailing list