1. Introduction

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, processes can be in different states of SLEEP, commonly referred to by the letters S (Interruptible Sleep) and D (Uninterruptible Sleep). When a process in D state, its sleeping can’t be interrupted until the specific event it’s waiting for occurs, such as an I/O operation to complete.

On the other hand, when a process is in S state, it can be interrupted by a termination signal or one of these events:

  • signal delivery
  • user input
  • network activity
  • timer expiration
  • file or directory events
  • job control signals

In any case, the SLEEP S or D state is effectively a blocked state until something happens to interrupt it.

The sleep command usually has a timer, but it may not have one if we’re using sleep infinity. In either case, it switches the Bash script process from the R (Running) state to S. It doesn’t need CPU resources while sleep is running, freeing them up for other processes.

When a process is blocked by a SLEEP S or D state, 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 192 are in interruptible sleep (S) and one is stopped (T). This allows the CPU to allocate its resources to processes that are ready to execute, improving overall system performance.

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, the sleep infinity command doesn’t actively consume system resources. Rather, it causes the process to wait indefinitely without performing active computations or significantly using CPU or memory resources. However, 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 Bash script process:

#!/bin/bash
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.

This way, we configured an infinite wait loop that ignores interrupt attempts via the TERM and INT signals, which is useful when we want a Bash script to continue to be blocked despite interrupt attempts.

5. Conclusion

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.