Created
September 16, 2015 04:38
-
-
Save yamachu/4f3cc8763fe12aa3c58d to your computer and use it in GitHub Desktop.
スクリプト終了通知が欲しかった 参考:http://qiita.com/tt2004d/items/50d79d1569c0ace118d6
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/sh | |
# usage 長時間かかるスクリプトの最後に echo 'finish' | slack_notify $0とか | |
set -eu | |
#Incoming WebHooksのURL | |
WEBHOOKURL="ここにIncoming Webhooks IntegrationのURLをいれてね" | |
#メッセージを保存する一時ファイル | |
#mktempで名前つけた時.Xの数が足りないと怒られたから適当につけた | |
MESSAGEFILE=$(mktemp -t webhooks.XXXXX) | |
trap " | |
rm ${MESSAGEFILE} | |
" 0 | |
usage_exit() { | |
echo "Usage: $0 [-m message] [-c channel] [-i icon] [-n botname]" 1>&2 | |
exit 0 | |
} | |
while getopts c:i:n:m: opts | |
do | |
case $opts in | |
c) | |
CHANNEL=$OPTARG | |
;; | |
i) | |
FACEICON=$OPTARG | |
;; | |
n) | |
BOTNAME=$OPTARG | |
;; | |
m) | |
MESSAGE=$OPTARG"\n" | |
;; | |
\?) | |
usage_exit | |
;; | |
esac | |
done | |
#slack 送信チャンネル | |
CHANNEL=${CHANNEL:-"#scriptnotify"} | |
#slack 送信名 | |
BOTNAME=${BOTNAME:-"scriptnotifier"} | |
#slack アイコン | |
FACEICON=${FACEICON:-":ghost:"} | |
#見出しとなるようなメッセージ | |
#slackのユーザー名と研究室のユーザー名が一緒だったからこれで個人リプライみたいな | |
#以前アップしたget_fullpathを使ってどのスクリプトが終わったか通知 | |
MESSAGE=${MESSAGE:-"@$USER\n"`get_fullpath ${1}`"\n"} | |
if [ -p /dev/stdin ] ; then | |
#改行コードをslack用に変換 | |
cat - | tr '\n' '\\' | sed 's/\\/\\n/g' > ${MESSAGEFILE} | |
else | |
echo "nothing stdin" | |
exit 1 | |
fi | |
WEBMESSAGE='```'`cat ${MESSAGEFILE}`'```' | |
#Incoming WebHooks送信 | |
curl -s -S -X POST --data-urlencode "payload={\"channel\": \"${CHANNEL}\", \"username\": \"${BOTNAME}\", \"icon_emoji\": \"${FACEICON}\", \"text\": \"${MESSAGE}${WEBMESSAGE}\" }" ${WEBHOOKURL} >/dev/null |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment