When was the last time you had to execute a long-running script and wished you could monitor the progress remotely without setting up a plethora of systems? For us, it was yesterday. And doing that was so simple, we had to share it.

At Postman, we use Slack for a lot of stuff, having it report the progress of an arbitrary script was simply natural. The following piece of code tails a file and for every new line added to the file, streams it to a slack incoming webhook using curl.


tail -n0 -F "$1" | while read LINE; do
  (echo "$LINE" | grep -e "$3") && curl -X POST --silent --data-urlencode \
    "payload={\"text\": \"$(echo $LINE | sed "s/\"/'/g")\"}" "$2";

To use this script, save it as an executable script and simply pass the path to the log file and a webhook url to this script.

./tail-slack.sh "file.log" "https://hooks.slack.com/services/...";

If you want this script to keep running even after you have logged out of your SSH terminal, use nohup command.

You can even make this script send the line to slack only when a particular keyword is found by sending the keyword as a third parameter.

./tail-slack.sh "/var/log/ngix/access.log" \
  "https://hooks.slack.com/services/..." " 404 ";

The above example tails an nginx access log and streams the line to slack when it has a 404 response. How simple and cool is that!