Admin

Cho đi là còn mãi
Thành viên BQT
72 ❤︎ Bài viết: 1056 Tìm chủ đề
Đọc: 5,243 Tìm: 0
Để chạy một file Python trên hệ điều hành CentOS, bạn có thể làm theo các bước dưới đây:

Kiểm tra Python đã được cài đặt chưa

Mã:
python3 --version
python 3.13 --version

Nếu chưa có, bạn cài bằng lệnh:

Mã:
sudo yum install python3 -y

Chạy file Python

Giả sử bạn có file tên bot.py, bạn chạy bằng lệnh:

Mã:
python3 bot.py

Cài đặt các thư viện cần thiết

Nếu script dùng các thư viện ngoài như requests, telethon, ccxt, v.v, bạn cần cài:

Mã:
pip3 install -r requirements.txt

Hoặc từng thư viện:

Mã:
pip3 install requests

Tuỳ chọn Chạy nền (background)

Nếu bạn muốn script chạy nền, không bị tắt khi đóng terminal:

Cách 1: Dùng nohup

Mã:
nohup python3 bot.py > output.log 2>&1 &

Cách 2: Dùng screen

Mã:
screen -S mybot
python3 bot.py
# để rời screen: nhấn Ctrl+A rồi D

Để quay lại:

Mã:
screen -r mybot

Nếu bạn muốn tự động chạy sau khi khởi động lại máy (reboot):

Mình sẽ hướng dẫn bạn tạo 1 service dùng systemd, ví dụ:

Bước 1: Tạo file service

Mã:
sudo nano /etc/systemd/system/mybot.service

Dán vào:

Mã:
[Unit]
Description=My Crypto Trading Bot
After=network.target

[Service]
ExecStart=/usr/local/bin/python3.13 /bot/name/mxc.py
WorkingDirectory=/bot/name
Restart=always
User=root

[Install]
WantedBy=multi-user.target

Nhớ thay /bot/name/mxc.py và root bằng đúng với máy bạn nhé.

Lưu ý lấy đúng đường dẫn python đúng version, lệnh lấy đường dẫn python:

Mã:
which python
hoặc
which python3
hoặc
which python3.13

Bước 2: Enable và start service

Mã:
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl enable bnb.service
sudo systemctl start bnb.service
systemctl status bnb.service

sudo systemctl stop bnb.service
sudo systemctl restart bnb.service

Sàn MXC:

Mã:
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl enable mxc.service
sudo systemctl start mxc.service
systemctl status mxc.service

Sàn Binance:

Mã:
sudo systemctl daemon-reexec
sudo systemctl daemon-reload
sudo systemctl enable bnb.service
sudo systemctl start bnb.service
systemctl status bnb.service


Bước 3: Kiểm tra bot có chạy chưa

Mã:
systemctl status mybot.service

Lệnh tạm dừng và khởi động lại bot:

Mã:
sudo systemctl stop mybot.service
sudo systemctl daemon-reload
sudo systemctl restart mybot.service
systemctl status mybot.service

Sàn MXC:

Mã:
sudo systemctl stop mxc.service
sudo systemctl daemon-reload
sudo systemctl restart mxc.service
systemctl status mxc.service

Khởi chạy bot lần đầu:

Mã:
/usr/local/bin/python3.13 /bot/name/mxc.py

Nếu chạy status thấy Failed có thể chạy lại lệnh restart thêm 1 lần nữa:

Mã:
sudo systemctl restart mxc.service
systemctl status mxc.service
 
Chỉnh sửa cuối:
72 ❤︎ Bài viết: 1056 Tìm chủ đề
Lỗi:

Mã:
/usr/local/bin/python3.13 /bot/bnb/bot.py Traceback (most recent call last): File "/bot/bnb/bot.py", line 9, in <module> from binance.um_futures import UMFutures ModuleNotFoundError: No module named 'binance'

Cách xử lý:

Cách fix nhanh
Chạy đúng pip của Python 3.13 (để cài vào đúng interpreter bạn đang chạy):


/usr/local/bin/python3.13 -m pip install --upgrade pip
/usr/local/bin/python3.13 -m pip install binance-connector python-dotenv requests

Kiểm tra import & khởi tạo client

from binance.um_futures import UMFutures

Mã:
client = UMFutures(key=api_key, secret=api_secret)  # nên truyền theo tên tham số

Nếu vẫn lỗi, kiểm tra thêm
  • Tên file/ thư mục trùng: đảm bảo trong dự án không có binance.py hoặc thư mục binance/ của bạn (nó sẽ che mất package).

  • Đúng môi trường: bạn cài vào venv A nhưng chạy bằng python B sẽ không thấy package. In thử:

    Mã:
    /usr/local/bin/python3.13 -m pip -V
    /usr/local/bin/python3.13 -c "import sys; print(sys.executable)"

    Để chắc đang dùng đúng interpreter.
  • Docker/requirements: Thêm vào requirements. Txt:

    Binance-connector

    Python-dotenv

    Requests
  • Nếu gặp lỗi build/compat với 3.13 (hiếm), dùng Python 3.11/3.10 là an toàn hơn.

Làm xong các bước trên là import được binance. Um_futures.
 

Những người đang xem chủ đề này

Xu hướng nội dung

Back