Previous Table of Contents Next

Module 121


The external sleep command suspends execution for a specified amount of time. The sleep command causes the shell to wait for a certain period of time to elapse before the next instruction or command is processed.


Following is the general format of the sleep command.

     sleep [ n ]


The following argument may be passed to the sleep command.

n Specifies the number of seconds to sleep before processing the next command.


The sleep command is often used in shell scripts to delay the execution of a periodic check. You may also want to delay the execution of one command. For instance, let's say you need to go to a meeting but you also need a printout after a job finishes in 15 minutes. You could use the following code to print the output file to the printer in 30 minutes

     cj> ( sleep 1800; lp -dhplj report1 ) &

This causes the subprocess to sleep for 1800 seconds (30 minutes) before executing the lp command. The ampersand (&) places the subshell in the background.

Another useful example of the sleep command is to notify you of an appointment or other important time-relative need. The following example can be used:

     cj> ( sleep 3600; echo "\007\nMeeting in large conference room!\n" ) &

This subshell will remain in background for about 1 hour. Then it will echo an audible BELL and the message to your terminal.

The seconds specified are an estimate of time. Since the sleep command is a process, it may or may not get enough CPU time per second to perform its time-keeping correctly. Extremely slow response on a system usually causes sleep to overestimate its allocated time.


Refer to the wait command described in Module 152.


The sleep command has two types of uses. The first is to delay the execution of a command. If you want to wait a few minutes before you send something to the printer but you need to leave your office, you can use sleep to delay the print command.

The second use is to use sleep inside a loop. You can have an endless loop that checks or monitors another process. By placing a sleep in the loop you can cause the loop to wait before performing the periodic check. This saves CPU cycles for you and the other users.


In this activity you use the sleep command to delay a loop that checks for certain users logging in to the system. You may want to select user names not currently logged into the system to perform this activity, then repeat it with user names that are logged in to the system. Begin at the shell prompt.

1.  Type the following code, pressing Return at the end of each line. You may wish to place this code in a file and use it as a shell script.
     cj> until who | egrep "bill|nancy"
      echo "Sleeping 60 seconds."
      sleep 60
     echo "\007\n bill|nancy are on"

C Shell
     who | egrep "bill|nancy"
     if ($status !=0) then
      echo "Sleeping 60 seconds."
      sleep 60
      goto loop
     echo "^G^M bill|nancy are on"

If you wish to use this as a shell script, place it in a file (named monitor) using your favorite UNIX editor (vi). Change the modes to 755 and change the string "bill|nancy" to "$1." To execute the new command type monitor "bill|nancy" and press Return.
The $1 allows you to specify a valid egrep expression for monitor to check for in the output from the who command.
2.  This code executes until the user bill or nancy logs in to the system. Therefore, you will need to Break out of the loop by pressing Delete.
3.  Turn to Module 152 to continue the learning sequence.

Previous Table of Contents Next