Skip to content

Instantly share code, notes, and snippets.

@rlegene
Created October 16, 2019 07:00
Show Gist options
  • Save rlegene/16820fbd102f88018811f313f11d0e2e to your computer and use it in GitHub Desktop.
Save rlegene/16820fbd102f88018811f313f11d0e2e to your computer and use it in GitHub Desktop.
Calling sleep many times in bash takes one process per time you sleep. Not anymore!
#!/bin/bash
# 20191016 Robert Martin-Legene
# CC0
# Make us a directory just for this pipe
sleepdir=$( mktemp -d )
# Remember to clean up when we're done
tailpid=
trap 'test -n "${tailpid}" && kill ${tailpid} ; rm -rf ${sleepdir}' EXIT
# File name of the actual pipe
pipe="${sleepdir}/boringpipe"
# Create a FIFO pipe
mknod "${pipe}" p
# read(1) hangs in open(2) until there is a
# program already connected to the other end.
tail -fs 3600 /dev/null > "${pipe}" &
tailpid=$!
sleep() {
# Don't let read(1) overwrite data in the main program
local REPLY
read -t $1 < $pipe
}
sleep 1
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment