Ubuntu 20.04
python 3.8
I am trying to send a message to slack channel everyday at 9am using crontab.
Following https://www.youtube.com/watch?v=5bTkiV_Aadc&t=482s crontab successfully works when I write to log file every minute.
However replicating same method to slack messaging seems to not work and cannot find a reason for it.
Here is what I've tried out so far (reference: Why crontab scripts are not working?)
First, here is what my code looks like:
from dotenv import load_dotenv
import datetime
import numpy as np
import os
import pandas as pd
from pathlib import Path
import plotly.graph_objects as go
import slack
# ---- Commented out -----
# from extract_data import DataExtraction
# from const import inf_type, slack_channel
# env_path = Path('.') / '.env'
# load_dotenv(dotenv_path=env_path)
# --------------
date = (datetime.date.today() - datetime.timedelta(days=1)).strftime("%Y%m%d")
dir_path = os.path.dirname(os.path.realpath(__file__))
filename = os.path.join(dir_path, 'test_log.log')
# Logger
logger = logging.getLogger(__name__)
logger.setLevel(logging.INFO)
file_handler = logging.FileHandler(filename)
file_handler.setLevel(logging.INFO)
file_handler.setFormatter(logging.Formatter('%(asctime)s - %(levelname)s - %(message)s'))
logger.addHandler(file_handler)
def write_to_log():
logger.info("test")
if __name__ == "__main__":
write_to_log()
Above code works on crontab only if I comment out part when I am using environment variable or importing script that does.
After checking it works for writing to log file, replacing writing to log to sending message to slack doesn't seem to work.
Here is replaced code (all the above code are the same)
def send_msg():
client = slack.WebClient(token="Token_name")
client.chat_postMessage(channel="#slackbot_test", text="helllo")
if __name__ == "__main__":
send_msg()
Currently no luck in searching for if crontab does not work with slack, any help would be appreciated, thanks!