Phones and mobile apps2 minutes to read

Can I run a Discord bot 24/7 on my Android phone with Termux?

Termux can run a bot while the phone is awake and charging, but sleep, battery limits and changing networks take it offline. Here is what holds up 24/7.

It runs, but it won’t stay online reliably

You can run a discord.js or discord.py bot in Termux on Android, and it works for testing. It won’t stay online reliably. Android limits background apps to save battery, the phone may switch networks, and the bot stops whenever the phone restarts, overheats or runs flat. An iPhone can’t keep a background process running at all. For 24/7 uptime, run the bot on a host and use your phone to control it.

What takes a phone-hosted bot offline

Common interruptions on a phone
What happensEffect on the botCan you work around it?
Screen off, battery optimizationAndroid pauses or kills TermuxPartly: termux-wake-lock and a battery exemption, at the cost of battery
Wi-Fi to mobile dataThe connection to Discord dropsThe library reconnects, after a gap
Reboot, update or flat batteryThe process is goneOnly by restarting it by hand
Heat while charging all dayThe system throttles or closes appsNo, and it wears the battery
Webhook bots (Telegram)Carrier networks rarely accept incoming connectionsUse polling instead

Keep the phone, move the process

Put the same code on a host and keep the phone as the remote control. On Bothosting, the free Spark plan runs one bot with 256 MB of memory and 25% CPU. It doesn’t sleep when idle. Turn on Restart on crash and it comes back after an error. The iOS app or the Android app alerts your phone when it stops unexpectedly, and lets you restart it, read live logs, edit files and change variables. If you already have the bot working in Termux, push the project to GitHub and connect that repository to the deployment. With the push webhook, each later commit redeploys it. See hosting a Discord bot from your phone.

When a phone is good enough

A phone is fine for trying a bot for an evening, testing a command or learning the library. Use a test application with its own token so a crash on the phone doesn’t affect your real bot. Before moving to a host, stop the copy on the phone. Two processes logged in with the same token both reply to each command. Testing a Discord bot before deploying.