Skip to content
Node.js and Python3 min read

Bot stops after you close the terminal: keep it running 24/7

Bot stops when the SSH session closes

Short answer

A bot stops when you close the terminal because a process started in an SSH session ends with that session. `nohup` or `screen` keep it alive after you log out, but don't restart it when it crashes or the server reboots. The dependable fix on cPanel hosting is a cron job every 5 minutes that starts the bot through `flock`, so it starts the bot if it's down and never launches a second copy, or turning the bot into a webhook inside a Passenger app.

ArabyHost teamLast updated: ShareWhatsAppXLinkedIn
Contents
  1. The most likely causes, in order
  2. How to confirm it’s fixed
  3. Prevention
  4. On ArabyHost

You started the bot over SSH with python bot.py or node bot.js, it answered messages, and then you closed the terminal or your connection dropped and the bot stopped instantly. Or you started it with nohup, it ran for hours, and then it stopped and never came back. That’s expected: the process belongs to the session, and nothing restarts it.

The most likely causes, in order

1. The bot runs in the foreground of an SSH session

When the session closes, the system sends SIGHUP to the processes started in it, and they exit.

Check: after logging back in, run pgrep -af bot. No output means the bot is down.

2. Nothing restarts it after a crash or a server restart

nohup, screen and tmux keep the process after you log out, but the bot can still stop from an unhandled error, a network drop, the account’s memory limit, or a server restart for maintenance.

Fix for both: a cron job with flock. In cPanel open Cron Jobs, choose Once Per Five Minutes, and enter:

# Python (the environment Setup Python App created)
cd /home/USER/bot && /usr/bin/flock -n /home/USER/bot/bot.lock /home/USER/virtualenv/bot/3.12/bin/python bot.py >> /home/USER/bot/bot.log 2>&1

# Node.js
cd /home/USER/bot && /usr/bin/flock -n /home/USER/bot/bot.lock /home/USER/nodevenv/bot/24/bin/node bot.js >> /home/USER/bot/bot.log 2>&1

flock -n holds the lock file for as long as the bot runs. If the bot is running, the job exits at once; if it stopped, the job starts it again within 5 minutes. The cron jobs guide explains the timing, and the cron generator writes the line for you.

3. Two copies run at once

What you see: in Telegram, 409 Conflict: terminated by other getUpdates request (explained).

Fix: stop every copy and let cron start a single one:

pkill -u "$USER" -f bot.py

Stop the copy on your own computer too.

4. The job runs but the bot doesn’t start from it

Cron doesn’t read .bashrc, doesn’t know your variables, and starts in your home folder.

Fix: use full paths as above, load the token inside the code from .env (with python-dotenv or dotenv), and read bot.log. If the job never runs at all, see cron job not running.

5. Alternative: a webhook instead of a permanent process

A Telegram bot can receive updates by webhook instead of constant polling. The bot then becomes a web app in Setup Python App or Setup Node.js App that Passenger starts when a message arrives, with no process running all the time. Call setWebhook with an https URL on your domain. A Discord bot needs a permanent connection, so use the cron method for it.

How to confirm it’s fixed

Close SSH, wait, then message the bot. For a full test, stop the bot yourself with pkill, check it’s back within 5 minutes, and that pgrep -af bot shows exactly one copy.

Prevention

  • Handle errors inside the bot and log them to bot.log instead of exiting.
  • Empty the log regularly so it doesn’t grow forever.
  • One token, one running copy per bot.

On ArabyHost

On ArabyHost AI/Developer plans, bots run on Python 3.9 to 3.12 and Node.js 20 and 24, with SSH jailed in CageFS; background processes are kept alive with cron as above, and webhook bots run as Passenger apps with free SSL. The bot shares your account’s resource limits, and there’s no root and no Docker. Full walkthroughs are in the Telegram bot guide and the Discord bot guide, and AI/Developer hosting lists what the plans include.

FAQ

Isn't nohup or screen enough to keep the bot running?

They keep it alive after you log out, but if the bot crashes or the server restarts, nothing brings it back. Always add a cron job that restarts it.

Can I use PM2?

PM2 restarts the bot when it crashes, but PM2 itself stops when the server restarts, and setting it to start at boot needs root. On shared hosting, tie it to a cron job, or use flock directly as shown here.

Why do I get 409 Conflict after starting the bot on the server?

A second copy is running with the same token, often on your laptop or an old copy on the server. Stop the extras with `pkill -f bot.py` and use flock so two copies never start.

Can I host a Telegram bot free 24/7?

Free services exist, but they often stop processes after idle time or cap running hours. A bot that must run all the time needs somewhere that keeps it alive and restarts it, which is what a cron job does on hosting that allows background processes.

Useful? Share it with someone who needs it:WhatsAppXLinkedIn

Read next

Ready to put your idea online?

Fast, isolated hosting, support that understands you, and an AI assistant that fixes problems with you.