Time -v in Linux: 5 Metrics to Diagnose Slow Scripts

Quick Linux Tip #113:

Question: My script is slow but I don't know if it's CPU, I/O, or waiting. How do I find out?

Try: /usr/bin/time -v ./slow_script.sh 2>&1 | grep -E 'Elapsed|CPU|Maximum|context|wait'

Info: Profiles resource bottlenecks by outputting detailed timing, CPU utilization, page faults, and context switches.

Examples:

  • $ /usr/bin/time -v command 2>&1 | tee runtime.log  # Profile command execution and save metrics to log
  • $ perf stat ./script  # Advanced performance profiling using CPU hardware counters
  • $ pidstat -u -r -d 1 $(pgrep script)  # Monitor real-time CPU, memory, and I/O usage for a live process

Note: Low CPU percentage with long runtime indicates an I/O or locking bottleneck. High involuntary context switches mean the kernel is preempting your process due to heavy system load.




LinuxTeck.com
linuxteck:~$ /usr/bin/time -v ./slow_script.sh 2>&1 | grep -E 'Elapsed|CPU|Maximum|context|wait'
        Elapsed (wall clock) time (h:mm:ss or m:ss): 0:12.34
        Percent of CPU this job got: 0%
        Maximum resident set size (kbytes): 234567
        Average total size (kbytes): 0
        Major (requiring I/O) page faults: 234
        Minor (reclaiming a frame) page faults: 4567
        Voluntary context switches: 12345
        Involuntary context switches: 234
linuxteck:~$

PREVIOUS ARTICLE Quick Linux Tip #112: Chroot in Linux: 3 Powerful Ways to Isolate Processes NEXT ARTICLE Quick Linux Tip #114: SSL Certificate Expiry: 3 Linux Commands to Check Dates
About John Britto

John Britto Founder & Chief-Editor @LinuxTeck. A Computer Geek and Linux Intellectual having more than 20+ years of experience in Linux and Open Source technologies.

View all posts by John Britto →

Leave a Reply

Your email address will not be published.