Skip to content

Instantly share code, notes, and snippets.

@beccasaurus
Last active November 14, 2016 20:07
Show Gist options
  • Save beccasaurus/1fe47d85593c3d52120b1c261738ce85 to your computer and use it in GitHub Desktop.
Save beccasaurus/1fe47d85593c3d52120b1c261738ce85 to your computer and use it in GitHub Desktop.
textme :: Text me after a long-running command finishes

xkcd compiling comic

Do you find yourself frequently getting coffee while you wait for your code to compile? ☕

Test suite take a long time to run?

Application taking awhile to deploy?

Not to worry!

Using textme, you can get a text on your phone when your command finishes running!

Enjoy your

Installing

This tool uses Ruby and Twilio. So first you must install Ruby & create a Twilio account. You'll want to walk through the Twilio signup flow to get a phone number.

Install Ruby Twilio library

gem install twilio-ruby

Download textme

Download and extact textme: textme.zip

Set required environment variables

# Set the number that you would like `textme` to send a text to
export TEXTME_NUMBER="+18005551111"

# Configure Twilio environment variables
export TWILIO_NUMBER="<your-number>"
export TWILIO_ACCOUNT_SID="<your-account-sid>"
export TWILIO_AUTH_TOKEN="<your-auth-token>"

Add the directory where you extracted textme to your PATH so you can call it from anywhere

export PATH="$PATH:/User/alice/Downloads/textme"

Running

Now test it out!

$ testme echo Hello World

Hello World
[Sending text notification]

Tip: add directory to your PATH to run textme from anywhere

TODO: update to send message via curl instead of Ruby
#! /usr/bin/env ruby
# This script should only be called from `textme` script
command, exit_code, start_time, end_time = ARGV
textme_number = ENV["TEXTME_NUMBER"]
twilio_number = ENV["TWILIO_NUMBER"]
twilio_sid = ENV["TWILIO_ACCOUNT_SID"]
twilio_token = ENV["TWILIO_AUTH_TOKEN"]
unless command && exit_code && start_time && end_time && \
textme_number && twilio_number && twilio_sid && twilio_token
puts "Missing required variable for sending text"
exit 1
end
# Helper to humanize time duration
def humanize secs
return "under a second" if secs == 0
[[60, :seconds], [60, :minutes], [24, :hours], [1000, :days]].map { |count, name|
if secs > 0
secs, n = secs.divmod(count)
"#{n.to_i} #{name}"
end
}.compact.reverse.join(" ")
end
require "twilio-ruby"
require "time"
puts "[Sending text notification]"
status = exit_code == "0" ? "succeeded" : "failed"
start_time = Time.parse start_time
end_time = Time.parse end_time
duration = end_time - start_time
text_message = "Command #{status} - took #{humanize duration.to_i}.\n\n#{command}"
twilio = Twilio::REST::Client.new twilio_sid, twilio_token
twilio.messages.create to: textme_number, from: twilio_number, body: text_message
#! /bin/bash
# The first argument is the command to run
COMMAND="$*"
if [ -z "$COMMAND" ] || [ -z "$TEXTME_NUMBER" ]; then
echo "textme :: Text me when you're done!"
echo
echo "Usage: textme <long-running-command>"
echo
echo " # Set phone number environment variable"
echo " export TEXTME_NUMBER=+18005551111"
echo
echo " # Set Twilio account environment variable"
echo " export TWILIO_NUMBER=<your-number>"
echo " export TWILIO_ACCOUNT_SID=<your-account-sid>"
echo " export TWILIO_AUTH_TOKEN=<your-auth-token>"
exit
fi
START_TIME="`date`"
# Execute the command
# Output will stream as normal
$COMMAND
EXIT_CODE=$?
END_TIME="`date`"
# Call Ruby script which sends text using Twilio
_textme_send_sms "$COMMAND" "$EXIT_CODE" "$START_TIME" "$END_TIME"
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment