SHARE
TWEET

Untitled

a guest Jul 23rd, 2019 54 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Place this in your .bashrc
  2. # Covers several corner cases such as nested apostrophes, history extraction in screens/subshells, Slack being down, etc.
  3. # Strings to replace with your own credentials:
  4. ## {your email address} (1 instance)
  5. ## {slack webhook url} (1 instance)
  6. ## {your computer name} (2 instances)
  7.  
  8. slack() {
  9.     # Get slack message when a command exits
  10.     # Example usage: python long_job.py; slack
  11.     # Example usage: python long_job_1.py; slack 'job 1 done'; python long_job_2.py; slack 'job 2 done';
  12.  
  13.     errorcode=$? # This has to go first; extracts error code
  14.     if [ -z "$1" ] # Allows you to attach a message as an argument for multi-step commands
  15.       then
  16.         message=" "
  17.       else
  18.         message=" with message '$1' "
  19.     fi
  20.     if [[ $errorcode -eq 0 ]] # Add emoji based on error code
  21.       then
  22.         erroremoji=":white_check_mark:"
  23.       else
  24.         erroremoji=":x:"
  25.     fi
  26.     myhistory=$(history | tail -n1 | sed 's/^ *[0-9]* *//g; s/\"/\\\"/g;') # Extract command from history
  27.     mydata='{"text":"`[{your computer name}:'$PWD']$ '$myhistory'` exited with status '$errorcode' '$erroremoji'"}'
  28.     returnstatus=$(curl -X POST -H 'Content-type: application/json' --data "$mydata" {slack webhook url})
  29.     if [[ $returnstatus == 'ok' ]] # Catch case when Slack is down or error message is syntactically invalid
  30.       then
  31.         echo 'Success'
  32.       else
  33.         echo 'bad request'
  34.         $(echo "Slack is down or message is invalid. Process finished at '$PWD'." | mail -s '{your computer name}: Slack is down or requested message is invalid.' '{your email address}')
  35.     fi
  36. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Not a member of Pastebin yet?
Sign Up, it unlocks many cool features!
 
Top