Skip to content

Keeping Programs Running

A program you start in SSH stops when you close the connection. To keep a bot or app running, use screen for a quick session you can come back to, or systemd to start it at boot and restart it if it crashes.

The examples run a Python bot in /root/my-bot with python3 bot.py. Replace the folder and command with your own, for example node index.js.

Install screen and start a named session:

Terminal window
apt install screen # Debian and Ubuntu
dnf install screen # Rocky Linux and AlmaLinux
screen -S my-bot

Start your program inside the session:

Terminal window
cd /root/my-bot
python3 bot.py

Press Ctrl+A, then D, to leave the session running in the background. You can close SSH.

To Run
List sessions screen -ls
Go back to a session screen -r my-bot
Stop the program Go back to the session and press Ctrl+C

Create a service file:

Terminal window
nano /etc/systemd/system/my-bot.service

Paste this and change WorkingDirectory and ExecStart to match your program:

[Unit]
Description=My Discord bot
After=network-online.target
Wants=network-online.target
[Service]
WorkingDirectory=/root/my-bot
ExecStart=/usr/bin/python3 bot.py
Restart=on-failure
User=root
[Install]
WantedBy=multi-user.target

Save with Ctrl+X, then Y and Enter. Start the service and enable it at boot:

Terminal window
systemctl daemon-reload
systemctl enable --now my-bot
To Run
Check if it runs systemctl status my-bot
Follow its output journalctl -u my-bot -f
Restart it after a code change systemctl restart my-bot
Stop it systemctl stop my-bot

ExecStart needs the full path to the program. Find it with which python3 or which node. If your bot uses a Python virtual environment, point ExecStart at the environment’s Python, for example /root/my-bot/venv/bin/python bot.py.