Delayed Scheduling of Processes

In most cases, users want processes to start immediately and finish quickly. The shell therefore normally starts each command as soon as the previous one finishes. Command completion speed is essentially resource-limited, and beyond the shell's purview.

In interactive use, it is sometimes unnecessary to wait for one command to complete before starting another. This is so common that the shell provides a simple way to request it: any command that ends with an ampersand is started in the background, but not waited for. In those rare cases in which you need to wait for backgrounded processes to complete, simply issue the wait command, as described in Section 13.2.

There are at least four other situations when it is desirable to delay process start until a future time; we treat them in the following subsections.

sleep: Delay Awhile

When a process should not be started until a certain time period has elapsed, use the sleep command to suspend execution for a specified number of seconds, then issue the delayed command. The sleep command uses few resources, and can be used without causing interference with active processes: indeed, the scheduler simply ignores the sleeping process until it finally awakes when its timer expires.

We use a short sleep in Example 13-1 and Example 13-3 to create programs that have an infinite loop, but do not consume all of the machine's resources in doing so. The short sleep in Section 9.10, ensures that a new pseudorandom-number ...

Get Classic Shell Scripting now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.