Creating and Activating Systemd Services for Running Bots
1 мин. чтения
For ensuring stable and automatic operation of your DEXBOT bots on the server, it is recommended to use systemd system services.
This allows running bots as services that automatically start on system boot, are monitored, and restart if they crash.
⚙️ Creating a systemd service unit file for DEXBOT #
To create the service, run the following command in the terminal:
sudo nano /lib/systemd/system/DEXBOT.service
In the opened editor, paste the following code:
[Unit]
Description=DEXBOT — CRYPTO BOT
After=network.target
[Service]
Type=simple
WorkingDirectory=/MYBOTS/DEXBOT
ExecStart=/MYBOTS/DEXBOT/DEXBOT.bin
Restart=always
RestartSec=60
[Install]
WantedBy=multi-user.target
Description— a brief description of the service.After=network.target— start the service after network services are initialized.WorkingDirectory— the working directory from which the bot is launched.ExecStart— command to start the bot executable.Restart=always— always restart the service if it stops.RestartSec=60— wait 60 seconds before attempting to restart.WantedBy=multi-user.target— run the service in multi-user mode.
🔧 Make the bot executable files executable #
Run the commands to give execute permissions:
chmod +x /MYBOTS/DEXBOT/DEXBOT.bin
This is necessary so that systemd can run your bots.
🚀 Enable services to start automatically on system boot #
Run the commands:
sudo systemctl enable DEXBOT
These commands create links in system directories, ensuring services start automatically after reboot.
▶️ Start services manually #
To start the services immediately without rebooting the server, run:
sudo systemctl start DEXBOT
If everything is done correctly, your bots will run as background processes.
📊 Additional useful service management commands #
- Check service status:
sudo systemctl status DEXBOT - Restart service:
sudo systemctl restart DEXBOT - Stop service:
sudo systemctl stop DEXBOT - Disable autostart:
sudo systemctl disable DEXBOT
⚠️ Recommendations and tips #
- Before creating a service, make sure the bots run correctly when started manually.
- Ensure the unit files contain correct paths to the executable files and working directories.
- Configure logs for services to see errors and messages — systemd stores them in the
journalctllog. - To view service logs, use:
sudo journalctl -u DEXBOT -f— for real-time log monitoring. - After editing unit files, run
sudo systemctl daemon-reloadso systemd reloads the new settings.
Now your bots run as full-fledged system services with automatic startup and control, greatly simplifying management and increasing reliability.
❓ Why use systemd services? #
- Automatic startup: bots start automatically when the server is powered on or rebooted.
- Reliability: in case of errors or crashes, the bot will be automatically restarted after the set time.
- Convenient management: starting, stopping, restarting, and checking status is done with standard
systemctlcommands. - Centralized control: all services can be managed and monitored via system tools.