19e36a7bb1fe9954c37d46c0e87fc522176e8cde
[p2p-testing-infrastructure.git] / ControlScripts / clients / transmission / detect_complete_transmission.sh
1 #!/bin/bash
2 #
3 # 2011, Calin-Andrei Burloiu, calin.burloiu@gmail.com
4 #
5 # Bash script used to detect when a regular client finished
6 # downloading a torrent
7 #
8 # The script
9 #  * monitors a log file provided as an argument
10 #  * as long as the doe is not finished the script keeps running
11 #  * when the doe is finished it returns 0
12 #  * blocks the caller - the script exits only when the doe has completed
13 #                        the download
14 #
15 # Script arguments:
16 #  * log file
17
18 if [ ! $# -eq 1 ]; then
19         echo "usage: $0 log-file"
20         exit 1
21 fi
22
23 LOG_FILE=$1
24
25 while true; do
26         # check for complete transfer
27         if cat $LOG_FILE | grep "Torrent finished" &> /dev/null; then
28                 echo "Torrent transfer finished"
29                 exit 0
30         fi
31
32         if cat $LOG_FILE | grep "Segmentation fault" &> /dev/null; then
33                 echo "Segmentation fault when transferring torrent"
34                 exit 0
35         fi
36
37         # check stat client is alive
38         if ! pgrep "hrktorrent" &> /dev/null; then
39                 if ! pgrep -f "schedule_client.sh" &> /dev/null; then
40                         echo "No hrktorrent and no schedule_client.sh process"
41                         exit 0
42                 fi
43         fi
44
45         # Don't do continuous polling
46         sleep 5
47 done