Connect with us

Website

How to Use Bash Sleep and Why

Published

on

How to Use Bash Sleep and Why

The “sleep” command in the bash terminal is a way to pause the execution of a script. We commonly use it to illustrate the concepts of processes to beginners, but beyond mere demonstrations, it’s quite useful for a variety of applications. In this article, I’ll show you what the sleep command does, how to use it directly from the bash terminal as well as within scripts, and also what utility the sleep command has, and when to use it in serious applications.

Using the Bash Sleep Command

The basic syntax of the sleep command is simple:

sleep [NUMBER][SUFFIX]

Here, [SUFFIX] refers to the units of time for which you want the sleep command to function. It can take the following values:

  1. “s” for seconds
  2. “m” for minutes
  3. “h” for hours
  4. “d” for days

So, for example, the following command:

sleep 60m

Will put the script to sleep for one hour. You could achieve the same thing using this:

sleep 1h

Mostly, though, you won’t be putting your script to sleep for so long, though I’ll discuss such scenarios later.

Running Sleep from the Bash Terminal vs a Script

You can run sleep either directly from the bash terminal, or as part of a script. Using it from the terminal has minimal utility since it’s only for demonstration purposes. But when you incorporate the sleep command into a script, you find that it has multiple uses.

Advertisement

Using sleep in a script puts the entire script on ice for the duration of the command. Using it in bash, however, freezes all input for the specified duration. You can’t do anything more during that time. The only way to interrupt the process is by pressing “Ctrl + c”, which sends a SIGINT interrupt signal and breaks the sleep process.

Using sleep from bash also pauses all the output the terminal would otherwise have received from other processes. As you can see, it’s quite a severe command, and it’s best used only with short durations for this purpose. However, if for whatever reason, you want to run sleep for a long time at bash, you can send it to the background, instead.

Sending Sleep into the Background from Bash

Since using sleep directly freezes the terminal, the preferred way to implement it is to send it into the background. In an earlier article, I talked about using the “nohup” command to prevent a process from shutting down once the user leaves the terminal. In that, I showed how to pair nohup with the sleep command by sending it into the background.

The way to use sleep on bash for a longer period is to append the command with the “&” character to send it into the background, like this:

sleep 60m &

This will create a job that runs the sleep command for one hour in the background and lets you continue working on whatever you want. However, if you’re using a long sleep command in a script, it’s probably better to send the whole script into the background when you run it directly from the terminal, otherwise, bash will freeze for the duration of the script command.

Advertisement

Using the sleep command with the “&” symbol while it’s part of a script can be done, but it has very specific use cases and you probably wouldn’t need to.

Practical Applications of the Sleep Command

The sleep command in bash might seem like a gimmick, but it can be quite useful, both for short as well as long durations. Here are some examples of when to use the sleep command in your own scripts in ways that you might not have considered.

Delaying Script Execution

The most straightforward example of extracting utility from the sleep command is to delay the execution of a script. If you want some portions of your script to execute a little bit after the previous ones, the sleep command is ideal. One example that comes to mind is an interactive text game, for example, where the dialogues are displayed after a couple of seconds. In such situations, the time for which the program would sleep would be very small.

Retrying Script Executions

If a particular command doesn’t work, you can instruct the script to try again and again in a loop, either a set number of times or infinitely. Ideally, though, you’d want to separate these attempts with a short timeout to give the underlying network or resource to become available.

A classic example of this is the “ping” command, which we use to test whether or not a particular IP address or web host is reachable. The standard ping command tries three times and then gives statistics on the packages. Each attempt occurs with a delay of a few seconds in case the previous attempt didn’t succeed.

Advertisement

Scheduling Maintenance

Imagine you have a script that runs constantly in the background, performing some maintenance tasks on the system – let’s say garbage collection. You don’t want it to run too often, so there’s no need to leave the process working throughout. At the same time, you don’t want to give it specific times to run because that may change depending on the context. For example, if we’re talking about a backup script, you might not want the next backup to start immediately after the previous one if the latter took an unusually long time to complete.

The easiest solution is to put the script on a long timer between cleaning attempts so that you get to decide how often it should run. For use cases like this, we would use the sleep command for longer periods. Something like:

sleep 6h

Waiting for Specific Events

For certain real-time applications in the real world with predictable times, you can configure your script to start while coinciding with the event you want to track. For example, if you want your script to display up-to-date market prices, you can check and see when the market opens when the user first fires up the script, and then calculate the time between now and then and instruct the program to sleep up until it’s time to wake up.

Regular Reporting

You can use the sleep command to generate system reports at regular intervals – say every 24 hours. While you could also achieve the same thing by scheduling it via a cron job, this is another alternative you could consider if you don’t want to specify an exact time for generating it.

The one advantage could be that a cron job is based on an external scheduler. At the same time, if you use the sleep command, you can bake the scheduling functionality into your own program and let the user choose the periodicity. This way, there’s less setup, making your script more user-friendly.

Advertisement

As you can see, the sleep command is very versatile, and you can use it in several ways to control the timing of the execution of your program.

Conclusion

Most people are only familiar with the bash sleep command as part of an easy introduction to the Linux terminal, but it could be so much more than that. Once you become aware of the potential uses of the sleep command, you’ll find yourself using it more often in your own code. I hope this article gave you a few useful ideas!

Stephen Oduntan is the founder and CEO of SirsteveHQ, one of the fastest growing independent web hosts in Nigeria. Stephen has been working online since 2010 and has over a decade experience in Internet Entrepreneurship.

Continue Reading
Advertisement
Comments

Trending

Copyright © 2024 SirsteveHQ. All Rights Reserved.