echo ./swift -h "$HASH" -t "${TRACKER_IP}:6881" -p > ~/deb
 ./swift -h "$HASH" -t "${TRACKER_IP}:6881" -p
 
+#
+#./swift -h "$HASH" -t "${TRACKER_IP}:6881" -p &
+# ps -ef | grep shedule bla bla # proper way of determining PID of schedule (master) process
+# kill -USR1 $SCHEDULE_PID # SCHEDULE_PID is pid for master process
+#
+# wait # wait for client process to complete
+#
+
 rm -f ${HASH}*
 
        fi
 }
 
+cleanup()
+{
+       stop_client ${CLIENT_TYPE}
+       kill ${mon_pid}
+}
+
+# use cleanup function when receiving signals
+trap cleanup 0 1 2 3 15
+
 parse_periods
 
 DEBUG echo "start_time is ${start_time}"
 start_client ${CLIENT_TYPE} ${DL_BW} ${UL_BW} ${NO_CONNECTIONS}
 background_pid=$!
 DEBUG echo "background_pid is ${background_pid}"
+
+# wait_for_usr1()
+# {
+#   # do nothing, successfully
+# }
+#
+# trap wait_for_usr1 10 # wait_for_usr1 is simple handler (waits for SIGUSR1 from start_client process)
+#
+
+sleep 5
+client_pid=$(pgrep -P ${background_pid})
+DEBUG echo "client_pid is ${client_pid}"
 old_time=${start_time}
 
+# Monitor client's resources
+echo "$client_pid"
+./hook_monitor_pid.sh "$client_pid" >> $REMOTE_PATH/P2P-Testing-Infrastructure/ClientWorkingFolders/TmpLogs/$(hostname).log.mon 2>&1 &
+mon_pid=$!
+
 for ((i = 0; i < ${#suspend_resume[@]}; i += 2)); do
        # Sleeping while the client runs. When i wake up i will suspend the client
        test_infinite ${suspend_resume[$i]}
        DEBUG echo "client_pid is ${client_pid}"
        suspend_client ${client_pid}
        old_time=${suspend_resume[$i]}
-
-       # Monitor client's resources
-       ./hook_monitor_pid $client_pid >> $REMOTE_PATH/P2P-Testing-Infrastructure/ClientWorkingFolders/TmpLogs/\$(hostname).log.mon 2>&1 &
-
+       
        # Client is suspended. Sleeping until the client should be restarted. When i wake up i will start the client
        test_infinite ${suspend_resume[$(($i+1))]}
 
 
 to_sleep=$((${stop_time} - ${old_time}))
 sleep ${to_sleep}
-stop_client ${CLIENT_TYPE}
+
+# stop client and monitoring script
+cleanup