Running a security monitoring script inside an interactive SSH session on your laptop is fine for quick testing—but the moment you close your terminal or put your machine to sleep, your alerts go dark. To run a Python security alert bot Ubuntu systemd setup reliably 24/7, you need an always-on Linux server, an isolated virtual environment, and a properly configured systemd service unit that automatically restarts your script after crashes or kernel reboots.
Recommended Cloud Environment • Ubuntu 24.04 LTS
Need an Always-On Linux VM for Your Python Bot? Python Security Alert Bot
A lightweight 1 vCPU / 1 GB RAM cloud instance is more than enough to run continuous Python monitoring daemons 24/7 without tying up your local workstation.
✔ Deploy Ubuntu 24.04 LTS in 55 seconds • Full root SSH access • Predictable monthly billing
Starts at $4–$6/mo • Instant cloud provisioning

Architecture Overview: Why systemd Beats screen or tmux
Many developers start background Python bots inside screen, tmux, or nohup. While convenient for ad-hoc tasks, those tools lack automatic crash recovery, structured log rotation, and boot-time startup guarantees. By wrapping your bot inside a native systemd service unit on Ubuntu 24.04 LTS, you get:
- Automatic Restart on Failure: If a network timeout or unhandled API exception kills the process,
systemdrespawns it within seconds. - Least-Privilege Isolation: Running the daemon under a dedicated system account (
secbot) prevents a compromised script from modifying system binaries. - Centralized Journal Logging: Standard output and tracebacks stream directly to
journalctlwithout filling your disk with unmanaged log files.
Step 1: Provision Ubuntu 24.04 & Create a Dedicated Service User. Python Security Alert Bot
Start with a clean Ubuntu 24.04 LTS virtual machine (a basic 1 GB RAM instance on DigitalOcean handles lightweight Python monitoring bots effortlessly). Connect via SSH as root, update package indexes, and install Python’s virtual environment toolchain:
apt update && apt upgrade -y
apt install -y python3-venv python3-pip
Next, create a dedicated non-root user without a login shell so the bot runs with minimal privileges:
useradd --system --create-home --home-dir /opt/secbot --shell /usr/sbin/nologin secbot
mkdir -p /opt/secbot/app
chown -R secbot:secbot /opt/secbot
Step 2: Build the Python Virtual Environment
Ubuntu 24.04 enforces PEP 668, blocking global pip install commands to protect OS packages. Create an isolated virtual environment inside /opt/secbot/venv and install requests for webhook alerts:
sudo -u secbot python3 -m venv /opt/secbot/venv
sudo -u secbot /opt/secbot/venv/bin/pip install --upgrade pip requests
Step 3: Write the 24/7 Security Alert Bot Script
To keep this example practical, we will build a lightweight daemon that tails /var/log/auth.log in real time and sends an immediate webhook alert (compatible with Telegram, Discord, or Slack) whenever an unauthorized SSH login or repeated authentication failure occurs.
First, grant the secbot user read access to system logs by adding it to the adm group:
usermod -aG adm secbot
Now create /opt/secbot/app/alert_bot.py:
#!/usr/bin/env python3
import os
import time
import re
import requests
LOG_PATH = "/var/log/auth.log"
WEBHOOK_URL = os.environ.get("ALERT_WEBHOOK_URL", "")
SUSPICIOUS_PATTERNS = re.compile(r"(Accepted publickey|Accepted password|Failed password|Invalid user)")
def send_alert(message: str) -> None:
if not WEBHOOK_URL:
print(f"[DRY-RUN ALERT] {message}", flush=True)
return
try:
resp = requests.post(WEBHOOK_URL, json={"text": message}, timeout=10)
resp.raise_for_status()
except requests.RequestException as exc:
print(f"[ERROR] Failed to dispatch webhook: {exc}", flush=True)
def tail_auth_log(filepath: str) -> None:
with open(filepath, "r", encoding="utf-8", errors="replace") as f:
f.seek(0, os.SEEK_END)
while True:
line = f.readline()
if not line:
time.sleep(0.5)
continue
if SUSPICIOUS_PATTERNS.search(line):
clean_line = line.strip()
send_alert(f"Security Event on {os.uname().nodename}: {clean_line}")
if __name__ == "__main__":
print("Starting 24/7 SSH Security Alert Bot...", flush=True)
tail_auth_log(LOG_PATH)
Set strict ownership and permissions, and store your webhook secret in a root-protected environment file (/opt/secbot/.env):
cat << 'EOF' > /opt/secbot/.env
ALERT_WEBHOOK_URL=https://your-webhook-endpoint.example.com/notify
EOF
chown -R secbot:secbot /opt/secbot
chmod 600 /opt/secbot/.env
chmod 750 /opt/secbot/app/alert_bot.py
Step 4: Configure the systemd Unit File
Create a unit file at /etc/systemd/system/secbot.service. Notice the Restart=always directive and basic filesystem hardening flags:
cat << 'EOF' > /etc/systemd/system/secbot.service
[Unit]
Description=24/7 Python Security Alert Bot
After=network-online.target
Wants=network-online.target
[Service]
Type=simple
User=secbot
Group=secbot
WorkingDirectory=/opt/secbot/app
EnvironmentFile=/opt/secbot/.env
ExecStart=/opt/secbot/venv/bin/python /opt/secbot/app/alert_bot.py
Restart=always
RestartSec=5
# Basic process hardening
NoNewPrivileges=true
ProtectSystem=strict
ProtectHome=true
PrivateTmp=true
[Install]
WantedBy=multi-user.target
EOF
Step 5: Enable, Start & Verify Your Daemon
Reload the systemd manager configuration, enable the service so it starts automatically on reboot, and inspect live logs:
systemctl daemon-reload
systemctl enable --now secbot.service
systemctl status secbot.service
To watch real-time output and confirm alerts fire as expected, stream the journal logs:
journalctl -u secbot.service -f
Once your daemon is running, be sure to lock down the host itself by following our upcoming guide on hardening SSH keys, UFW firewall rules, and Fail2Ban—and protect your local admin workstation with a dedicated endpoint security suite from our Bitdefender Review 2026.
Cloud Infrastructure for Developers
Launch Your Ubuntu 24.04 Server in Under a Minute
Deploy lightweight Linux Droplets with SSD storage, built-in cloud firewalls, and reliable uptime for Python bots, WireGuard gateways, and monitoring scripts.
✔ Simple developer pricing • 99.99% uptime SLA
Get started with Ubuntu 24.04 LTS