Running and Debugging Code

Last updated on 2026-08-27 | Edit this page

Overview

Questions

  • How do I run code directly in the VS Code terminal?
  • How do I use tmux for persistent sessions?
  • What debugging workflows work well on Sagehen HPC?

Objectives

  • Run Python and other scripts from the integrated terminal
  • Use tmux for sessions that survive disconnections
  • Debug code with interactive Python and VS Code tools
  • Create complete SLURM job scripts within VS Code

Running Code in the Terminal


Quick tests on the login node

For short tests, run code directly in the terminal:

BASH

cd /rhome/<myusername>/project
python test_script.py --quick

For longer computations, always submit through SLURM to avoid overloading the login node.

Interactive GPU sessions

For prototyping with GPU access:

BASH

srun --partition=gpu --gres=gpu:1 --mem=16G --time=01:00:00 --pty bash
module load miniconda3
python my_gpu_script.py

A Complete Job Script Workflow


Create the SLURM script in the VS Code editor, then submit from the terminal:

BASH

#!/bin/bash
#SBATCH --job-name=my_analysis
#SBATCH --time=01:00:00
#SBATCH --cpus-per-task=4
#SBATCH --mem=8G
#SBATCH --output=logs/analysis_%j.log

module load miniconda3
python analysis.py --input data.csv --output results.csv

After submitting, check results:

BASH

ls -lh logs/               # See log files
tail logs/analysis_45678.log    # View output
head results.csv           # Preview results

Using tmux for Persistent Sessions


Callout

Session Persistence with tmux

tmux is a terminal multiplexer that keeps sessions running even if VS Code disconnects.

BASH

tmux new-session -s mywork       # Create session
# Work in tmux...
# Ctrl+B D to detach (session keeps running)
tmux attach-session -t mywork    # Reattach later
tmux list-sessions               # See all sessions

Useful tmux commands inside a session:

  • Ctrl+B C: Create new window
  • Ctrl+B N/P: Next/previous window
  • Ctrl+B D: Detach (session continues running)

Debugging Strategies


Interactive Python debugging

BASH

python -i my_script.py      # Start interactive mode after script runs
>>> variable_name           # Inspect variables
>>> exit()                  # Exit

VS Code debugging

With the Python extension installed (see next episode), you can:

  1. Set breakpoints by clicking next to line numbers (red dots appear)
  2. Press F5 to start debugging
  3. Step through code with F10 (step over) or F11 (step into)
  4. Inspect variables in the Debug sidebar

This works for code running on Sagehen through the Remote-SSH connection.

Common Debugging Workflow


  1. Edit analysis.py in the VS Code editor
  2. Test with a small dataset in the terminal: python analysis.py --test
  3. Fix errors visible in the terminal output
  4. When working, submit the full job: sbatch run_analysis.sh
  5. Check output: tail -f logs/analysis_*.log
  6. Iterate as needed
Challenge

Challenge: tmux and Job Monitoring

  1. Open the terminal and create a tmux session: tmux new-session -s workshop
  2. Inside tmux, create a second window with Ctrl+B C
  3. In window 0, run: watch -n 5 squeue -u username
  4. Switch to window 1 (Ctrl+B N) for other commands
  5. Detach from tmux (Ctrl+B D)
  6. Reattach: tmux attach-session -t workshop
  7. Verify your watch command is still running

After detaching and reattaching, window 0 still shows the watch command running with periodic squeue updates. This demonstrates that tmux sessions persist through VS Code disconnections. To kill the tmux session when done: tmux kill-session -t workshop.

Key Points
  • Run quick tests directly in the terminal; submit longer jobs through SLURM
  • Use srun for interactive GPU sessions for prototyping
  • tmux sessions persist through disconnections – ideal for long-running monitoring
  • VS Code’s debugger works remotely through Remote-SSH with language extensions
  • The edit-test-submit cycle is the core HPC development workflow