Created
March 3, 2015 02:01
-
-
Save tbl3rd/dbe0bed1b779fc6cf9dc to your computer and use it in GitHub Desktop.
system programing in bash
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
#!/bin/bash | |
# This script is designed to make sure that a signal delivered to it | |
# is propagated to a subshell running the program feeding the lines to | |
# be timestamped. The usual command line is: | |
# | |
# timestamp command ... arguments ... | |
# The 'builtin' specifications are not necessary, but are included to | |
# convey that what follows them are internal shell commands, so bash | |
# doesn't spawn subshells to run them. | |
# Start a subshell running "$@" (impitool ...) and read its stdout (<) | |
# from FD 3 (exec 3< ...). Use (exec "$@) so the PID captured by $! | |
# is the command line "$@" (ipmitool ...) and not the bash process | |
# handling the command line. | |
# | |
exec 3< <(exec "$@") | |
# Trap HUP, INT, and TERM ($sn) to kill the subshell running "$@" | |
# (kill $!), then remove the $sn trap and kill this process ($$) with | |
# $sn so whatever is waiting for this process sees $sn in this | |
# process's exit status. | |
# | |
for sn in HUP INT TERM | |
do | |
trap "builtin kill -$sn $!; trap $sn; builtin kill -$sn $$" $sn | |
done | |
# Read raw (-r) lines from FD 3 (-u3) into the variable $line, then | |
# print the line preceeded by a timestamp in brackets ([<ts>]: ...). | |
# The cut removes the 3 least significant digits of the nanosecond | |
# field (%N) formatted by date. | |
# | |
while IFS= read -r -u3 line | |
do | |
builtin printf '[%s]: %s\n' \ | |
"$(date +%Y-%m-%dT%H:%M:%S.%N%:z | cut -c1-26,30-35)" \ | |
"$line" | |
done |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment