Linux Sleep Command – delay for a specified amount of time

x


Linux sleep command adds delay/pause for a specified amount of time. As per man sleep this command,

  • Written by Jim Meyering and Paul Eggert.
  • Part of GNU Coreutils.
  • License GPLv3+: GNU GPL version 3
  • free software, you are free to change and redistribute it. There is NO WARRANTY

Syntax

sleep NUMBER[smhd]
sleep OPTION

Options

Each argument is a number followed by an optional unit.

The units are:-

  • s – seconds
  • m – minutes
  • h – hours
  • d – days

Note: Default unit is seconds.

–help – display this help and exit

–version – output version information and exit

An exit status of zero indicates success, and a nonzero value indicates failure.

Description

sleep pauses for an amount of time specified in the argument. Just open the terminal and enter below command.

~$ sleep 5

You can see terminal sleep for 5 seconds and then respond back. The NUMBER argument to this command is a mandatory while [smhd] units are optional. The default is seconds (s).

$ sleep
sleep: missing operand
Try 'sleep – help' for more information.

Historical implementations of sleep have required that number be an integer and only accepted a single argument without a suffix. However, GNU accepts arbitrary floating point numbers.

$ sleep 5.5

Sleep is shell built-in as well as an external command in some of the Linux distribution. Use type -a command to check this.

type -a sleep
sleep is /bin/sleep

It is part of core utilities so use below to see additional information.

info coreutils 'sleep invocation'

How to use sleep command in a shell script?

A shell script is a sequence of internal and/or external Unix commands. These are executed in sequence one by one. When you want to pause or add a delay in execution, you can use it as below.

Consider below bash sleep script. This script,

  1. Print message
  2. Wait for 5 seconds
  3. Print message
$ cat sleep_demo.sh
#!/bin/sh

echo "Time before - "
date
sleep 5
date
echo "Time after - "

Let’s run this Unix script using sh sleep_demo.sh command.

$ sh sleep_demo.sh
Time before - 
Sat Dec 15 12:28:06 IST 2018
Sat Dec 15 12:28:11 IST 2018
Time after -

You can clearly see the second message printed after 5 seconds.

This command is helpful when two programs running and you want one to wait for another for some time.

Let’s consider a simple interface where a system is Ftpeing a file to another system. In another system, there is scheduled cron job which reads and copy this file to some other location.

Sometimes file creation may take time, in that case, you can add a sleep for some time and make another script until the file is created.

Examples

Below are the examples of Unix sleep command with different options.

Example 1 – sleep for 1 second

sleep 1

or

sleep 1s

Example 2 – sleep for 1 minute

sleep 1m

Example 3 – sleep for 1 hour

sleep 1h

Example 4 – sleep for a day

sleep 1d

Example 5 – print help

$ sleep – help
Usage: sleep NUMBER[SUFFIX]...
  or:  sleep OPTION
Pause for NUMBER seconds.  SUFFIX may be 's' for seconds (the default),
'm' for minutes, 'h' for hours or 'd' for days.  Unlike most implementations
that require NUMBER be an integer, here NUMBER may be an arbitrary floating
point number.  Given two or more arguments, pause for the amount of time
specified by the sum of their values.

      – help     display this help and exit
      – version  output version information and exit

GNU coreutils online help: <http://www.gnu.org/software/coreutils/>
Full documentation at: <http://www.gnu.org/software/coreutils/sleep>
or available locally via: info '(coreutils) sleep invocation'

Example 6 – print version information

~$ sleep – version
sleep (GNU coreutils) 8.28
Copyright (C) 2017 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <http://gnu.org/licenses/gpl.html>.
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.

Written by Jim Meyering and Paul Eggert.

Reference and further reading