- How to Become a Chief Information Officer: CIO Cheat Sheet
- 3 handy upgrades in MacOS 15.1 - especially if AI isn't your thing (like me)
- Your Android device is vulnerable to attack and Google's fix is imminent
- Microsoft's Copilot AI is coming to your Office apps - whether you like it or not
- How to track US election results on your iPhone, iPad or Apple Watch
How to wait for things to happen on Linux
There are always things to wait for on a Linux system – for upgrades to complete, for a process to finish, for coworkers to log in and help resolve a problem, or for a status report 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 by crafting the waiting and the condition for which you are waiting in a script, or you can use the wait command – a bash builtin that will watch for a process 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 less than three people are logged in” condition.
The next script waits for a 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 builtin
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 builtin, you can type “man wait” and page through all the content that proceeds this particular builtin 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 when isn’t that the case? This post describes a number of ways to make the waiting work best for you.
Copyright © 2023 IDG Communications, Inc.