Skip to content

Instantly share code, notes, and snippets.

@rklemme
Created November 27, 2012 15:22
Show Gist options
  • Save rklemme/4154768 to your computer and use it in GitHub Desktop.
Save rklemme/4154768 to your computer and use it in GitHub Desktop.
Periodically write something to stdin of a console command
#!/usr/bin/bash
# create fifo and ensure cleanup on exit
fifo=fifo-$$
mkfifo "$fifo"
trap 'rm "$fifo"' 0
# other command
cat -n <"$fifo" &
bg=$!
# we write to fd 3
exec 3>"$fifo"
# sleep and signal this process after a while
bgsignal() {
sleep 10 && kill -USR1 $$ &
notif=$!
}
# handle periodic interrupt
trap 'echo save-all >&3; bgsignal' USR1
# start interrupt
bgsignal
# handle termination
trap 'kill -TERM $notif; echo "stop" >&3; exec 3>&-; wait $bg; exit 0' INT QUIT TERM
# main loop: read from stdin and write
# to stdin of the other process
while read -r; do
echo "$REPLY" >&3
done
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment