Waiting for things to happen on Linux

If you don't want to stop everything you're doing to wait for some other Linux workload to complete, custom scripts and the bash 'wait' built-in can set you free.

A stressed employee works alone in a dimly lit office.
Cicilie Arcurs / Getty Images

There are always things to wait for on a Linux system—upgrades to complete, processes to finish, coworkers to log in and help resolve problems, status reports to be ready.

Fortunately, you don’t have to sit twiddling your thumbs. Instead, you can get Linux to do the waiting and let you know when the work is done. You can do this with a script or you can use the wait command, a bash built-in that watches for processes running in the background to complete.

Crafting waiting within scripts

There are many ways to craft waiting within a script. Here’s a simple example of simply waiting for a period of time before moving on to the next task:

#!/bin/bash
# waiting for 11 seconds

num=0

while [ $num != 11 ]
do
  echo hmmm
  sleep 1
  ((num++))
done

# add commands here

The “!= 11” portion of the commands keeps the loop going as long as this condition is true.

In this next example, the script waits for a third person to log into a Linux system before letting the person running the script know that this has happened.

#!/bin/bash
# waiting for a 3rd person to log in

while [ `who | wc -l` -lt 3 ]
do
  echo waiting
  sleep 5
done

The “-lt 3” portion of the while command sets a “while fewer than three people are logged in” condition.

This next script waits for another script to be both available and executable before it attempts to run it.

#!/bin/bash
# waiting for a script to be executable

script=~/myscript2

while [ ! -x $script ]
do
  echo waiting
  sleep 5
done

echo $script is ready to run

$script

Using the wait built-in

You can also wait for a process to finish by using the bash wait builtin. In this case, the process should be run in the background and the wait command provided with the process ID.

$ loop2 &
[1] 12345
wait 12345
[1]+  Done

To view the man page information on the wait built-in, you can type “man wait” and page through all the content that proceeds this particular built-in or use a command like this which should get you close to the right spot.

$ man wait | tail -165 | head -25
       wait [-fn] [-p varname] [id ...]
              Wait  for  each specified child process and return its termina‐
              tion status.  Each id may be a process ID or a  job  specifica‐
              tion; if a job spec is given, all processes in that job’s pipe‐
              line are waited for.  If id is not given, wait  waits  for  all
              running background jobs and the last-executed process substitu‐
              tion, if its process id is the same as $!, and the return  sta‐
              tus  is  zero.   If the -n option is supplied, wait waits for a
              single job from the list of ids or, if no ids are supplied, any
              job,  to  complete and returns its exit status.  If none of the
              supplied arguments is a child of the shell, or if no  arguments
              are  supplied  and  the shell has no unwaited-for children, the
              exit status is 127.  If the -p option is supplied, the  process
              or  job  identifier of the job for which the exit status is re‐
              turned is assigned to the variable varname named by the  option
              argument.  The variable will be unset initially, before any as‐
              signment.  This is useful only when the -n option is  supplied.
              Supplying  the  -f  option, when job control is enabled, forces
              wait to wait for id to terminate before returning  its  status,
              instead of returning when it changes status.  If id specifies a
              non-existent process or job, the return status is 127.  If wait
              is  interrupted  by a signal, the return status will be greater
              than 128, as described under SIGNALS  in  bash(1).   Otherwise,
              the return status is the exit status of the last process or job
              waited for.

Wrap-Up

Having Linux inform you when a process has completed, a file is ready or some condition has been met can be very useful, especially when you have a lot of other things to get done and, well, when isn’t that the case? Bookmark this post so it can remind you of a number of ways to make the waiting work best for you.

Related:

Copyright © 2023 IDG Communications, Inc.

The 10 most powerful companies in enterprise networking 2022