TL;DR: waitdone source.

For quite a while when using Linux I found myself forgetting to chain commands when I wanted to. Using && is simple enough, but I found when I was first getting started with Linux it really didn’t help me very much. I rarely thought about commands that I wanted to run in sequence, and most commands were so fast that I would be spending more time typing them than they would run for anyway. What was to be gained by spending time thinking about it.

As I use Linux more and more, and more importantly found myself using it in ways that pushed the hardware I was using, the value started to be more apparent than an abstract trick. When extracting multiple archives fragmentation and file disk seeking is reduced by running two tar commands in sequence rather than in parallel, or running a second instance of a command that’s resource intensive (for example ffmpeg) is easier to handle after a previous instance was running. The problem of course being that if I started one of these commands, and forgot about the next command, to chain them together required me to stop the first to modify the command.

This started as a frustration, but after enough instances I grew tired of either stopping the original command, or randomly waiting to start the second by hand, and wrote a little bash script I called waitdone. The principle of it’s use is simple enough, suppose I have two ffmpeg calls and I want the second to start after the first has finished. In terminal session A I first make my call to ffmpeg to get it started quickly.

$ ffmpeg -i <input file 1> <some crap> <output file 1>

Now in a second session, I call waitdone on ffmpeg in place of chaining to ffmpeg calls, then chain waitdone with ffmpeg.

$ waitdone ffmpeg && ffmpeg -i <input file 2> <some crap> <output file 2>

The first terminal will run normally, while the second will display a sleep message until all running instances of ffmpeg exit. At that point it will the waitdone will exit cleanly, passing flow onto the second ffmpeg call.

An alternate handy way to use this is in conjunction with something like tmux and the notify-send alias in Ubuntu systems. If ffmpeg (or some other long process) is left in the background, waitdone can be chained into notify-send to visually alert the user when the process exits.

Hopefully someone will get as much use out of this as I have. Source code is below for the interested. -Luke

GIST