Skip to content

Instantly share code, notes, and snippets.

@tizot
Last active March 24, 2017 11:20
Show Gist options
  • Save tizot/88b9ed001e78a3e0e196f414352368e3 to your computer and use it in GitHub Desktop.
Save tizot/88b9ed001e78a3e0e196f414352368e3 to your computer and use it in GitHub Desktop.
Script to send a notification on Pushbullet when a command has finished. See details in comment.
#!/usr/local/bin/python3
from argparse import ArgumentParser
from socket import gethostname
import subprocess
import arrow
import requests
# Usage:
# monitor ping -c 5 google.com
PUSH_KEY = 'PUSHBULLET_API_KEY' # change me!
def main(command, params):
begin_time = arrow.utcnow()
push_data = {'type': 'note'}
try:
ret = subprocess.check_call([command] + params)
push_data['title'] = 'La commande "%s" a terminé.' % command
except subprocess.CalledProcessError as err:
push_data['title'] = 'La commande "%s" a terminé avec une erreur...' % command
duration = arrow.utcnow().humanize(begin_time, locale='fr_fr', only_distance=True)
push_data['body'] = "Hostname : %s\nTemps d'exécution : %s" % (gethostname(), duration)
push_headers = {'Access-Token': PUSH_KEY}
r = requests.post('https://api.pushbullet.com/v2/pushes', data=push_data, headers=push_headers)
if __name__ == "__main__":
parser = ArgumentParser()
parser.add_argument('command')
args = parser.parse_known_args()
cmd = args[0].command
params = args[1]
main(cmd, params)
@tizot
Copy link
Author

tizot commented Mar 24, 2017

Place this file into your /usr/local/bin, make it executable (chmod +x monitor). Ensure that you have python3 with the modules arrow and requests (installable with pip install arrow requests).

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment