Created
November 27, 2012 15:22
-
-
Save rklemme/4154768 to your computer and use it in GitHub Desktop.
Periodically write something to stdin of a console command
This file contains hidden or 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
#!/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