Send Alerts from the Command Line to Discord
In the previous post, you set a static IP address on an Ubuntu server. Now that the server connects to the Internet, its a good idea to have it report on its status or specific events. A good way to do this is using Discord, as its a free service for small use cases, and there is a reliable mobile app.
In this post, a linux server is setup to securely make alerts to Discord, which can be picked up using the Discord app, on your mobile device or desktop computer.
0 . Latest updates and prerequisites.
2023.09.15 – First draft, Ubuntu 22.04 Server.
Check back for updates, if you run into trouble. Leave a comment if the post is missing some detail.
You need sudo or root access to modify files in /etc, otherwise you can follow this post as a regular user with a small variation to what is explained here. A basic Discord account is free.
1 . Create a Discord account and server.
Go to discord.com and create a free account, if you have none, yet. Make sure to setup 2-factor-authentication while there; Discord is a popular gaming platform and intrusion attempts happen often.
Once there, you can “create a server”.

Once your server is created, there should be a default text channel called “#general”, a good idea is to create a new text channel just for this project. In my case, I have created a channel “#automatons” for the purpose of automation and alerting. Make sure the channel is set to private.

In the channel settings, under integrations, there is a Webhook section. Create a new Webhook and select “Copy Webhook URL”. This is an api key of sorts that has authority in this channel. Do not share it, save it someplace for the next steps.

Your Webhook will look something like this:
https://discord.com/api/webhooks/1152522872586252368/PaKsnnjvb-qpN7N1rhkuVNsdfgsdgIf you feel so inclined, the channel icon and name fields can be updated to your preference. The only important part for this post is the existence of the channel and the related Webhook.
2 . Check command line functionality.
There are many guides on posting to Discord from the command-line, however, at its most basic, all that is required is a curl POST statement which defines a message in JSON format, to the Webhook URL. A message can be sent with a line, like this, from the shell:
curl -H "Content-Type:application/json" -d '{"username": "server30", "content": "Posted Via Command line"}' https://discord.com/api/webhooks/1152187184116408380/ZOSUZPKjnbx-n5CjzOyVExsuV3MThe message block has to be formatted well and as you can see, the username can be post specific, which means you can post from multiple services and servers some meaningful messages. After some experimenting, its possible to post a message from the command-line.
Now that you know it works, in the next steps some basic monitoring and alerting will be setup.
3 . Create a shell script.
The trouble with automating this curl statement is that is has lots of interesting formatting, so it needs to be taken apart carefully and made into a function that takes parameters for the username and message content. Once that is achieved, the function can be called by system events and cron jobs, for example.
First, the Webhook must be hidden and locked down as it contains enough detail that somebody may post false messages or even get the channel banned. Create a file called “/etc/discord.conf” with an editor. If you are not root, you can place this file in the home of the user under which the crontab will run:
admin@server40:~$ sudo nano /etc/discord.confThe file contains only the webhook and in the future, maybe some other restricted variables:
webhook="https://discord.com/api/webhooks/1152522872586252368/PaKsnnjvb-N7nXvcjy"If a user or service wants to send a message to that Discord channel, they or one of their groups needs at least read access to this file:
sudo chmod 440 /etc/discord.confNow, if you try to cat the file as a regular user, access should be denied. This is to prevent anybody posting to the channel and while this security precaution is not required, it is highly recommended:
user@server40:~$ cat /etc/discord.conf
cat: /etc/discord.conf: Permission deniedNow a simple BASH script can be created that accepts only two parameters; the username and the message:
sudo mkdir -p /opt/discord
sudo nano /opt/discord/discord.shNotice the script calls the conf file for the webhook variable. In BASH, the first parameter is “$1” and the second, “$2”, etc. The backslashes are preserving the formatting of the JSON file, while enclosed in double quotes:
#!/bin/bash
. /etc/discord.conf
curl -H "Content-Type:application/json" -d "{\"username\": \"$1\", \"content\": \"$2\"}" $webhookSet the script to executable:
sudo chmod +x /opt/discord/discord.shTry and run this script with something simple, but make sure you are a user with permissions to the conf file and the script file:
admin@server40:~$ sudo /opt/discord/discord.sh "server03" "HDD03 has error!"And the output on Discord should be similar to this:

Now you can set the Discord app up on your mobile phone or desktop to always be running in the background. It then receives alerts form your infrastructure, servers, switches, any automation you manage without paying for SMS services, worrying about email alerts being lost, etc.
In the next steps, you will setup a simple alert for when the server is restarted and a scheduled check for disk space.
4 . Automate an alert.
Crontab is included in almost every linux distrubution in some form or another. This is a scheduler that runs script regularly or at some event, such as at startup, with the time set to “@reboot”, for example. Crontab has no intellegence around dependency so a network down alert will simply not get processed and some more complicated scripting is required. However, to send an alert at startup, with a short delay to wait for network services to come up is possible. Start the crontab editor:
sudo crontab -eAdd a line this like this, which introduces a 20 second pause before sending an alert:
@reboot sleep 20 && /opt/discord/discord.sh "server40" "Starting up..."Once you reboot your server plus about 20 seconds, Discord will get the message.
Another useful feature of this kind of automation is to report on progress of tasks, availability of resources, etc. Here is a simple way to report disk space, everyday at 0900 from crontab:
0 9 * * * /opt/discord/discord.sh "server32" "Disk space on / is $(df -kh / | tail -n1 | awk '{print $4}')"And every day at 0900, Discord will post the disk status.
And since the discord.sh script runs with just those two parameters, a more complicated solution is possible, scheduled or not, the discord.sh can be called from anywhere, too, such as Zabbix, PRTG or other third party applications that allow a script to be run as part of any other action or activity.
5 . Supporting this blog.
This type of content takes a lot of effort to write. Each post is drafted, researched, then tested multiple times, even a simple step or detail might take more than a few hours to go from the idea to a published blog post.
If you feel I have saved you some time, you can support me by;
- hosting with DigitalOcean, like I do – DigitalOcean.
- buying me a beer through PayPal – PayPal.
© HorseFreeGlue, 2025. Unauthorized use and/or duplication of this material without express and written permission from this site’s author and/or owner is strictly prohibited