-
Notifications
You must be signed in to change notification settings - Fork 0
/
watch-download-progress
executable file
·57 lines (53 loc) · 1.61 KB
/
watch-download-progress
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
57
#!/bin/bash
set -eu
if [ -z "${1:-}" -o -n "${2:-}" ] ; then
echo "usage: $0 <poll time in seconds>"
echo
echo "Run this is a download directory where googlegargle/listerine is operating."
echo "Prints status updates and tries to stop listerine in case of dangerously low disk space."
exit 1
fi
interval=$1
# The following might need tuning for non-GNU df(1)s
df_filter() {
sed 's/ */\t/g' | cut -f 4
}
case $(df -P . | df_filter | head -n1) in
Available)
# Okay, we picked the right column from the output
;;
*)
echo "Sorry, I don't know how to parse the output of your df command. Please try to fix me..."
exit 1
;;
esac
free_space_threshold_kb=$(( 500 * 1024 ))
size=0
time=0
while true
do
previous_size=$size
previous_time=$time
size=$(du -kc ? | tail -n1 | cut -f1)
time=$(date +%s)
echo -n "Have $(( $size / 1024 ))M"
echo -n " in $(find . -name '*flv' | wc -l) files,"
kbps=$(( $(( $size - $previous_size )) / $(( $time - $previous_time )) ))
echo -n " growing at ${kbps}k/s."
free_space_kb=$(df -Pk . | df_filter | tail -n1)
echo -n " $(( ${free_space_kb} / 1024 ))M of free space"
if [ $kbps -gt 0 ]; then
echo -n ", will last until $(date -d @$(( $(date +%s) + $free_space_kb / $kbps )))"
fi
echo -n .
if [ -f STOP ]; then
echo " STOP file exists."
else
echo
if [ $free_space_kb -lt $free_space_threshold_kb ]; then
echo "Running out of disk space! Creating STOP file to warn listerine."
touch STOP
fi
fi
sleep $interval
done