If you manage a Linux server, you rely on cron jobs and bash scripts to handle automated tasks like database backups, system updates, or heavy video rendering. When these long-running scripts finish—or worse, when they crash—you need to know immediately. Checking log files manually is inefficient; a true server administrator wants a push notification directly to their smartphone.
While you could configure a complex SMTP server to send email alerts, emails are often delayed or trapped in spam filters. A much faster, more reliable, and completely free alternative is to use the Telegram Messenger API. With a few simple steps, you can configure your Linux terminal to send instantaneous push notifications to your phone using a basic curl command.
Step 1: Create a Telegram Bot
To send messages programmatically, you need a dedicated bot account.
- Open the Telegram app on your phone or desktop.
- Search for @BotFather (the official Telegram bot manager) and start a chat.
- Type the command
/newbotand follow the prompts to give your bot a name and a unique username. - When finished, BotFather will give you an HTTP API Token (a long string of numbers and letters, e.g.,
123456789:ABCdefGHIjklMNOpqrSTUvwxYZ). Copy this token to a safe place; it is the password to your bot.
Step 2: Get Your Personal Chat ID
Your bot needs to know exactly who to send the message to. You need your unique Telegram Chat ID.
- In the Telegram search bar, look for your newly created bot (using the username you just made) and send it a simple message like “Hello”.
- Now, open a web browser and paste the following URL, replacing
<YOUR_BOT_TOKEN>with the token from Step 1:https://api.telegram.org/bot<YOUR_BOT_TOKEN>/getUpdates - Press Enter. You will see a raw JSON output. Look for the section labeled
"chat":{"id":followed by a 9 or 10-digit number. Copy that number; this is your Chat ID.
Step 3: Send a Message Using curl
Now that you have your Bot Token and your Chat ID, you can send a message directly from your Linux terminal using the curl command (which comes pre-installed on almost all distributions).
- Open your Linux terminal.
- Type the following command, replacing the placeholders with your actual Token and Chat ID:
curl -s -X POST https://api.telegram.org/bot<YOUR_BOT_TOKEN>/sendMessage -d chat_id=<YOUR_CHAT_ID> -d text="Backup completed successfully!" - Press Enter.
Within half a second, your phone will buzz with a new Telegram message from your bot stating “Backup completed successfully!”
Step 4: Integrate into a Bash Script
To make this truly useful, you can embed this one-liner at the very end of any automated script. For example, if you have a nightly backup script (backup.sh), simply append the curl command to the bottom.
#!/bin/bash
# ... (Your complex backup commands here) ...
# Send a notification when finished
curl -s -X POST https://api.telegram.org/bot123456789:ABCdefGHIjklMNOpqrSTUvwxYZ/sendMessage -d chat_id=987654321 -d text="Server 01: Nightly MySQL backup is complete." > /dev/null
(Note: Adding > /dev/null at the end prevents the raw JSON response from cluttering your terminal output when the script runs).