This guide provides step-by-step instructions for running TradeX as a background service using either Supervisor (supervisord) or Systemd. These tools ensure that your application runs reliably and restarts automatically if it crashes.
Supervisord is a process control system that allows you to manage multiple processes (like TradeX) as background services. Below are the steps to set up TradeX using supervisord.
-
Install
supervisoron your system:sudo apt update sudo apt install supervisor
-
Ensure your project directory contains the provided
supervisord.conffile. If not, copy it into your project directory.
If you're running TradeX locally (not in Docker), copy the supervisord.conf file to the appropriate directory:
sudo cp supervisord.conf /etc/supervisor/conf.d/tradex.confFor Docker-based setups, the supervisord.conf file is already integrated into the Dockerfile, so no additional setup is required.
After copying the configuration file, reload supervisor to recognize the new configuration:
sudo supervisorctl reread
sudo supervisorctl updateStart all services defined in the supervisord.conf file:
sudo supervisorctl start tradex:*Verify that all services are running without errors:
sudo supervisorctl statusYou should see output similar to:
tradex:dashboard_app RUNNING pid 1234, uptime 0:05:23
tradex:webhook_app RUNNING pid 1235, uptime 0:05:23
tradex:email_reader RUNNING pid 1236, uptime 0:05:23
To debug issues or monitor logs, check the log files specified in the supervisord.conf file:
tail -f /var/log/supervisor/dashboard_app.err.log
tail -f /var/log/supervisor/webhook_app.err.log
tail -f /var/log/supervisor/email_reader.err.logTo restart or stop the services:
sudo supervisorctl restart tradex:*
sudo supervisorctl stop tradex:*Systemd is a system and service manager for Linux operating systems. It allows you to manage services (like TradeX) as background processes that start automatically on boot. Below is a comprehensive guide to setting up TradeX as a systemd service.
- Ensure your Python environment (including dependencies) is set up and working.
- If using a virtual environment, ensure its path is known.
- Ensure you have
sudoprivileges to create and managesystemdservices.
All systemd service files are stored in /etc/systemd/system/. Navigate to this directory:
cd /etc/systemd/system/The dashboard app (dashboard_app.py) serves the web interface. Create a service file for it:
sudo nano dashboard_app.serviceAdd the following content:
[Unit]
Description=Gunicorn instance to serve TradeX Dashboard
After=network.target
[Service]
User=your_user # Replace with your username (e.g., "ubuntu" or "root")
Group=www-data # Replace with your group (optional)
WorkingDirectory=/path/to/tradex # Replace with the absolute path to your project directory
ExecStart=/path/to/venv/bin/gunicorn -w 4 -b 0.0.0.0:5000 dashboard_app:app
Restart=always
Environment="PATH=/path/to/venv/bin" # Replace with the path to your virtual environment's bin folder
EnvironmentFile=/path/to/tradex/.env # Optional: Load environment variables from .env
[Install]
WantedBy=multi-user.targetThe webhook app (webhook_receiver.py) listens for incoming trade signals. Create a service file for it:
sudo nano webhook_app.serviceAdd the following content:
[Unit]
Description=Gunicorn instance to serve TradeX Webhook
After=network.target
[Service]
User=your_user # Replace with your username
Group=www-data # Replace with your group (optional)
WorkingDirectory=/path/to/tradex # Replace with the absolute path to your project directory
ExecStart=/path/to/venv/bin/gunicorn -w 2 -b 0.0.0.0:5005 webhook_receiver:app
Restart=always
Environment="PATH=/path/to/venv/bin" # Replace with the path to your virtual environment's bin folder
EnvironmentFile=/path/to/tradex/.env # Optional: Load environment variables from .env
[Install]
WantedBy=multi-user.targetReload systemd to recognize the new services:
sudo systemctl daemon-reloadStart both the dashboard and webhook services:
sudo systemctl start dashboard_app
sudo systemctl start webhook_appTo ensure the services start automatically when the system boots:
sudo systemctl enable dashboard_app
sudo systemctl enable webhook_appVerify that both services are running without errors:
sudo systemctl status dashboard_app
sudo systemctl status webhook_appIf everything is working correctly, you should see output similar to:
● dashboard_app.service - Gunicorn instance to serve TradeX Dashboard
Loaded: loaded (/etc/systemd/system/dashboard_app.service; enabled; vendor preset: enabled)
Active: active (running) since ...
To debug issues or monitor logs, use journalctl:
sudo journalctl -u dashboard_app -f
sudo journalctl -u webhook_app -fTo restart or stop the services:
sudo systemctl restart dashboard_app
sudo systemctl stop dashboard_app
sudo systemctl restart webhook_app
sudo systemctl stop webhook_app- Check Logs: Use
journalctl(forsystemd) ortail -f(forsupervisord) to view logs and identify errors. - Permissions: Ensure the user running the service has read/write access to the project directory and logs.
- Ports: Verify that ports
5000(dashboard) and5005(webhook) are not already in use by another process. - Dependencies: Ensure all Python dependencies are installed in your virtual environment.
-
Service Fails to Start:
- Check the logs for detailed error messages.
- Ensure all paths in the
.serviceorsupervisord.conffiles are correct. - Verify that the
.envfile exists and contains valid configuration.
-
Port Conflicts:
- If ports
5000or5005are already in use, update theExecStartcommands in the.servicefiles or thecommandfields in thesupervisord.conffile to use different ports.
- If ports
-
Gunicorn Not Found:
- Ensure Gunicorn is installed in your virtual environment. Run:
pip install gunicorn
- Ensure Gunicorn is installed in your virtual environment. Run: