In Linux, we can put a process to sleep using the sleep command for a set amount of time. This action suspends the process’s execution temporarily.
In this tutorial, we’ll see how to put a process to sleep indefinitely, thereby effectively blocking it.
2. Process Blocking
Before we look at how to put a process to sleep indefinitely, let’s see what happens when a process is blocked in Bash.
In Linux, we consider a process in the SLEEP state to be blocked. This means that the process is waiting for a specific event to occur before it continues its execution. The following examples are events that may trigger sleeping processes to wake:
- signal delivery
- user input
- network activity
- timer expiration
- file or directory events
- job control signals
In the case of the sleep command with an infinite timeout, the process is waiting indefinitely. This effectively blocks any further execution of instructions until interrupted in the case of interruptible SLEEP.
When a process is blocked, it’s removed from the CPU’s active process list and placed in a waiting queue until it receives the event it’s waiting for. The top command shows data for sleeping and running processes:
$ top top - 11:40:03 up 5:51, 1 user, load average: 0.23, 0.23, 0.24 Tasks: 196 total, 3 running, 192 sleeping, 1 stopped, 0 zombie %Cpu(s): 22.5 us, 5.7 sy, 0.0 ni, 71.4 id, 0.4 wa, 0.0 hi, 0.0 si, 0.0 st MiB Mem : 3924.3 total, 775.9 free, 1144.9 used, 2003.4 buff/cache MiB Swap: 3720.0 total, 3720.0 free, 0.0 used. 2509.4 avail Mem PID USER PR NI VIRT RES SHR S %CPU %MEM TIME+ COMMAND 2060 chris 20 0 4006148 322236 135148 R 23.2 8.0 2:26.33 gnome-shell 3354 chris 20 0 836332 54428 41284 R 5.6 1.4 0:19.30 gnome-terminal- 6355 chris 20 0 21732 3936 3336 R 0.7 0.1 0:01.30 top 607 systemd+ 20 0 14824 6084 5292 S 0.3 0.2 0:26.46 systemd-oomd 1 root 20 0 166820 12028 8320 S 0.0 0.3 0:03.47 systemd ...
Here, we can see from the analysis that the system has 196 processes, of which only three are running (R), while the others are sleeping (S). This allows the CPU to allocate its resources to processes that are ready to execute, improving overall system performance.
On the other hand, since a process in an infinite uninterruptible SLEEP can’t receive any events, it may remain in the waiting queue indefinitely. This waiting consumes system resources without doing any useful work.
Let’s now see how to put a process to sleep indefinitely.
3. The sleep Command
In Bash, we can put a process to sleep for a specified period using the sleep command:
$ sleep NUMBER[SUFFIX]
Here, NUMBER is the amount of time to sleep, and SUFFIX is an optional unit of time. Notably, we can use the following suffixes:
- s: seconds (default)
- m: minutes
- h: hours
- d: days
Let’s now employ the sleep command to block a shell process.
3.1. Using the sleep Command
For example, to cause the shell to sleep for 10 seconds, we can use the command:
$ sleep 10s
Also, to sleep for two hours, we can use a suffix like h:
$ sleep 2h
Further, to block a process indefinitely, we can use the sleep command with a very large value:
$ sleep 9999999999
The command will cause the process to sleep for a very long time, effectively blocking it indefinitely.
3.2. sleep infinity
Alternatively, we can use the infinity argument to set up an infinite sleep in Bash. To do so, we’ll supply infinity as an argument to sleep:
$ sleep infinity
This command will put the shell to sleep indefinitely, effectively blocking any further execution of commands until interrupted.
Again, it’s important to note that using infinite sleep can lead to a system resource problem, as it consumes system resources indefinitely. Also, using the infinity argument can make scripts difficult to debug and maintain.
3.3. Using sleep in a while loop
The while loop in Bash enables us to iterate tasks continuously. Thus, we can use the while loop to initiate an infinite sleep in Bash and block a process entirely:
$ while true do sleep 1 done
This loop will execute the sleep 1 command repeatedly, indefinitely putting the shell to sleep for one second at a time. Notably, we can adjust the sleep duration as we need.
Consequently, when we run any of these commands in a terminal, we’ll be unable to interact with the shell until we stop the process. To terminate the infinite sleep, we can use a keyboard interrupt (Ctrl+C) or kill the process.
4. Other Commands for Infinite Blocking
Apart from the sleep command, we can use the pause command and two other commands in Bash to initiate an infinite sleep.
4.1. The pause Command
In Bash, the pause command is a function defined in the /etc/bash.bashrc file. The command suspends the execution of a shell script until the user presses the Enter key. It is similar to the sleep command, although it waits for a signal to wake up instead of waiting for a specific duration.
For instance, the following script shows the role of the pause command in blocking processes:
#!/bin/bash # Main script code here echo "Running script..." # Wait for user input pause "Press Enter to exit." # Exit script exit 0
To explain, when the pause command is executed, the shell script stops executing and waits for the user to press the Enter key. Once the user presses Enter, the script resumes executing from where it left off.
Notably, we can employ the pause command in shell scripts that require user input or interaction. For example, scripts that prompt the user for input before continuing can use the command. Thus, we can ensure that the script doesn’t continue until we’re ready to proceed.
4.2. The trap Command
Next, the trap command is a built-in command that intercepts and handles signals that a script or process receives. For example, we can trap signals that stop our process:
$ trap '' TERM INT; sleep infinity & wait
This command traps the signals TERM and INT and then executes the sleep infinity command in the background, preventing it from being interrupted. Further, the wait command ensures that the shell doesn’t exit until the background process completes.
The effect is that the shell will remain in an infinite sleep until it receives a signal to terminate or interrupt the process.
In this article, we saw how to put a process to sleep indefinitely, thereby effectively blocking it.
In summary, we saw what it means to block a process. Then we explored the sleep command and how to apply it to achieve infinite sleep, thus blocking a process. Further, we looked into alternative commands that we can use to initiate infinite sleep in Bash.