1
Fork 0
crypto_bot_training/Session_06/Notebooks/method_screen_tmux.ipynb
2025-06-28 16:08:28 +02:00

636 lines
No EOL
16 KiB
Text

{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"# Running Your Trading Bot 24/7 with Screen/Tmux\n",
"\n",
"## Option 2: Screen & Tmux - Interactive Terminal Sessions\n",
"\n",
"**Best for:** Interactive monitoring, debugging, development\n",
"\n",
"**What Screen/Tmux do:**\n",
"- Create persistent terminal sessions that survive SSH disconnections\n",
"- Allow you to detach/reattach to running sessions\n",
"- Perfect for watching your bot's real-time output\n",
"- Great for debugging and development"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part A: Using Screen\n",
"\n",
"### Step 1: Install Screen (if not already installed)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"source": [
"# Install screen on Ubuntu/Debian\n",
"sudo apt update\n",
"sudo apt install screen -y\n",
"\n",
"# Check if screen is installed\n",
"screen --version"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 2: Basic Screen Usage"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"source": [
"# Create a new screen session named 'trading-bot'\n",
"screen -S trading-bot\n",
"\n",
"# Inside the screen session, navigate to your bot directory\n",
"cd /root/trading-bot\n",
"\n",
"# Run your bot (you'll see live output)\n",
"python3 main.py"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 3: Screen Session Management"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"source": [
"# Detach from screen session (bot keeps running)\n",
"# Press: Ctrl+A, then D\n",
"\n",
"# List all screen sessions\n",
"screen -ls\n",
"\n",
"# Reattach to your session\n",
"screen -r trading-bot\n",
"\n",
"# If only one session exists, just use:\n",
"screen -r\n",
"\n",
"# Force reattach if session is marked as attached\n",
"screen -d -r trading-bot"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 4: Advanced Screen Commands"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"source": [
"# Create session with custom title\n",
"screen -S trading-bot -t \"Bot-Main\"\n",
"\n",
"# Create multiple windows in same session\n",
"# Inside screen:\n",
"# Ctrl+A, then C (create new window)\n",
"# Ctrl+A, then \" (list all windows)\n",
"# Ctrl+A, then 0-9 (switch to window number)\n",
"# Ctrl+A, then N (next window)\n",
"# Ctrl+A, then P (previous window)\n",
"\n",
"# Kill a screen session\n",
"screen -X -S trading-bot quit\n",
"\n",
"# Send command to screen session from outside\n",
"screen -S trading-bot -X stuff \"echo 'Bot status check'\\n\""
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 5: Screen Configuration (.screenrc)\n",
"\n",
"Create a custom screen configuration file:"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"source": [
"# Create screen configuration file\n",
"nano ~/.screenrc"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Contents of .screenrc:**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"source": [
"# Disable startup message\n",
"startup_message off\n",
"\n",
"# Set scrollback buffer\n",
"defscrollback 10000\n",
"\n",
"# Status line at bottom\n",
"hardstatus alwayslastline\n",
"hardstatus string '%{= kG}[%{G}%H%? %1`%?%{g}][%= %{= kw}%-w%{+b yk} %n*%t%?(%u)%? %{-}%+w %=%{g}][%{B}%m/%d %{W}%C%A%{g}]'\n",
"\n",
"# Enable mouse scrolling\n",
"termcapinfo xterm* ti@:te@\n",
"\n",
"# Auto-detach on hangup\n",
"autodetach on\n",
"\n",
"# Log output to file\n",
"deflog on\n",
"logfile /root/logs/screen-%Y%m%d-%n.log"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part B: Using Tmux (Alternative to Screen)\n",
"\n",
"### Step 1: Install Tmux"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"source": [
"# Install tmux on Ubuntu/Debian\n",
"sudo apt update\n",
"sudo apt install tmux -y\n",
"\n",
"# Check tmux version\n",
"tmux -V"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 2: Basic Tmux Usage"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"source": [
"# Create a new tmux session named 'trading-bot'\n",
"tmux new-session -s trading-bot\n",
"\n",
"# Or shorter version:\n",
"tmux new -s trading-bot\n",
"\n",
"# Inside tmux session, run your bot\n",
"cd /root/trading-bot\n",
"python3 main.py"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 3: Tmux Session Management"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"source": [
"# Detach from tmux session (bot keeps running)\n",
"# Press: Ctrl+B, then D\n",
"\n",
"# List all tmux sessions\n",
"tmux list-sessions\n",
"# or shorter:\n",
"tmux ls\n",
"\n",
"# Attach to session\n",
"tmux attach-session -t trading-bot\n",
"# or shorter:\n",
"tmux a -t trading-bot\n",
"\n",
"# Kill session\n",
"tmux kill-session -t trading-bot"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 4: Tmux Windows and Panes"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"source": [
"# Inside tmux session:\n",
"\n",
"# Create new window: Ctrl+B, then C\n",
"# Switch windows: Ctrl+B, then 0-9\n",
"# Next window: Ctrl+B, then N\n",
"# Previous window: Ctrl+B, then P\n",
"# List windows: Ctrl+B, then W\n",
"\n",
"# Split panes:\n",
"# Horizontal split: Ctrl+B, then \"\n",
"# Vertical split: Ctrl+B, then %\n",
"# Switch panes: Ctrl+B, then arrow keys\n",
"# Close pane: Ctrl+B, then X"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 5: Advanced Tmux Setup for Trading Bot"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"source": [
"# Create a comprehensive trading bot session\n",
"tmux new-session -d -s trading-bot -n main\n",
"tmux send-keys -t trading-bot:main \"cd /root/trading-bot\" Enter\n",
"tmux send-keys -t trading-bot:main \"python3 main.py\" Enter\n",
"\n",
"# Create additional windows for monitoring\n",
"tmux new-window -t trading-bot -n logs\n",
"tmux send-keys -t trading-bot:logs \"cd /root/trading-bot && tail -f bot.log\" Enter\n",
"\n",
"tmux new-window -t trading-bot -n system\n",
"tmux send-keys -t trading-bot:system \"htop\" Enter\n",
"\n",
"# Attach to the session\n",
"tmux attach-session -t trading-bot"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Step 6: Create Automated Setup Script"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"source": [
"# Create setup script\n",
"nano setup_trading_session.sh"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Contents of setup_trading_session.sh:**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"source": [
"#!/bin/bash\n",
"\n",
"SESSION_NAME=\"trading-bot\"\n",
"BOT_DIR=\"/root/trading-bot\"\n",
"\n",
"# Check if session already exists\n",
"tmux has-session -t $SESSION_NAME 2>/dev/null\n",
"if [ $? == 0 ]; then\n",
" echo \"Session $SESSION_NAME already exists. Attaching...\"\n",
" tmux attach-session -t $SESSION_NAME\n",
" exit 0\n",
"fi\n",
"\n",
"echo \"Creating new trading bot session...\"\n",
"\n",
"# Create new session with main window\n",
"tmux new-session -d -s $SESSION_NAME -n \"bot\"\n",
"tmux send-keys -t $SESSION_NAME:bot \"cd $BOT_DIR\" Enter\n",
"tmux send-keys -t $SESSION_NAME:bot \"echo 'Starting trading bot...'\" Enter\n",
"tmux send-keys -t $SESSION_NAME:bot \"python3 main.py\" Enter\n",
"\n",
"# Create logs window\n",
"tmux new-window -t $SESSION_NAME -n \"logs\"\n",
"tmux send-keys -t $SESSION_NAME:logs \"cd $BOT_DIR\" Enter\n",
"tmux send-keys -t $SESSION_NAME:logs \"tail -f *.log\" Enter\n",
"\n",
"# Create monitoring window with split panes\n",
"tmux new-window -t $SESSION_NAME -n \"monitor\"\n",
"tmux send-keys -t $SESSION_NAME:monitor \"htop\" Enter\n",
"tmux split-window -v -t $SESSION_NAME:monitor\n",
"tmux send-keys -t $SESSION_NAME:monitor \"watch -n 5 'df -h'\" Enter\n",
"\n",
"# Create development window\n",
"tmux new-window -t $SESSION_NAME -n \"dev\"\n",
"tmux send-keys -t $SESSION_NAME:dev \"cd $BOT_DIR\" Enter\n",
"\n",
"# Select the main bot window\n",
"tmux select-window -t $SESSION_NAME:bot\n",
"\n",
"echo \"Trading bot session created. Attaching...\"\n",
"tmux attach-session -t $SESSION_NAME"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"source": [
"# Make script executable\n",
"chmod +x setup_trading_session.sh\n",
"\n",
"# Run the setup script\n",
"./setup_trading_session.sh"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part C: Comparison - Screen vs Tmux\n",
"\n",
"### Screen\n",
"**Pros:**\n",
"- Simpler syntax and commands\n",
"- Lightweight and fast\n",
"- Available on almost all Unix systems\n",
"- Good for basic use cases\n",
"\n",
"**Cons:**\n",
"- Limited customization\n",
"- No pane splitting\n",
"- Less intuitive key bindings\n",
"\n",
"### Tmux\n",
"**Pros:**\n",
"- More powerful and flexible\n",
"- Better pane and window management\n",
"- Highly customizable\n",
"- Better mouse support\n",
"- More active development\n",
"\n",
"**Cons:**\n",
"- Steeper learning curve\n",
"- More complex configuration\n",
"- Might not be installed by default"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part D: Best Practices for Trading Bots\n",
"\n",
"### 1. Multiple Windows Setup"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"source": [
"# Window 1: Main bot execution\n",
"# Window 2: Log monitoring (tail -f logs)\n",
"# Window 3: System monitoring (htop, df -h)\n",
"# Window 4: Development/debugging\n",
"# Window 5: Market data (if applicable)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 2. Session Persistence"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"source": [
"# For tmux - save and restore sessions\n",
"# Install tmux-resurrect plugin\n",
"git clone https://github.com/tmux-plugins/tmux-resurrect ~/.tmux/plugins/tmux-resurrect\n",
"\n",
"# Add to ~/.tmux.conf:\n",
"# run-shell ~/.tmux/plugins/tmux-resurrect/resurrect.tmux\n",
"\n",
"# Save session: Ctrl+B, then Ctrl+S\n",
"# Restore session: Ctrl+B, then Ctrl+R"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### 3. Logging Within Sessions"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"source": [
"# Enable logging in screen\n",
"# Inside screen session: Ctrl+A, then H\n",
"\n",
"# Enable logging in tmux\n",
"# Add to ~/.tmux.conf:\n",
"# set -g history-limit 50000\n",
"# bind-key S capture-pane -p > ~/tmux-session.log"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part E: Monitoring and Troubleshooting\n",
"\n",
"### Quick Status Checks"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"source": [
"# Check if your session is running\n",
"screen -ls | grep trading-bot\n",
"# or\n",
"tmux ls | grep trading-bot\n",
"\n",
"# Quick attach without typing session name\n",
"# If only one session exists:\n",
"screen -r\n",
"# or\n",
"tmux a\n",
"\n",
"# Check processes inside session\n",
"ps aux | grep python3"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"### Emergency Recovery"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"source": [
"# If session appears stuck or unresponsive:\n",
"\n",
"# Force detach and reattach\n",
"screen -d trading-bot\n",
"screen -r trading-bot\n",
"\n",
"# Or for tmux:\n",
"tmux detach-client -s trading-bot\n",
"tmux a -t trading-bot\n",
"\n",
"# If session is completely broken, kill and restart:\n",
"screen -X -S trading-bot quit\n",
"# or\n",
"tmux kill-session -t trading-bot"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Part F: Integration with Other Tools\n",
"\n",
"### Combining with Systemd"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"source": [
"# Create a systemd service that starts a tmux session\n",
"sudo nano /etc/systemd/system/trading-bot-tmux.service"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"**Service file content:**"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"source": [
"[Unit]\n",
"Description=Trading Bot Tmux Session\n",
"After=network.target\n",
"\n",
"[Service]\n",
"Type=forking\n",
"User=root\n",
"ExecStart=/usr/bin/tmux new-session -d -s trading-bot -c /root/trading-bot 'python3 main.py'\n",
"ExecStop=/usr/bin/tmux kill-session -t trading-bot\n",
"Restart=on-failure\n",
"RestartSec=10\n",
"\n",
"[Install]\n",
"WantedBy=multi-user.target"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"## Pros and Cons Summary\n",
"\n",
"### ✅ Pros:\n",
"- **Interactive monitoring** - see real-time output\n",
"- **Easy debugging** - can interact with running bot\n",
"- **Session persistence** - survives SSH disconnections\n",
"- **Multiple windows** - organize different views\n",
"- **Flexible** - can run any commands in sessions\n",
"- **No additional setup** - works out of the box\n",
"\n",
"### ❌ Cons:\n",
"- **No automatic restart** on crash\n",
"- **Manual session management** - need to remember to detach\n",
"- **Resource usage** - sessions consume memory\n",
"- **Lost on reboot** - sessions don't survive server restart\n",
"- **Learning curve** - need to memorize key combinations\n",
"\n",
"### 🎯 Best Use Cases:\n",
"- Development and testing phase\n",
"- Active monitoring and debugging\n",
"- When you need to interact with the bot frequently\n",
"- Learning how your bot behaves\n",
"- Running multiple related processes"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.8.5"
}
},
"nbformat": 4,
"nbformat_minor": 4
}