Home > Environment & Utility > sleep

sleep: Wait for a Specified Duration

The `sleep` command is used to temporarily suspend the execution of the system for a specified duration (seconds, minutes, hours, days). It is useful for creating intervals between tasks in scripts or terminals, or for waiting until a specific task completes.

Overview

Like its name suggests, `sleep` pauses the execution of a command for a specified duration. This command is primarily used in shell scripts for time-related control, such as adjusting execution intervals of automated tasks or waiting for a specific file to be created. It supports units of seconds (s), minutes (m), hours (h), and days (d), allowing you to set wait times in various time units.

Key Features

The key features of the `sleep` command are as follows:

  • Waits for a specified duration (seconds, minutes, hours, days).
  • Used to control time intervals between tasks in scripts.
  • Supports fractional time for more precise time control.
  • Uses minimal CPU resources while waiting.

Key Options

The `sleep` command takes a wait time as an argument and does not have specific options for unit specification. Instead, units are specified directly with the argument.

1) Execution Options

2) Help

Generated command:

Try combining the commands.

Description:

`sleep` Executes the command.

Combine the above options to virtually execute commands with AI.

Usage Examples

Explore the functionalities of the `sleep` command through various usage examples.

Wait for 3 Seconds

sleep 3

Waits for 3 seconds after command execution. If no unit is specified, it defaults to seconds.

Wait for 1 Minute 30 Seconds

sleep 1m 30s

Multiple time durations can be added by separating them with spaces. `1m` is 60 seconds, and `30s` is 30 seconds, so it waits for a total of 90 seconds.

Using Fractional Time

sleep 0.5

Waits for 0.5 seconds using fractional time.

Usage in Scripts

while true; do ping -c 1 google.com; sleep 5; done

When used with a `while` loop in a script, you can create a task that executes the `ping` command every 5 seconds.

Run in Background

sleep 1h &

Using the `&` symbol allows you to run the `sleep` command in the background, preventing it from occupying the terminal.

Installation

`sleep` is part of the `coreutils` package and is included by default in most Linux distributions. No separate installation is required.

Tips & Cautions

Here are some points to note when using the `sleep` command.

Tips

  • `sleep` uses minimal CPU resources while waiting, making it suitable for long-duration waiting tasks.
  • You can provide multiple time durations as arguments, like `sleep 1h 30m`. In such cases, all durations are added together to form the total wait time.
  • Time units (`s`, `m`, `h`, `d`) are case-insensitive. `5S` and `5s` both mean 5 seconds.

Same category commands