-
Notifications
You must be signed in to change notification settings - Fork 73
/
relink
executable file
·52 lines (46 loc) · 1.04 KB
/
relink
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#!/bin/sh
# vim: set sw=4 expandtab:
#
# SPDX-License-Identifier: GPL-2.0-or-later
# Copyright 2017, Jérôme Pouiller <[email protected]>
#
usage() {
echo 'Usage: relink PID'
echo 'Redirect outputs of a running program as if it was output of this command.'
echo 'On exit, this script automatically detach from PID, original output of PID'
echo 'is restored, and PID continue to run normaly'
echo
echo 'Exemples:'
echo ' relink $(pidof dhclient)'
echo ' relink $(pidof dhclient) | grep usefull_line'
}
# detach
atexit() {
sh $RESTORE_SCRIPT
rm $RESTORE_SCRIPT
# Note: no need to kill 'cat'
rm $FIFO_OUT
rm $FIFO_ERR
exit 0
}
trap atexit INT
if [ "$#" -ne 1 ]; then
usage
exit 1
fi
PID=$1
FIFO_OUT=$(mktemp -u)
FIFO_ERR=$(mktemp -u)
RESTORE_SCRIPT=$(mktemp -u)
mkfifo $FIFO_OUT
mkfifo $FIFO_ERR
cat $FIFO_ERR >&2 &
cat $FIFO_OUT &
CAT_PID=$!
reredirect -o $FIFO_OUT -e $FIFO_ERR $PID > $RESTORE_SCRIPT || atexit
wait $CAT_PID
# $PID has stopped
rm $RESTORE_SCRIPT
rm $FIFO_OUT
rm $FIFO_ERR
exit 0