Often times software engineers, data scientists and data engineers will run programs that take quite a while to finish. During execution, they’ll context switch to another task while waiting for results. Instead of manually performing periodic check ins on the program, to see if it finished, wouldn’t it be great if we were notified upon completion?

Phone Notification via Pushover

I’m a big fan of the tool Pushover. By having their application installed on my phone, I can make a simple HTTP REST call, and have a notification be fired off. This is a lovely thing you can tie into a long running process, or even home automation. Here’s how I often achieve this with say Python:

import os
import http.client, urllib
from dotenv import load_dotenv

load_dotenv()

conn = http.client.HTTPSConnection("api.pushover.net:443")
conn.request('POST', '/1/messages.json',
  urllib.parse.urlencode({
    'token': os.environ['PUSHOVER_APP_TOKEN'],
    'user': os.environ['PUSHOVER_USER_KEY'],
    'message': 'hello world',
  }), { 'Content-type': 'application/x-www-form-urlencoded' })
conn.getresponse()

See more code examples here.

Audio Alert

Using a simple bash program, with no external dependencies, we can get an audible alert upon the program’s completion, with the status of “finished without error” or “finished with error”.

On Linux we’ll leverage the program spd-say and on OSX we can leverage say, both of which take in a string and will read them on the default audio out device. I’ll assume you’re on Linux for the rest of the post, but you can just swap spd-say with say if you’re on OSX. As for Windows, I cannot comment on how you’d achieve this but I imagine its doable, so I’ll leave that as an exercise for the reader :)

This command:

$ if [ $? -ne 0 ]; then spd-say 'Error!'; else spd-say 'Finished!'; fi

Will inspect the return value of the previous command, using $? and use that result to perform conditional branching. Saying “Error” or “Finished”. So we can run our long command and have this command be ran afterwords like this:

$ ./long-running-program.sh; if [ $? -ne 0 ]; then spd-say 'Error!'; else spd-say 'Finished!'; fi

To make this command easier to remember and type, I’d suggest making it an alias in your bash/ZSH rc file, like this:

# announce finished when done successfully otherwise announce error
alias annouce='if [ $? -ne 0 ]; then spd-say 'Error!'; else spd-say 'Finished!'; fi'

then we’d leverage it by running:

$ ./long-running-program.sh; announce

While this approach only works when you’re running the program locally, I imagine it will still be useful for a number of people. In the future we can look into an external service to send us an alert via text, email or push notification.

Slack Notification

Slack makes it incredibly easy to send a notification to a channel or user programmatically. One can login as an admin to their Slack workspace and create a web hook URL. At that point one can simply make a rest POST request to that URL. A simple breakdown of the steps are provided by slack, but I’ll copy them here for convenience:

  1. Create a new Slack app in the workspace where you want to post messages.
  2. From the Features page, toggle Activate Incoming Webhooks on.
  3. Click Add New Webhook to Workspace.
  4. Pick a channel that the app will post to, then click Authorize.
  5. Use your Incoming Webhook URL to post a message to Slack.
  6. Note: Check out our Slack API documentation for more details about using incoming webhooks.

I ended up posting to my Slack Webhook URL in Python code using the popular requests library, which allows me to have my home server alert me during specific events.

Email

The code is pretty gross and could definitely be improved, but that section of my toolbox also has a function for sending an email.