Spinning while sleeping

I have some long-running scripts that sleep while waiting for something to happen. It’d be nice to look at the terminal and know whether the process is working or sleeping. To that end, I replaced some of the sleep(1) invocations with spinner.py, which shows a simple spinner while sleeping.

Now, David R. MacIver pointed out that I of course should’ve enhanced sleep(1) with a LD_PRELOAD hack. LD_PRELOAD is an environmental variable that tells the Linux dynamic loader to look at given shared objects before loading anything else. This allows, among other things, overwriting calls to the C standard library.

I’ve never used LD_PRELOAD before, but it turned out to be simple! Create a shared library with a function with the name and signature of the function you want to overwrite, point LD_PRELOAD to it and you’re done.

A quick search for gnu sleep source tells me that sleep(1) uses nanosleep(2), so that’s the function I rewrote:

Making this work on OS X is left as an exercise for the reader.

Comments or questions? Tweet to me or send me an e-mail.