Last active
December 25, 2015 23:59
-
-
Save mlgill/7060718 to your computer and use it in GitHub Desktop.
See blog post for more information:
http://themodernscientist.com/posts/2013/2013-10-19-automated_notifications_of_experiment_progress_part_2/
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/bin/zsh | |
#### BEGIN USER CONFIG OPTIONS #### | |
# Path to the data file to watch for modifications | |
local_progress_file="/path/to/file_that_is_periodically_modified" | |
# Interval in minutes to wait between queries | |
# Must be an integer | |
interval_min=30 | |
# User account and hostname of Mac where you will be notified | |
dest_user=your_login_name | |
dest_address=your_mac_hostname | |
# Set to 1 to enable data synchronization or 0 to disable | |
data_sync=1 | |
# Paths for data synchronization | |
local_data_path="/path/to/local/data/directory" | |
remote_data_path="/path/to/remote/data/directory" | |
# Prowl API key | |
prowl_api_key="enter_your_api_key" | |
#### END USER CONFIG OPTIONS #### | |
# Calculate time to wait between checks in seconds | |
(( sleep_sec = $interval_min * 60 )) | |
# Check if the progress file exists | |
if [[ -f ${local_progress_file} ]]; then | |
# Perform checks until the script is killed | |
while ((1)); do | |
# Check if the data file has been modified within the time period | |
mod_file=`find ${local_progress_file} -mmin -${interval_min}` | |
# Set notification message based on modification time of data file | |
if [[ $mod_file == "" ]]; then | |
message_string="NOTE: Experiment is stalled or complete." | |
else | |
message_string="OK: Experiment still running." | |
fi | |
# See if the remote computer is accessible | |
ping -c 1 ${dest_address} >| /dev/null 2>&1 | |
ping_result=$? # Result will be 0 if computer is accessible | |
if [[ $ping_result -eq 0 ]]; then | |
# Synchronize the data if appropriate | |
if (($data_sync)); then | |
rsync --recursive --update --delete --owner --group \ | |
--perms --times --compress --rsh=ssh \ | |
${local_data_path} ${dest_user}@${dest_address}:${remote_data_path} | |
fi | |
# Send the notification to your Mac | |
ssh ${dest_user}@${dest_address} 'echo `/usr/local/bin/growlnotify \ | |
-p high -m "'$message_string'" -t "Experiment Update"`' | |
else | |
# Update notification if data could not be synchronized. | |
if (($data_sync)); then | |
message_string="$message_string Data not synchronized." | |
fi | |
# Send the notification directly to Prowl | |
prowl_output=$(curl -s --data \ | |
"apikey=${prowl_api_key}&application=Experiment Update&event=${message_string}" \ | |
https://api.prowlapp.com/publicapi/add) | |
# Parse the output to get the success/error code | |
# Successive grep commands are used in lieu of other methods for BSD/GNU compatibility | |
prowl_output_code=$(echo $prowl_output | grep -oE 'code="[0-9]+"' | grep -oE '[0-9]+') | |
# Print a message if an error code was returned | |
if [[ $prowl_output_code -ne 200 ]]; then | |
echo "There was an error posting to Prowl: ${prowl_output_code}" | |
fi | |
fi | |
# Wait until it's time to check again | |
sleep $sleep_sec | |
done | |
else | |
# Data file not found | |
echo "File ${local_progress_file} not found." | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment