Sleep process in linux

Linux sleep Command | Explained With Examples

In Linux, the sleep command adds a delay for running the commands or scripts. When the user wants to execute a command after a specific time, you can add a delay it can be seconds (s), minutes (m), or hours. The sleep command is very useful when you want to complete the previous commands first and then execute the new commands. The post’s content is as follows:

What is Linux sleep Command?

The sleep command pauses the system to execute the previous execution and then run the next command. This command sleeps the system for a specific time or at a specific time. This article will discuss the different uses of sleep command. Let’s start!

Syntax of sleep Command

We can use the sleep command differently. Check the syntax for the sleep command below:

The syntax components are described below:

  • The “number” represents the numerical value for the delay.
  • The “suffix” can be second (s), minutes (m), hours (h), or days (d). If the suffix is not provided, it will take seconds as the default value.

For more help on the sleep command, use the following command:

Let’s get into the usage of the sleep command!

How to Use sleep Command in Linux?

The sleep command can be utilized in several ways, which are elaborated with the help of different examples.

Example 1: How to Delay for Specific Time?

We can add a delay of seconds, minutes, hours, and days in Linux with the sleep command. Let’s discuss how to add a specific delay.

Delay for Second

To delay three seconds in the execution, use the below sleep commands with the specified number of seconds:

We can use the sleep command with the above syntax with any other command to delay the system for a specific time. For instance, to sleep the system for three minutes with the cat command is given below:

$ sleep 3 && cat testfile3.txt

Delay for Minutes

To pause the execution for minutes, the “m” convention is used. To delay for three minutes, use the below-written command:

Читайте также:  Увеличить размер swap linux

Note: You can use the floating number like 0.5 hours for 30 minutes.

For pausing the system to run the “pwd” command for three minutes, execute the below command:

Delay for Hours

For sleeping the system for three hours (h), utilize the below command:

If you want to run the command “cd ~/Downloads”, after five hours, run the below command in the terminal:

Delay for Days

To delay the execution of the system for three days, use the following command:

If you want to play an audio testfile.mp3 after/schedule three days, execute the following command:

Delay for Specific Hours and Minutes

To pause the system for specific seconds, minutes, hours, and days, we can use them together. For instance, to sleep the system for two hours and two minutes, utilize the below command utility:

For example, to have a pause of 2 hours and 2 minutes to play a video named testfile.mp4, execute the following command:

Example 2: How to Delay the Multiple Linux Commands?

The sleep command allows you to give a pause between two commands. For example, if you want to display the “testfile1.txt” content and give a pause of 5 seconds to show the “testfile2.txt” content, execute the below command:

Note: The cat command is used to output the file’s content.

$ cat testfile1.txt && sleep 5s && cat testfile2.txt $ cat testfile1.txt ; sleep 5s ; cat testfile2.txt

Similarly, to change the directory to “Desktop” and delay for five seconds before listing out the file in that directory, run the below command:

Example 3: How to Start any Process After a Specific Time?

Similarly, any other Linux process can also be executed with a specific delay. For instance, to start a video named “movie.mp4” after a two hours delay time, use the below command:

Example 4: How to Verify the Delay Time?

The sleep time adds a pause to the execution. If you to verify the delay time at the time of execution, you can use the time command for that. To pause the system for two seconds and print out the “Time Finished”, use the below command:

$ time(sleep 2s; echo "Time Finished")

The output shows the real-time as 0 minutes and 2 seconds, which verifies the delay of 2 seconds.

Example 5: How to Delay Bash Script?

The sleep command is useful to delay the bash scripts. The long bash scripts take time to process the executions while the next command starts executing. For executing a simple bash script, with a delay of five minutes, the code is given below:

#!/bin/bash echo "Start Time $(date)" sleep 5s echo "Print Time $(date)"

The scripts start with the “#!” which indicates the start of execution and /bin/bash is a directory for bash scripts.

The echo displays the text “Start Time” with a date variable which will print the date & time, and after the 5-second delay, the text “Print Time” with the current date and time will be displayed.

Читайте также:  Линукс для древнего ноутбука

Note: If you try to run the bash script code without enabling the Execute (x) permission, you will get the below error:

Permission denied!

Use the below chmod command to enable the execute (x) permission for that bash script:

To run the bash script file named “testfile.sh”, use the following command:

The output shows the text with the date & time before and after the delay of 5 seconds.

Bonus Tip: How to Exit From Sleep Mode?

If you have used a large delay time and want to exit the sleep mode, press the shortcut keys “Ctrl + C” to exit the sleep mode.

For instance, to exit the sleep cycle of five minutes, as used in the below command, use the “Ctrl + C” keys.

$ sleep 5m ; cat testfile1.txt

That’s all from this guide!

Conclusion

Linux sleep command is used to delay the executions in the system commands/processes by adding the delay in seconds, minutes, hours, or days; the syntax “sleep number[suffix]” is used. The execution delay can be for single as well as multiple commands. This post has briefly demonstrated the working of the sleep command in Linux with examples.

Источник

How to Use “sleep” Command in Linux [6 Useful Examples]

Linux programmers and system administrators prefer to write shell scripts to automate simple tasks. However, writing a robust script is not an easy task. We need to handle many corner cases, such as – retry mechanism, debugging, logging, error reporting, etc.

In addition to this, many times, we need to simulate a delay in command execution to test timeout scenarios. Similarly, we need to introduce delays in the script to implement the retry mechanism. The sleep command is a perfect solution to handle such scenarios.

In this guide, we will discuss the usage of the sleep command. As the name suggests, the sleep command is used to delay the next command execution. It causes the calling program to sleep for a specified amount of time.

sleep Command Syntax

The syntax of the sleep command is very simple, as it accepts one mandatory parameter with an optional suffix:

It is important to note that, in the above syntax, there isn’t a space between the NUMBER and SUFFIX.

1. How to Delay Linux Command Execution

By default, the sleep command waits for a number of seconds. To understand this, let’s print the current time before and after the sleep command:

For example, the following command waits for 5 seconds after printing the current time:

In this example, we have used a semicolon (;) to separate each command.

Delay Linux Command Execution

2. How to Make a Command to Wait N Minutes

Optionally, the sleep command allows us to specify the time unit using the suffixes. We can use the following suffixes with the sleep command:

  • s – to specify time units in seconds.
  • m – to specify time units in minutes.
  • h – to specify time units in hours.
  • d – to specify time units in days.
Читайте также:  Remote desktop linux kali linux

So, let’s use the ‘m’ suffix to sleep for 1 minute:

Delay Linux Command Specified Time

The important point to note that is there shouldn’t be any space between NUMBER and SUFFIX.

3. How to Make Command Sleep for X Minutes and Seconds

We can use multiple suffixes with the sleep command. In such a case, the time duration is calculated by summing up all the values.

To understand this, let’s use the following command to sleep for 1 minute and 20 seconds:

$ date '+%r'; sleep 1m 20s; date '+%r'

In the above example, we have used two different suffixes. However, we can use the same suffixes as well.

For example, we can use 2s and 3s suffixed to sleep for 5 seconds:

$ date '+%r'; sleep 2s 3s; date '+%r'

Make Linux Command Sleep for X Time

4. How to Use Floating Point Numbers With the sleep Command

Additionally, the sleep command also accepts a floating point number as input. We can use this method to sleep for less than a second.

For example, we can use the 0.5s value to sleep for a half second:

In the above output, we can see that the date command shows the same value for the seconds time unit.

In addition to this, we can use floating values with other suffixes as well. For example, we can use the 0.5m value to sleep for 30 seconds:

Use Floating Point Numbers with Sleep Command

5. How to Simulate Alarm Clock Using sleep Command

In the previous few examples, we saw how to use the sleep command to delay the next command execution. We can use this trick to simulate an alarm clock.

So, let’s use the below command to set alarm after 5 seconds:

Simulate Alarm Clock with sleep Command

The above command opens the alarm.mp3 file using the VLC media player after waiting for 5 minutes. Just like any other Linux command, we can use the ctrl+c key combination to stop the VLC media player.

In this example, we have used the VLC media player but it is possible to use any other Linux media player or sound utility to achieve the same result.

6. How to Simulate Digital Clock with sleep Command

To simulate a digital clock, we can run the sleep command in an infinite loop after every second. Let’s understand this with an example.

First, let’s write a simple shell script called digital-clock.sh with the following code:

#!/bin/bash while [ 1 ] do clear tput cup 5 30 date '+%r' sleep 1 done

In this script, we have used the tput command to place the cursor at the 5th row and 30th column.

Now, let’s run the script and see the result:

$ chmod +x digital-clock.sh $ ./digital-clock.sh

Simulate Digital Clock with sleep Command

Finally, we can use the ctrl+c key combination to stop the script execution.

You might also like:

In this guide, we discussed how to use the sleep command to delay the next command execution. Linux programmers can use sleep commands to write robust shell scripts.

Do you know of any other best example of the sleep command in Linux? Let us know your views in the comments below.

Источник

Оцените статью
Adblock
detector