-
Notifications
You must be signed in to change notification settings - Fork 0
/
read-fd-progress.sh
executable file
·47 lines (38 loc) · 1.13 KB
/
read-fd-progress.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
#!/usr/bin/env bash
# Check that we have at least a PID
if [ -z "$1" ];then
echo "$0 <PID> [<FD>]"
exit 1
fi
set -eu # Bail on any error
PID=$1
FD=${2:-} # Take FD if we have second parameter, else consider it empty
if [ -z "$FD" ];then
# Show the user the available file descriptors
echo "Select a file descriptor:"
for i in /proc/$PID/fd/*;do
printf " %s: " $(basename $i)
readlink $i
done
read -p "FD: " FD
fi
FSIZE=$(stat -L /proc/$PID/fd/$FD --printf=%s) # Read full file size
while [ 1 ];do
# Stop if the process has finished reading the file
if [ ! -f /proc/$PID/fdinfo/$FD ];then
break
fi
# Read position on file
x=$(cat "/proc/$PID/fdinfo/$FD"|grep pos: |cut -d: -f2|tr -d '\t ')
# Convert that position into a % of the file size
PER10000=$(( $x * 10000 / $FSIZE ))
if [ $PER10000 -le 100 ];then
# Less than 1%
printf " 0.%02i%%\n" $(( $PER10000 ));
else
# More than 1%
printf "%3i.%02i%%\n" $(( $PER10000 / 100 )) $(( $PER10000 % 100 ));
fi
# Wait for the next loop
sleep 1
done