tocOn this page 6 sections
You need two checks. A PowerShell script on the VPS can send a message through the Telegram Bot API when terminal64.exe stops or the server restarts. A second machine has to watch the VPS from outside, because a server that is down can't report its own outage. Set up both, and add a small EA if you also want to know when MT5 is running but has lost its broker connection.
Step 1: Create the bot and find your chat ID
- In Telegram, open @BotFather, send
/newbotand follow the prompts. Copy the token it gives you. - Send any message to your new bot. A bot can't start a chat with you, so you have to message it first.
- On the VPS, open
https://api.telegram.org/bot<TOKEN>/getUpdatesin a browser and find"chat":{"id":...}. That number is your chat ID.
Store the token in C:\Monitor\token.txt. Then remove inherited permissions from that folder and grant access only to SYSTEM and Administrators. Anyone who has the token can send messages as your bot.
Step 2: A PowerShell watchdog for MT5
Save this as C:\Monitor\mt5-watch.ps1. It matches the terminal by its full path, so on a VPS with several terminals you can run one copy per install. It writes the last known state to a file and only sends a message when the state changes, so you don't get the same alert every minute.
$ErrorActionPreference = 'Stop'
$Token = (Get-Content 'C:\Monitor\token.txt').Trim()
$ChatId = '123456789'
$Exe = 'C:\Program Files\MetaTrader 5\terminal64.exe'
$State = 'C:\Monitor\mt5.state'
function Send-Tg([string]$Text) {
[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12
Invoke-RestMethod -Method Post -Uri "https://api.telegram.org/bot$Token/sendMessage" `
-Body @{ chat_id = $ChatId; text = $Text } | Out-Null
}
$up = [bool](Get-Process terminal64 -ErrorAction SilentlyContinue |
Where-Object Path -eq $Exe)
$now = if ($up) { 'up' } else { 'down' }
$was = if (Test-Path $State) { Get-Content $State } else { 'up' }
if ($now -ne $was) {
Send-Tg "${env:COMPUTERNAME}: MT5 is $now at $(Get-Date -Format 'yyyy-MM-dd HH:mm')"
Set-Content $State $now
}
The state file is written only after the message sends. If Telegram can't be reached, the script tries again on the next run instead of losing the alert.
Register it as a SYSTEM task that runs every minute. Run this in an elevated PowerShell window:
$a = New-ScheduledTaskAction -Execute 'powershell.exe' `
-Argument '-NoProfile -ExecutionPolicy Bypass -File C:\Monitor\mt5-watch.ps1'
$t = New-ScheduledTaskTrigger -Once -At (Get-Date) `
-RepetitionInterval (New-TimeSpan -Minutes 1) `
-RepetitionDuration (New-TimeSpan -Days 3650)
Register-ScheduledTask -TaskName 'MT5 Watch' -Action $a -Trigger $t `
-User 'SYSTEM' -RunLevel Highest
Running the task as SYSTEM means it keeps working after you close or disconnect your RDP session. Test it by closing MT5. You should get a "down" message within a minute, and an "up" message after you start MT5 again.
Step 3: An alert when the VPS restarts
Windows Update restarts are the most common reason MT5 stops without warning. Create a second task with New-ScheduledTaskTrigger -AtStartup that runs a short script. The script should wait about 60 seconds for the network to come up (Start-Sleep 60) and then call Send-Tg "VPS restarted". If you get that message and no "MT5 is up" message soon after, the terminal didn't start again. To make it start automatically, see auto-restarting MT4 and MT5 after update reboots.
Step 4: Alert on a lost broker connection from inside MT5
MT5 can keep running while it is disconnected from the trade server, and the process check above won't notice. A small EA can check TerminalInfoInteger(TERMINAL_CONNECTED) on a timer and call Telegram when the result changes:
input string Token = "";
input string ChatId = "";
bool lastConn = true;
int OnInit() { EventSetTimer(30); return INIT_SUCCEEDED; }
void OnTimer() {
bool conn = (bool)TerminalInfoInteger(TERMINAL_CONNECTED);
if (conn == lastConn) return;
lastConn = conn;
string url = "https://api.telegram.org/bot" + Token + "/sendMessage?chat_id="
+ ChatId + "&text=MT5%20" + (conn ? "reconnected" : "disconnected");
char data[], res[]; string hdr;
WebRequest("GET", url, "", 5000, data, res, hdr);
}
Add https://api.telegram.org under Tools > Options > Expert Advisors > Allow WebRequest for listed URL. If you skip this, the call fails with error 4014. Our WebRequest 4014 guide covers that fix. Attach the EA to one chart only, so you don't get one alert per chart.
Step 5: Watch the VPS from outside
Everything above stops if the VPS crashes, hangs or loses its network. For those cases, run a check from a different server, such as a small Linux VPS. This script tests TCP 3389, or whatever port you moved RDP to. It alerts only after three failures in a row, so a single dropped packet doesn't wake you at night:
#!/bin/bash
HOST=203.0.113.10; PORT=3389
TOKEN=$(cat /root/.tg_token); CHAT=123456789
F=/var/tmp/vps.fails; n=$(cat $F 2>/dev/null || echo 0)
if nc -z -w 5 $HOST $PORT; then
[ "$n" -ge 3 ] && MSG="VPS $HOST is back"
echo 0 > $F
else
n=$((n+1)); echo $n > $F
[ "$n" -eq 3 ] && MSG="VPS $HOST unreachable for 3 checks"
fi
[ -n "$MSG" ] && curl -s "https://api.telegram.org/bot$TOKEN/sendMessage" \
--data-urlencode "chat_id=$CHAT" --data-urlencode "text=$MSG" > /dev/null
Run it every minute from cron: * * * * * /usr/local/bin/vps-check.sh. If you limit RDP to certain IP addresses in Windows Defender Firewall, add the watcher's IP to the inbound rule. If you don't have a second server, a hosted uptime monitor that supports Telegram notifications can do this job.
For more on running trading terminals around the clock, see our Windows VPS for Forex Trading hub.
FAQ
Can I use the bot from my signal copier for these alerts?
Yes, if you only send messages to it. If another program on the VPS is already polling that bot with getUpdates, run your chat ID lookup from Step 1 on a separate bot. Two programs reading updates from the same bot will interfere with each other.
Why not just use MT5's built-in push notifications?
MT5's push notifications go to the MetaTrader mobile app through your MetaQuotes ID, not to Telegram. They also come from the terminal itself, so they stop when MT5 or the VPS stops, which is when you most need an alert.
How do I watch several terminals?
Make one copy of the script for each terminal, each with its own $Exe path and state file. Or change the script to loop over a list of paths and keep one state file for each path.
Run this on your own Windows VPS
Get a Windows VPS from WindowsVPS in about 60 seconds, with full Administrator access over RDP, NVMe storage and the Windows license included.
Written by the WindowsVPS Team
We run Windows Server VPS hosting and write these guides from the same setups our customers use: RDP, PowerShell, trading terminals and always-on bots. Questions about a guide? Contact us.