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
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:
Using tmux for Persistent Sessions
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
VS Code debugging
With the Python extension installed (see next episode), you can:
- Set breakpoints by clicking next to line numbers (red dots appear)
- Press F5 to start debugging
- Step through code with F10 (step over) or F11 (step into)
- Inspect variables in the Debug sidebar
This works for code running on Sagehen through the Remote-SSH connection.
Common Debugging Workflow
- Edit
analysis.pyin the VS Code editor - Test with a small dataset in the terminal:
python analysis.py --test - Fix errors visible in the terminal output
- When working, submit the full job:
sbatch run_analysis.sh - Check output:
tail -f logs/analysis_*.log - Iterate as needed
Challenge: tmux and Job Monitoring
- Open the terminal and create a tmux session:
tmux new-session -s workshop - Inside tmux, create a second window with Ctrl+B C
- In window 0, run:
watch -n 5 squeue -u username - Switch to window 1 (Ctrl+B N) for other commands
- Detach from tmux (Ctrl+B D)
- Reattach:
tmux attach-session -t workshop - 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.
- Run quick tests directly in the terminal; submit longer jobs through SLURM
- Use
srunfor 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