Home > Software design >  Post real time output to Slack with bash script
Post real time output to Slack with bash script

Time:08-02

I have a python script that I am executing with cron job. This script generates some output while executing and I wish to post it to Slack channel in real time.

Here is a bash script that I have:

#!/bin/bash

log_file=logs_$(date ' \%Y-\%m-\%d_\%H:\%M').txt

cd /path/to/script/run.py > /path/to/logs/${log_file} 2>&1 

cat /path/to/logs/${log_file} | while read LINE; do
  (echo "$LINE" | grep -e "Message" ) && curl -X POST --silent --data-urlencode \
    "payload={\"text\": \"$(echo $LINE | sed "s/\"/'/g")\"}" "https://hooks.slack.com/services/xxxxxx";
done

This scrip works but it of course posts all messages to slack once the python script has already been executed. Is there any way I could configure it so that messages would be sent to Slack in real time while the python script is still being executed?

CodePudding user response:

You may be able to read the output from your run.py script via process substitution:

#!/bin/bash

log_file=logs_$(date ' \%Y-\%m-\%d_\%H:\%M').txt

while read -r line ; do
    echo "$line"
    (echo "$line" | grep -e "Message" ) && curl -X POST --silent --data-urlencode \
        "payload={\"text\": \"$(echo $line | sed "s/\"/'/g")\"}" "https://hooks.slack.com/services/xxxxxx";
done < <(/path/to/script/run.py 2>&1) >> "$log_file"

It may also prove useful to paste your code into shellcheck.net and have a look at the suggested changes.

CodePudding user response:

Your script shouldn't work at all as you're not executing run.py but you're changing your working directory into it, so unless run.py is a directory, your script should fail.

Also, commands in bash scripting are executed sequentially, so if you launch your python command and then read the log, no wonder that you're not getting log lines in real time.

What i would do is using some pipes and xargs:

#!/bin/bash

/path/to/script/run.py | grep -e "Message" | sed "s/\"/'/g" | xargs -I{} curl -L -X POST --silent --data-urlencode 'payload={"text":"{}"}' https://hooks.slack.com/services/xxx

I've added -L to the curl command because hooks.slack.com makes a redirect to api.slack.com and without that flag curl will stop after the 302 instead of following the Location header in the response.

  • Related