DEV Community

Jeff Shomali
Jeff Shomali

Posted on

3 1

Using Slack Webhook

What do you learn in this post?

  • You will learn how to create a Shell script.
  • You will learn how to backup up your database by a script.
  • You will learn how to automate your script by specif time of day.
  • Also, you will learn how to send a message to your Slack channel programmatically.

Overview

Just imagine that you have a Slack channel, and you wanted to get notified when something happened in your Linux/Unix machine. For instance, getting back up of something, or checking disk usage, monitor the network/process, or whatever you want to do in that machine manually. In this case, I assume you want to get a database backup and send a process success or failure message to your one of your Slack channels. Before to start this, you need to have a Slack account. The goal of this post is to take a backup every day and send a message to your Slack channel. Before to start this you need to have Slack

Create Script

  • Create your script.sh file in your machine.
  • Make your script executable by chmod u+x script.sh
  • Add your logic to it. For this case to get backup of MySQL database with mysqldump.

#!/bin/sh

date=`date +%Y%m%d`

mysqldump -h localhost -u root -p pass --single-transaction --quick databasename > jeff/backup/databse${date}.sql

if [ "$?" -eq 0 ]
then
    echo " Script is running - ${date}" >> jeff/dbbackup.log
     curl -X POST -H 'Content-type: application/json' --data '{"channel":"#<your channel name>","text":"<your message here>"}' https://<your-slack-chanel>.slack.com/services/hooks/<your hooks key>
else
    echo "Script failed to run on ${date}" >> jeff/dbbackup.log
    curl -X POST -H 'Content-type: application/json' --data '{"channel":"#<your channel name>","text":"<your message here> "}' https://<your-slack-chanel>.slack.com/services/hooks/<your hookks key>
fi

Enter fullscreen mode Exit fullscreen mode

You need to modify the <your message here>, <your channel name>, <your hooks key with your custom message and Slack webhooks' key. Where to get the key? follow next step

Create Slack Webhook

Add script to Cron jobs.

If you want to schedules a command or script on your server to run automatically at a specified time and date, please read the end of my last post.

Good luck 😉

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • 0:56 --last-failed
  • 2:34 --only-changed
  • 4:27 --repeat-each
  • 5:15 --forbid-only
  • 5:51 --ui --headed --workers 1

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Click on any timestamp above to jump directly to that section in the tutorial!

Watch Full Video 📹️

Top comments (0)

Playwright CLI Flags Tutorial

5 Playwright CLI Flags That Will Transform Your Testing Workflow

  • 0:56 --last-failed: Zero in on just the tests that failed in your previous run
  • 2:34 --only-changed: Test only the spec files you've modified in git
  • 4:27 --repeat-each: Run tests multiple times to catch flaky behavior before it reaches production
  • 5:15 --forbid-only: Prevent accidental test.only commits from breaking your CI pipeline
  • 5:51 --ui --headed --workers 1: Debug visually with browser windows and sequential test execution

Learn how these powerful command-line options can save you time, strengthen your test suite, and streamline your Playwright testing experience. Click on any timestamp above to jump directly to that section in the tutorial!

Watch Full Video 📹️

👋 Kindness is contagious

Explore a trove of insights in this engaging article, celebrated within our welcoming DEV Community. Developers from every background are invited to join and enhance our shared wisdom.

A genuine "thank you" can truly uplift someone’s day. Feel free to express your gratitude in the comments below!

On DEV, our collective exchange of knowledge lightens the road ahead and strengthens our community bonds. Found something valuable here? A small thank you to the author can make a big difference.

Okay