strace on Linux: Debug Slow Scripts and Permission Errors (Syscalls)

strace linux - custom-strace-featured.png

strace on Linux is the fastest way to answer “what file or socket is this binary actually touching?” — especially when logs are empty but the app exits with code 1. It records every syscall the process makes: open, read, connect, execve, and more.

Pair with: lsof,
journalctl,
kill & signals.

When strace Helps

Mystery permission denials, wrong config paths, DNS/connect hangs, and cron jobs that work interactively but fail unattended.

syscalls
debug

Run strace on a Command

Wrap the program once; use -f to follow child processes (shell scripts, workers).

bashtrace script
strace -f -o /tmp/app.strace ./deploy.sh 2>&1 | tail -20
-f
-o log

Filter Noise

Full traces are huge — limit to file or network syscalls while you iterate.

bashfiles only
strace -e trace=openat,open,stat,access -f ./myapp
-e trace=
openat

Attach to a Running PID

Useful for daemons; requires permission to ptrace (often root or same user).

bashattach
sudo strace -p $(pidof nginx | awk '{print $1}') -e trace=network
strace -p
attach

Performance and Production

strace slows programs — use short windows and filters, not permanent tracing on hot paths.

WarningOn busy production hosts, prefer targeted logging or eBPF tools — strace can distort timing bugs.
overhead
short run

Read Common Errors

ENOENT = path not found; EACCES = permissions; ECONNREFUSED = nothing listening on that port.

bashgrep log
grep -E 'ENOENT|EACCES|ECONNREFUSED' /tmp/app.strace | tail -20
ENOENT
EACCES

Confirm the Root Cause

Reproduce once with a minimal filter, fix the path or ACL, rerun without strace.

minimal repro
fix

Quick Reference

  • strace -f -e trace=openat,connect ./cmd for quick wins
  • Save: -o /tmp/trace.log
  • Attach: strace -p PID (careful in prod)

Related tutorials

Diagrams are original illustrations by Gnome IT Solutions. Tutorial text © Gnome IT Solutions.