forked from bewakes/fdswap
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfdswap.sh
executable file
·56 lines (45 loc) · 1.16 KB
/
fdswap.sh
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
52
53
54
55
56
#!/bin/bash
PID='-1' #pid of the required process
file='-1'$2
if [ $file == '-1' ]; then
file=/tmp/stdout
else
file=$2
fi
while read -a line; do
pid=${line[0]}
stat=${line[1]}
cmd=${line[2]}
#grp="grep"
if [ $stat == 'T' ]; then
# && [ "$cmd" != "$grp" ] && [ $$ -ne $pid ]; then
#echo found
PID=$pid
fi
done < <(ps ax -o pid,state,command | grep "$1")
if [ $PID == '-1' ]; then
echo ''
echo No such process is found
echo USAGE: fdswap '<processname> [<redirect file path>]'
echo if no path specified default is /tmp/stdout
echo NOTE: be sure to first stop the process '[ Ctrl + Z ]'
echo ''
return
fi
#echo pid is : $PID
mx=0 # to find out the file descriptor of the new file created
for fd in `sudo ls /proc/$PID/fd`; do
if [ $fd -gt $mx ]; then
mx=$fd
fi
done
mx=`expr $mx + 1` # because new file descriptor is 1 more than the existing max fd
(
echo 'call creat("'$file'", 0600)'
echo 'call dup2('$mx', 1)'
) | sudo gdb -p $PID > tmp
rm tmp
echo output stored in /tmp/stdout
echo running the process $1 in background
bg
echo to see output use '"tail -f '$file'"'