
Sleep is a command in Linux that suspends program execution for a specified time. It is an operation that lets you wait for an event, which is triggered when the time passed from sleep invocation exceeds the sleep parameter. The sleep instruction suspends the calling process for at least the specified number of seconds, minutes, hours, or days. The sleep function puts the current execution context to sleep by calling a syscall to invoke the kernel sleep function, which sets a wake-up timer, marks the current process as sleeping, and waits until the timer fires or an interrupt occurs.
| Characteristics | Values |
|---|---|
| Command | sleep |
| Operating Systems | Unix, Unix-like, OS-9, KolibriOS, FreeDOS, MSX-DOS2, PowerShell |
| Function | Suspends program execution for a specified time |
| Implementation | nanosleep(2), alarm(2), clock_nanosleep(2), getitimer(2), ualarm(3), usleep(3), signal(2,7), signal-safety(7), time(7) |
| State | Active or passive wait |
| Thread | Calling thread is removed from the Operating System's ready queue and inserted into another queue |
| Timing | Minimum period of time, specified in seconds, milliseconds or microseconds |
| Precision | Higher precision with busy loop over QueryPerformanceCounter() |
| Interruption | Interruptible by signals, returning remaining sleep time |
| Uninterruptible Sleep | Wakes only when a waited-upon resource becomes available or after a timeout |
Explore related products
What You'll Learn

Sleep command suspends program execution
The sleep command in Linux is a versatile tool that allows users to suspend program execution for a specified duration. It is a command-line utility that pauses the execution of the next command or script for a given number of seconds, minutes, hours, or even days. The basic syntax is `sleep NUMBER [SUFFIX]`. The NUMBER represents the duration of the delay, and the SUFFIX is optional and used to specify the unit of time. If no suffix is provided, the system defaults to seconds. For example, `sleep 5` will cause a delay of 5 seconds, while `sleep 3m` will cause a delay of 3 minutes.
The sleep command is particularly useful when the execution of a subsequent command depends on the successful completion of a previous command. It can also be used to manage system resources and control the timing of automated tasks. For instance, in a script that checks whether a website is online, the sleep command can be used to introduce a delay before retrying a failed operation. If a website fails to load, the script can wait for a specified duration (e.g., 10 seconds) before attempting to reload the page.
The sleep command can be integrated into shell scripts to control the timing of automated tasks. By setting multiple durations as separate arguments, users can create flexible delays. Additionally, the sleep command supports floating-point numbers, allowing for sub-second delays. For example, `sleep 0.5` will cause a delay of half a second.
It's important to note that the sleep command is designed to work in combination with other Linux commands. For example, it can be used to schedule the system to play an mp3 file after a specified time or to enforce a delay between the execution of two commands. By creating a shell script with a text editor, users can assign a variable to specify the sleep command duration. This allows for precise control over the timing of command execution.
Sleep Needs: Adults vs Kids, What's the Difference?
You may want to see also
Explore related products

Sleep() function call
The `sleep()` function in Linux is a versatile tool that allows users to introduce delays in script execution. It is a system call that suspends program execution for a specified duration, allowing the CPU to function properly while the current executable sleeps for the requested time interval.
The syntax for the `sleep()` function is straightforward: ``sleep NUMBER [SUFFIX]``. This format enables users to specify time durations in seconds or use suffixes to denote minutes, hours, or even days. For example, `sleep 6` indicates a delay of 6 seconds, while `sleep 3m` represents a pause of 3 minutes. Decimal values can also be used to specify fractional seconds, such as `sleep 3.5` for a delay of 3.5 seconds.
The `sleep()` function is implemented via ``nanosleep(2)``, which is a system call that suspends the execution of the program for a specific number of nanoseconds. On some systems, `sleep()` may also be implemented using ``alarm(2)`` and ``SIGALRM``. However, it is important to note that mixing calls to ``alarm(2)`` and `sleep()` is not recommended.
The `sleep()` function is particularly useful in scripting, where precise control over time delays is essential. It can be used to delay actions, manage timing manually, introduce delays for testing timeouts, and even slow down a loop's execution to prevent overloading during API calls or data scraping.
In summary, the `sleep()` function call in Linux provides a simple yet powerful mechanism to pause the execution of a script or command for a specified duration, enhancing the efficiency and control of Linux operations.
Autistic Kids and Sleep: Understanding the Complex Connection
You may want to see also
Explore related products

Passive wait
The sleep function in Linux is used to suspend the execution of a process or thread for a specified period of time. This is known as a "passive wait", where the thread is suspended passively until a certain condition is met or a signal is received.
In Linux, the sleep() function is implemented through system calls like nanosleep(2) or alarm(2) along with SIGALRM (POSIX.1 permits this). However, it is not advisable to mix calls to alarm(2) and sleep(). The function suspends the calling thread from execution until either the specified time has elapsed or a signal is delivered to the thread. If a signal is received, the thread's action is to invoke a signal-catching function or terminate the process.
The passive wait in Linux is particularly useful when a process needs to wait for a certain condition to be met before proceeding. For example, in a scenario where a child process needs to wait for a resource held by the parent process, the child process can be put into a passive wait state until the parent process releases the resource. Once the condition is met or the signal is received, the process "wakes up" and resumes execution.
One way to implement passive waiting in Linux is by using the pause() system call, which allows a process to wait for a signal before proceeding. Another approach is to use condition variables, which provide a mechanism for a process to wait for a specific condition to be true before proceeding. This can be useful in scenarios like a queue or synchronization primitives.
It's important to note that the actual implementation of passive waiting can vary depending on the specific requirements and constraints of the system. The behavior of the sleep function can also be influenced by the scheduling of other activities by the system, which may result in a longer suspension time than initially requested.
Better Sleep: Heart Monitoring for Health Insights
You may want to see also
Explore related products

Uninterruptible sleep state
One of the unique features of Unix systems, including Linux, is the "uninterruptible sleep" state. This is a state that a process can enter when executing certain system calls. In this state, the process is blocked from performing a system call, and it cannot be interrupted (or terminated) until the system call is completed. Most of these uninterruptible system calls are instantaneous, so you won't observe the uninterruptible nature of the call. However, in rare cases, due to buggy kernel drivers or other issues, a process can become stuck in this uninterruptible state. This scenario is similar to a zombie process state as both are unkillable, although the reasons for entering these states differ. When a process is stuck in the uninterruptible sleep state, the only option is often to reboot the system.
An infamous example of this issue is Linux with NFS. Certain local I/O operations, such as the mkdir(2) system call, are historically not interruptible. On a networked filesystem like NFS, this operation can involve network RPC calls that may block indefinitely. As a result, a program using mkdir(2) can enter the uninterruptible sleep state, leaving the operator with no choice but to reboot the system or endure the presence of this zombie-like process.
The uninterruptible sleep state can also impact the strace process. In rare cases, attaching to a process using the ptrace system call can cause the traced process to be interrupted, but if it's in the uninterruptible sleep state, the process can't be interrupted, causing the strace process to hang indefinitely. Interestingly, the ptrace(2) system call itself appears to be uninterruptible, adding complexity to the situation.
To identify processes in the uninterruptible sleep state, you can use the "ps" command, which indicates these processes with a "D" in the fourth column. Additionally, the vmstat command shows the current processes that are "blocked" or waiting on I/O. It's important to note that the ps and vmstat commands may not always agree on the number of processes in the "D" state, but that's not a significant concern.
Daytime Naps: The Sleep Patterns of Corn Snakes
You may want to see also
Explore related products

Sleep in Linux vs Windows
Sleep in Linux and Windows refers to a command that suspends program execution for a specified time. In Linux, the sleep command is implemented via nanosleep(2) or alarm(2) and SIGALRM. The version of sleep bundled with GNU coreutils was written by Jim Meyering and Paul Eggert.
In Windows, a similar function is performed by the predefined command alias Start-Sleep in PowerShell. However, one key difference between the two operating systems is that Linux also offers a hybrid sleep mode. This mode combines the advantages of hibernation and sleep/suspend functions. When activated, it writes all RAM to the hard drive and then enters a low-power state that keeps the RAM refreshed. This allows for a fast resume if power is maintained, but also protects against power failure by having the RAM data saved to disk.
To enable hybrid sleep on Linux, users may need to create a swap space and configure specific actions to trigger it, such as lid closure, power button press, or critical battery level. This can be done through tools like Upower for Ubuntu-based systems. On Windows, hybrid sleep appears to be an automatic function that is triggered when the battery reaches a critical level.
While hybrid sleep offers benefits in terms of speed and data protection, some users have reported issues with its implementation on both Linux and Windows. These issues may be related to the system's inability to hibernate properly or may require specific configurations to function correctly.
Daytime Sleep: A Sign of Health or Concern?
You may want to see also
Frequently asked questions
The sleep command is used to suspend the execution of a command or process for a specified amount of time. This can be done in seconds, minutes, hours, or days.
The sleep command is implemented via nanosleep(2) and can be found in the kernel sources. It is designed to work in combination with other Linux commands. For example, the following command will play an mp3 file after a seven-hour and 30-minute delay:
```bash
sleep 7h30m && mplayer alarm.mp3
```
The syntax for the sleep command is as follows:
```bash
sleep NUMBER[SUFFIX]
```
The suffix can be used to specify the unit of time, such as seconds, minutes, or hours. For example, to sleep for 10 seconds, the command would be:
```bash
sleep 10s
```








































