sleep

process managementLinux/Unix
The sleep command is one of the most frequently used commands in Linux/Unix-like operating systems. sleep Delay for a specified amount of time

Quick Reference

Command Name:

sleep

Category:

process management

Platform:

Linux/Unix

Basic Usage:

sleep [options] [arguments]

Common Use Cases

    Syntax

    sleep number[suffix]...

    Options

    Suffix Description
    s Seconds (default if no suffix is specified)
    m Minutes
    h Hours
    d Days
    Option Description
    --help Display a help message and exit
    --version Output version information and exit

    Examples

    How to Use These Examples

    The examples below show common ways to use the sleep command. Try them in your terminal to see the results. You can copy any example by clicking on the code block.

    # Basic Examples Basic
    sleep 5
    Pause execution for 5 seconds.
    sleep 1m
    Pause execution for 1 minute.
    sleep 0.5
    Pause execution for half a second. # Advanced Examples Advanced sleep 1h 30m 10s Pause execution for 1 hour, 30 minutes, and 10 seconds. echo "Starting task..." sleep 2 echo "Task completed!" Add a delay between outputs. for i in {1..5}; do echo "Iteration $i" sleep 1 done Create a countdown with 1-second intervals. # Run a command and retry if it fails until command_that_might_fail; do echo "Command failed, retrying in 5 seconds..." sleep 5 done # Create a simple countdown timer countdown() { for ((i=$1; i>=1; i--)); do echo "$i..." sleep 1 done echo "Time's up!" } countdown 10 # Monitor a log file with periodic updates tail -f logfile.log & sleep 10 && kill $! View log updates for 10 seconds then stop. # Simulate slow network for testing
    cat largefile.txt | while read line; do
    echo "$line" sleep 0.1 done # Execute a command at regular intervals while true; do date sleep 60 done Display the current time every minute. # Create a progress bar effect progress() { for ((i=0; i<=100; i+=5)); do printf "\rProgress: %d%%" $i sleep 0.2 done echo } progress

    Try It Yourself

    Practice makes perfect! The best way to learn is by trying these examples on your own system with real files.

    Understanding Syntax

    Pay attention to the syntax coloring: commands, options, and file paths are highlighted differently.

    Notes

    The `sleep` command is a simple yet powerful utility in Unix-like operating systems that pauses execution for a specified amount of time. While conceptually straightforward, this command serves numerous practical purposes in shell scripting and system administration. At its core, `sleep` temporarily suspends execution for the duration specified in its arguments. The time can be specified in various units: seconds (default), minutes, hours, or days, using the suffixes s, m, h, and d respectively. The command also accepts floating-point numbers, allowing for sub-second precision (e.g., `sleep 0.5` for half a second). One of the key features of `sleep` is its ability to combine multiple time units in a single command. For example, `sleep 1h 30m 10s` will pause for 1 hour, 30 minutes, and 10 seconds. This flexibility makes it easy to specify complex time intervals without manual conversion to a single unit. Common use cases for the `sleep` command include: 1. Adding delays between commands in shell scripts to control timing or pacing 2. Creating scheduled intervals for repeated tasks or monitoring 3. Implementing retry mechanisms with progressively longer waits 4. Simulating network latency or other time-dependent behaviors during testing 5. Creating countdown timers or progress indicators in terminal applications 6. Limiting resource usage by spreading operations over time 7. Allowing time for background processes to complete before proceeding While `sleep` is not CPU-intensive (it puts the process to sleep without consuming significant resources), it's worth noting that it has limitations in precision. The actual sleep duration might vary slightly from the requested time due to system scheduling and process priorities, especially for very short sleep periods. In more complex scenarios, specialized timing tools or the `cron` system might be more appropriate for precise scheduling. However, for most scripting needs, `sleep` provides a perfectly adequate solution for introducing delays and controlling execution timing. The `sleep` command is available on virtually all Unix-like systems, including Linux, macOS, and BSD, making it a reliable tool for cross-platform scripts.

    Related Commands

    These commands are frequently used alongside sleep or serve similar purposes:

    Use Cases

    Learn By Doing

    The best way to learn Linux commands is by practicing. Try out these examples in your terminal to build muscle memory and understand how the sleep command works in different scenarios.

    $ sleep
    View All Commands