LearnLinux FoundationsTime

sleep

sleep - a hands-on Linux lab on a real virtual machine.

Pause a shell for an exact duration with sleep. Seconds by default, the s/m/h/d suffixes for longer spans, decimals for fractions, and the && chain that proves the wait finished. sleep prints nothing on success, so the drills teach the shape and the two errors (invalid interval, missing operand) that call out a bad or missing duration.

Every command you have met so far tries to DO something and show you the result. ls lists. echo prints. wc counts. This lesson teaches the one command whose entire job is to do nothing at all, for exactly as long as you tell it, and then get out of the way.

That sounds useless until you need it. A script that talks to a slow service has to pause and let the service catch up. A loop that hammers a server needs to space its requests out. A crude timer just needs to wait sixty seconds and then beep. All of those are the same tiny tool, and it is called sleep.

The black boxes in this lesson are a practice terminal: a safe sandbox that checks the one command each step teaches. sleep is unusual. When it works, it prints NOTHING and simply pauses. So watch for the effect, not the text. The one part that changes machine to machine is how long the pause FEELS, a real fraction of wall-clock time. The output stays the same everywhere: empty on success, and a fixed error line when you feed it something wrong.

sleep pauses for a length of time you give it, prints nothing, and then returns control to the shell. That is the whole command. You hand it a duration, it waits that long, and then your prompt comes back.

The word to define here is duration: how long the pause lasts. With sleep the duration is not a flag or an option, it IS the argument. sleep 2 means wait two, sleep 10 means wait ten. There is nothing else to configure.

Think of sleep like the pause button on a remote. It does not change the channel or the volume. It just holds everything still for a set number of seconds, then lets the show carry on.

Run the smallest useful pause. Two seconds. Type it, press Enter, and watch what happens to your prompt:

sleep 2

prompt: student@linuxcamp:~$ answer: sleep 2 output: hint: Type the word sleep, a space, then the number 2: sleep 2

Nothing printed, and that is correct. For about two seconds your prompt just sat there, then it came back on its own. The exact feel of the pause is the only thing that varies from machine to machine, and here it is close to two seconds. What is fixed everywhere is the shape: no output, then the prompt returns.

That empty box is the whole point. sleep does not announce that it is waiting and does not count down. It goes quiet, holds the shell for the duration you gave, then hands the prompt back exactly as it found it. A blank result from sleep is success, not a mistake. If you ever want proof it finished, chain a command after it, which you will do in a moment.

Plain sleep 5 waits five SECONDS, because seconds are the default unit. But sleep also understands longer spans if you add a one-letter suffix to the number. No space between the number and the letter.

There are four suffixes, and they read like plain English: s for seconds, m for minutes, h for hours, d for days. So sleep 1m waits one minute, sleep 1.5h waits an hour and a half, and sleep 90s and sleep 1.5m both wait the same ninety seconds.

You can also pass a decimal for a fraction of a second. sleep 0.5 waits half a second, which is the kind of tiny pause you sprinkle into a loop so it does not run away.

Ask for a one-minute pause using the minute suffix. Same command, a unit letter added:

sleep 1m

prompt: student@linuxcamp:~$ answer: sleep 1m output: hint: The number 1 with the letter m stuck right after it, no space: sleep 1m

Again the box is empty, exactly as before. The only difference from sleep 2 is the length of the wait, one minute instead of two seconds, and the length is the part that changes with the duration you type. The lesson is the suffix: m turned the plain number into minutes.

Suffixes add up when you list more than one duration. sleep 1m 30s waits ninety seconds total, because sleep sums every argument you give it. It is the same idea as saying one minute and thirty seconds out loud.

An empty pause is hard to feel confident about. The classic way to SEE sleep do its job is to chain another command after it with &&. You met && earlier: it runs the command on the right only after the command on the left finishes successfully.

So sleep 2 && echo done says: wait two seconds, and only then print the word done. The word does not appear right away. It appears after the pause, which is sleep proving it waited.

Run it and watch the timing. The prompt sits quiet, then done prints, then the prompt returns:

sleep 2 && echo done

prompt: student@linuxcamp:~$ answer: sleep 2 && echo done output: done hint: Combine what you know: sleep 2, then two ampersands, then echo done. sleep 2 && echo done

The delta is WHEN done showed up. If you typed echo done on its own it would print instantly. Here it waited for sleep 2 to finish first, so the word arrived about two seconds late. That gap you felt is sleep doing its one job. This exact pattern, pause then act, is how scripts pace themselves: wait for a service to boot, then check on it; wait a beat between requests, then send the next one.

sleep is forgiving in what it prints on success, but strict about what it accepts. There are two mistakes almost everyone makes at least once, and each has its own error line worth recognising on sight.

First, a duration that is not a number. sleep only understands numbers, optionally with a unit suffix. Hand it a bare word and it refuses. Try telling it to sleep for abc:

sleep abc

prompt: student@linuxcamp:~$ answer: sleep abc output: sleep: invalid time interval 'abc' Try 'sleep --help' for more information. hint: Type sleep, a space, then the letters abc: sleep abc

The line sleep: invalid time interval 'abc' means exactly what it says: the thing you handed sleep as a duration was not a valid time. The quoted part echoes back the bad value so you can spot the typo. First thing to check: did you pass a real number, and if you used a suffix, was it one of s, m, h, d with no space before it. A stray letter, a comma instead of a dot, or 2 s with a space all land here.

The second common mistake is leaving the duration off. sleep cannot guess how long to wait, so with no argument at all it stops and tells you a piece is missing. Run sleep bare, with nothing after it:

sleep

prompt: student@linuxcamp:~$ answer: sleep output: sleep: missing operand Try 'sleep --help' for more information. hint: Just the word sleep on its own, nothing after it: sleep

The word operand simply means the value a command acts on, here the duration. sleep: missing operand is sleep telling you the one thing it needs was not there. First thing to check: did you actually type a number after sleep. This is the single most common sleep mistake, usually a duration that got deleted while editing a script line.

Scaffolding off. No command is printed this time. You have every piece you need.

You want to wait a short, exact moment and then signal that the wait is over. Specifically: pause for five seconds, and the instant the pause ends, print the word ready. Nothing should appear until the five seconds have passed.

prompt: student@linuxcamp:~$ answer: sleep 5 && echo ready output: ready hint: Combine the pause with the chain you learned: a sleep for five seconds, then && , then an echo of the word ready.

That is the everyday pattern in one line. sleep 5 held everything for five seconds, && waited for that pause to finish successfully, and only then did echo ready print. The word ready arrived about five seconds late, and its lateness is the proof the pause happened. Your exact wait may feel a hair over or under five seconds. But the shape, silence then the word, is what you will see on any machine. The command is exactly what a working engineer types to pace a script.

You earned this cheat sheet. Every row is a form of sleep you just ran:

And the two error lines you can now read on sight: sleep: invalid time interval '...' means the duration was not a number, and sleep: missing operand means you forgot the duration entirely. Both point you straight at the argument, which is the only thing sleep cares about.

sleep waits blind: it does not care WHETHER the thing you are waiting for is actually ready, only that the clock ran out. That is why it is called a crude timer. For pausing a script a fixed beat it is perfect, and it is the pacing tool you will reach for first.

The practice terminal has shown you the shape of sleep. You have the bare number for seconds, the s, m, h, d suffixes for longer spans, and decimals for fractions. You have the && chain that proves the wait finished, and the two errors that call out a bad or missing duration. Every one of those you typed yourself.

The Time module ends with one real Linux machine, the Time capstone mission, and that is where you use sleep for real. A VM boots just for you, and it hands you objectives that use exactly what you practiced across this module: timing a command, watching it repeat, pausing between steps, and counting through a range. One difference from here: the mission shows no commands. You read the objective, you recall the command, you type it. That recall is what makes it stick.

Finish the other Time lessons, then go pace a real machine for yourself.

Practice sleep in a real Linux terminal at The Linux Camp. Progress is verified automatically as you type commands on the machine.