Here's a complete guide to run your Telegram AI tool in the background on Termux: Method 1: Using nohup (Simple) bash
nohup python telegram_ai_tagger.py > bot.log 2>&1 &
ps aux | grep python
tail -f bot.log
pkill -f "python telegram_ai_tagger.py"
Method 2: Using tmux (Recommended) Install and setup tmux: bash
pkg install tmux
Create a tmux session: bash
tmux new-session -d -s telegram-bot
tmux attach-session -t telegram-bot
python telegram_ai_tagger.py
To detach from tmux (keep running in background): bash
Manage tmux sessions: bash
tmux list-sessions
tmux attach-session -t telegram-bot
tmux kill-session -t telegram-bot
Method 3: Using screen bash
pkg install screen
screen -S telegram-bot
python telegram_ai_tagger.py
screen -ls
screen -r telegram-bot
screen -X -S telegram-bot quit
Method 4: Create a Startup Script Create management script manage_bot.sh: bash
#!/data/data/com.termux/files/usr/bin/bash
SCRIPT_DIR="$(cd "$(dirname "$0")" && pwd)" SCRIPT_NAME="telegram_ai_tagger.py" LOG_FILE="$SCRIPT_DIR/bot.log" PID_FILE="$SCRIPT_DIR/bot.pid"
case "$1" in start) if [ -f "$PID_FILE" ] && ps -p $(cat "$PID_FILE") > /dev/null; then echo "Bot is already running (PID: $(cat "$PID_FILE"))" else echo "Starting Telegram AI Bot..." nohup python "$SCRIPT_DIR/$SCRIPT_NAME" > "$LOG_FILE" 2>&1 & echo $! > "$PID_FILE" echo "Bot started with PID: $!" echo "Logs: $LOG_FILE" fi ;; stop) if [ -f "$PID_FILE" ]; then PID=$(cat "$PID_FILE") if ps -p $PID > /dev/null; then kill $PID echo "Bot stopped (PID: $PID)" rm -f "$PID_FILE" else echo "Bot is not running" rm -f "$PID_FILE" fi else echo "PID file not found. Is the bot running?" fi ;; status) if [ -f "$PID_FILE" ]; then PID=$(cat "$PID_FILE") if ps -p $PID > /dev/null; then echo "Bot is running (PID: $PID)" echo "=== Recent Logs ===" tail -20 "$LOG_FILE" else echo "Bot is not running (stale PID file)" rm -f "$PID_FILE" fi else echo "Bot is not running" fi ;; restart) $0 stop sleep 2 $0 start ;; logs) tail -f "$LOG_FILE" ;; *) echo "Usage: $0 {start|stop|status|restart|logs}" exit 1 ;; esac
Make it executable and use it: bash
chmod +x manage_bot.sh
./manage_bot.sh start
./manage_bot.sh status
./manage_bot.sh logs
./manage_bot.sh stop
./manage_bot.sh restart
Method 5: Auto-start on Termux Boot Install Termux:Boot app:
Install F-Droid from https://f-droid.org
Install Termux:Boot from F-Droid
Install Termux:API for additional features
Create boot script: bash
mkdir -p ~/.termux/boot
cat > ~/.termux/boot/start-telegram-bot << 'EOF' #!/data/data/com.termux/files/usr/bin/bash
sleep 30
cd /data/data/com.termux/files/home/telegram-ai-tagger
./manage_bot.sh start EOF
chmod +x ~/.termux/boot/start-telegram-bot
Method 6: Using Termux Services Create service script: bash
mkdir -p ~/.termux/services
cat > ~/.termux/services/telegram-bot << 'EOF' #!/data/data/com.termux/files/usr/bin/bash while true; do cd /data/data/com.termux/files/home/telegram-ai-tagger python telegram_ai_tagger.py sleep 10 # Wait before restarting if crashed done EOF
chmod +x ~/.termux/services/telegram-bot
Complete Setup Script for Termux
Create setup_termux.sh: bash
#!/data/data/com.termux/files/usr/bin/bash
echo "Setting up Telegram AI Bot on Termux..."
pkg update && pkg upgrade -y
pkg install python git tmux wget -y
pip install --upgrade pip pip install telethon openai
mkdir -p ~/telegram-ai-tagger cd ~/telegram-ai-tagger
echo "Downloading script files..."
echo "Making scripts executable..." chmod +x manage_bot.sh
echo "Setup complete!" echo "Edit config in telegram_ai_tagger.py first" echo "Then run: ./manage_bot.sh start"
Monitoring and Maintenance Check resource usage: bash
top htop # if installed
df -h
netstat -tulpn
Log rotation script rotate_logs.sh: bash
#!/data/data/com.termux/files/usr/bin/bash LOG_FILE="bot.log" if [ -f "$LOG_FILE" ] && [ $(stat -c%s "$LOG_FILE") -gt 10485760 ]; then # 10MB mv "$LOG_FILE" "bot_$(date +%Y%m%d_%H%M%S).log" gzip "bot_$(date +%Y%m%d_%H%M%S).log" fi
Add to crontab for automatic log rotation: bash
crontab -e
0 2 * * * cd /path/to/your/bot && ./rotate_logs.sh
Troubleshooting Background Running Check if bot is running: bash
ps aux | grep -v grep | grep "python telegram_ai_tagger.py"
Check Termux wake lock: bash
termux-wake-lock
termux-wake-unlock
Check battery optimization: bash
termux-battery-optimization
Monitor with a simple watchdog script watchdog.sh: bash
#!/data/data/com.termux/files/usr/bin/bash if ! ps aux | grep -v grep | grep -q "python telegram_ai_tagger.py"; then echo "Bot is not running! Restarting..." cd /path/to/your/bot ./manage_bot.sh start fi
Recommended Approach
For best results, I recommend:
Use Method 2 (tmux) for development and testing
Use Method 4 (management script) for production
Set up Termux:Boot for auto-start after reboot
Use the watchdog script with crontab for automatic recovery
This setup will keep your Telegram AI bot running 24/7 in the background on Termux!