- Line notify의 서비스 만료로, Jandi Messenger 의 웹훅을 사용해서 메세지 보내기
- pyenv 환경에서 shell script 로 notebook 파일을 실행하고, 실행결과를 저장
- crontab 에 등록해서 정기적으로 실행
#1. notebook 파일에서 Jandi Messenger 로 메세지 보내기
게만아님 강의에서 사용했던 Line notify가 서비스 만료된다는 소식
Slack 도 쉽게 붙일 수 있지만, 업무관련 메세지가 많이 와서 Jandi Messenger 의 웹훅을 사용할 예정
jandi_alert.py
import json, requests
def SendMessage(message):
webhook_url = 'https://wh.jandi.com/connect-api/webhook/0000/000000000'
msg_data = {'body': str(message)}
response = requests.post(
webhook_url, data=json.dumps(msg_data),
headers={'Content-Type': 'application/json', 'Accept': 'application/vnd.tosslab.jandi-v2+json'}
)
send_message.ipynb
# notebook 파일 실행위치 확인용. (import 파일 못찾을 때)
# ! pwd
from datetime import datetime
import jandi_alert
now = datetime.now()
# 현재 날짜와 시간을 메세지로 보내기
formatted_time = now.strftime("%Y-%m-%d %H:%M:%S")
jandi_alert.SendMessage(formatted_time)
jupyter lab 에서 위 send_message.ipynb 파일을 실행해서 메세지 오는지 확인
#2. crontab 용 shell script 작성
- notebook 파일을 shell 에서 실행하기 위해 papermill 을 사용할 예정
- papermill 설치하고 실행 테스트
# papermill 설치
$ pip install papermill
# send_message.ipynb 를 실행하고, 결과를 ./output/send_message.ipynb 에 저장
$ papermill send_message.ipynb ./output/send_message.ipynb
send_message.sh
#!/bin/bash
# pyenv 실행위한 설정
export PYENV_ROOT="/home/ubuntu/.pyenv"
[[ -d $PYENV_ROOT/bin ]] && export PATH="$PYENV_ROOT/bin:$PATH"
eval "$(pyenv init - bash)"
# pyenv 실행위한 설정
cd /home/ubuntu/apps
papermill ./send_message.ipynb ./output/send_message_$(date +%Y%m%d_%H%M%S).ipynb
shell 에서 위 파일 실행 테스트
$ chmod +x send_message.sh
$ ./send_message.sh
#3. crontab 에 등록해서 자동실행
$ crontab -e
# 아래 내용을 제일 하단에 추가
* * * * * /home/ubuntu/apps/send_message.sh
자동으로 메세지 보내기 성공!
#4. 그밖에
jupyter lab 에서 웹기반으로 Terminal을 사용할 수 있다.
aws 콘솔에서 웹기반 터미널 접속을 사용하는 것과 비슷한 경험을 제공해서 매우 편리하다.
보안은 클라우드의 방화벽을 사용해서, 포트와 ip주소를 제한하고 사용할 예정