Previous | Table of Contents | Next |
DESCRIPTION
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 .
COMMAND FORMAT
Following is the general format of the sleep command.
sleep [ n ]
Arguments
The following argument may be passed to the sleep command.
n | Specifies the number of seconds to sleep before processing the next command. |
FURTHER DISCUSSION
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 "cj> ( sleep 3600; echo "\007\nMeeting in large conference room!\n" ) &7\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.
NOTE:
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.
RELATED COMMANDS
Refer to the wait command described in Module 152.
APPLICATIONS
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.
TYPICAL OPERATION
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.
cj> until who egrep "billnancy" do echo "Sleeping 60 seconds." sleep 60 done echo "cj> until who egrep "billnancy" do echo "Sleeping 60 seconds." sleep 60 done echo "\007\n billnancy are on"7\n billnancy are on"
loop: who egrep "billnancy" if ($status !=0) then echo "Sleeping 60 seconds." sleep 60 goto loop endif echo "^G^M billnancy are on"
Previous | Table of Contents | Next |