376

What is difference between wait and sleep?

dkb
  • 4,389
  • 4
  • 36
  • 54
tirenweb
  • 30,963
  • 73
  • 183
  • 303

3 Answers3

439

wait waits for a process to finish; sleep sleeps for a certain amount of seconds.

arc
  • 4,553
  • 5
  • 34
  • 43
MRAB
  • 20,356
  • 6
  • 40
  • 33
142

wait is a BASH built-in command. From man bash:

    wait [n ...]
        Wait  for each specified process and return its termination sta-
        tus.  Each n may be a process ID or a job  specification;  if  a
        job  spec  is  given,  all  processes in that job's pipeline are
        waited for.  If n is not given, all currently active child  pro-
        cesses  are  waited  for,  and  the return status is zero.  If n
        specifies a non-existent process or job, the  return  status  is
        127.   Otherwise,  the  return  status is the exit status of the
        last process or job waited for.

sleep is not a shell built-in command. It is a utility that delays for a specified amount of time.

The sleep command may support waiting in various units of time. GNU coreutils 8.4 man sleep says:

    SYNOPSIS
        sleep NUMBER[SUFFIX]...

    DESCRIPTION
        Pause for NUMBER seconds.  SUFFIX may be ‘s’ for seconds (the default),
        ‘m’ for minutes, ‘h’ for hours or ‘d’ for days.  Unlike most  implemen-
        tations  that require NUMBER be an integer, here NUMBER may be an arbi-
        trary floating point number.  Given two or more  arguments,  pause  for
        the amount of time specified by the sum of their values.
kbulgrien
  • 4,384
  • 2
  • 26
  • 43
110

sleep just delays the shell for the given amount of seconds.

wait makes the shell wait for the given job. e.g.:

workhard &
[1] 27408
workharder &
[2] 27409
wait %1 %2

delays the shell until both of the subprocesses have finished

iBug
  • 35,554
  • 7
  • 89
  • 134
pbhd
  • 4,384
  • 1
  • 20
  • 26
  • 29
    IMHO it is `wait %1 %2` or `wait 27408 27409` or simply `wait` if there is no other background process. In this case you are trying to wait for PID 1 (init) and PID 2 ([migration/0] on my Linux), but you will get error message, like: `-bash: wait: pid 1 is not a child of this shell` and returns the exit code `127`. – TrueY Nov 19 '14 at 08:54
  • 15
    So as of 2 Years nobody realized it. You are absolutely right, will edit the answer... – pbhd Nov 19 '14 at 09:02