Created
October 16, 2019 07:00
-
-
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!
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/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