Diferencia entre esperar y dormir


¿Cuál es la diferencia entre wait y sleep?

Author: dkb, 2012-11-09

5 answers

wait espera a que termine un proceso; sleep duerme durante una cierta cantidad de segundos.

 275
Author: MRAB,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2017-10-09 13:14:41

Wait es un comando integrado en BASH. De 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 no es un comando integrado en el shell. Es una utilidad que retrasa por una cantidad de tiempo especificada.

El comando sleep puede soportar la espera en varias unidades de tiempo. GNU coreutils 8.4 man sleep dice:

    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.
 83
Author: kbulgrien,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2018-05-10 16:57:53

sleep solo retrasa el shell por la cantidad de segundos dada.

wait hace que el shell espere el subproceso dado. por ejemplo:

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

Retrasa el shell hasta que ambos subprocesos hayan terminado

 72
Author: pbhd,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2014-11-19 09:03:31

Bash

Espere el comando detener la ejecución del script hasta que todos los trabajos que se ejecutan en segundo plano hayan terminado o hasta que el número de trabajo o id de proceso especificado como opción finalice

wait%1 or wait $PID
wait ${!}

Espera {{!} significa "esperar hasta que se complete el último proceso en segundo plano" ($! siendo el PID del último proceso de fondo)

Dormir

Añada retardo durante un período de tiempo especificado.

sleep NUMBER[SUFFIX]
sleep 5 (sleep five seconds)
 18
Author: jcrada,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-08 20:10:42

Prueba esto:

sleep 10 &
wait %1
 12
Author: leafei,
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/ajaxhispano.com/template/agent.layouts/content.php on line 61
2012-11-09 02:36:43