Facebook Twitter Instagram
    TecAdmin
    • Home
    • FeedBack
    • Submit Article
    • About Us
    Facebook Twitter Instagram
    TecAdmin
    You are at:Home»General Articles»sleep Command in Linux with Examples

    sleep Command in Linux with Examples

    By RahulFebruary 5, 20233 Mins Read

    The sleep command is a simple command-line utility that pauses the execution of a script or process for a specified amount of time. This can be useful in a variety of scenarios, such as waiting for a process to complete, or automating tasks that need to be performed at regular intervals. The sleep command takes a single argument, which is the number of seconds that you want to pause the execution of your script or process.

    Advertisement

    In this article, we’ll explore some common use cases of the sleep command in Linux.

    Syntax

    sleep NUMBER[SUFFIX]...
    

    Here the NUMBER can be a positive integer or a floating-point number.

    The optional SUFFIX can be one from the “s, m, h, d”.

    sleep Command Timing

    Time duration can be passed in seconds, minutes, hours, and days. Use the below suffix for defining the duration. The default duration is calculated in seconds if no suffix is provided.

    • s => Seconds (default)
    • m => Minutes
    • h => Hours
    • d => Days

    Combining sleep with other commands

    The sleep command can be combined with other commands and utilities in Linux to automate tasks, synchronize processes, and more. For example, you can use the sleep command in combination with the tar command to create a script that performs a backup of your files every hour:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    #!/bin/bash
    while true
    do
      # Perform backup
      tar -cvzf /backups/backup-$(date +"%Y-%m-%d").tar.gz /data
     
      # Wait for an hour
      sleep 3600
    done

    Using sleep in a shell script

    The sleep command is often used in shell scripts, which are collections of commands that can be executed together. By using the sleep command in a shell script, you can automate tasks, pause scripts, synchronize tasks, and more. Here’s an example of a simple shell script that uses the sleep command to pause the execution of the script for 5 seconds:

    1
    2
    3
    4
    5
    6
    7
    8
    9
    #!/bin/bash
    # Perform a task
    echo "Performing task"
     
    # Wait for 5 seconds
    sleep 5
     
    # Perform another task
    echo "Task completed"

    Synchronizing tasks:

    The sleep command can be used to synchronize the execution of different tasks so that they don’t run simultaneously and interfere with each other. For example, you can use the sleep command to run two scripts in sequence, ensuring that one script finishes before the other starts.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    #!/bin/bash
    # Run the first script
    ./script1.sh
     
    # Wait for 5 seconds
    sleep 5
     
    # Run the second script
    ./script2.sh

    Simulating user interaction

    In some cases, you may want to simulate user interaction in a script, such as clicking a button or waiting for a page to load. The sleep command can be used to pause the execution of the script for a specified amount of time, allowing the user interface to catch up.

    1
    2
    3
    4
    5
    6
    7
    8
    9
    #!/bin/bash
    # Perform some action
    xdotool click 1
     
    # Wait for 1 second
    sleep 1
     
    # Perform another actionx
    xdotool type "Hello World"

    Conclusion

    In conclusion, the sleep command is a simple yet powerful tool that allows you to pause the execution of a script or process for a specified amount of time. Whether you’re writing a shell script or automating some other process, the sleep command is an essential tool to have in your toolbox. With this beginner’s guide, you should now have a good understanding of the basics of the sleep command, and how to use it.

    Share. Facebook Twitter Pinterest LinkedIn Tumblr Email WhatsApp

    Related Posts

    Implementing a Linux Server Security Audit: Best Practices and Tools

    15 Practical Examples of dd Command in Linux

    Iptables: Common Firewall Rules and Commands

    Add A Comment

    Leave A Reply Cancel Reply

    Advertisement
    Recent Posts
    • Python Lambda Functions – A Beginner’s Guide
    • 10 Practical Use Cases for Lambda Functions in Python
    • Implementing a Linux Server Security Audit: Best Practices and Tools
    • cp Command in Linux (Copy Files Like a Pro)
    • 15 Practical Examples of dd Command in Linux
    Facebook Twitter Instagram Pinterest
    © 2023 Tecadmin.net. All Rights Reserved | Terms  | Privacy Policy

    Type above and press Enter to search. Press Esc to cancel.