AI Hints
Home›Linux Tutorial›Stopping Processes
Linux · Lesson 84 of 120

Practice: A Runaway Process

Find it and stop it.

⏱ 2 min read▶ Video at 1:52:10🎓 Free
Watch this lesson in the Linux courseWatch on YouTube ↗Full Linux course

A buggy script has got stuck in a loop, and it's eating all the processor time. The computer is crawling. Your job is to find it, and stop it. Pause, and think about which commands you'd use.

Practice: A Runaway Process
From the video: Practice: A Runaway Process
  • A script stuck in a loop
  • Eating the processor
  • Find it, stop it

Find the troublemaker

A snapshot of top, sorted by processor use, shows the culprit right at the top. Hog dot SH, using nearly one hundred percent of a processor. Its PID is in the first column.

Terminal
top -bn1 -o %CPU | sed -n "7,9p"
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
   22 sam       20   0    4624   3096   2840 R 100.0   0.0   0:02.22 hog.sh
    1 sam       20   0    4460   3148   3020 S   0.0   0.0   0:00.01 bash

Stop it

P kill hog asks it to stop. P grep checks that it's gone. And another snapshot of top shows the processor is calm again. If the polite signal hadn't worked, we'd have used P kill dash nine.

Terminal
pkill hog
pgrep hog || echo "hog is gone"
hog is gone
top -bn1 -o %CPU | sed -n "7,9p"
  PID USER      PR  NI    VIRT    RES    SHR S  %CPU  %MEM     TIME+ COMMAND
    1 sam       20   0    4460   2984   2856 S   0.0   0.0   0:00.01 bash
   56 sam       20   0    9216   4856   2936 R   0.0   0.0   0:00.00 top

The rescue routine

Remember this routine. Look, with top or PSAUX. Identify the PID, or the name. Ask politely, with kill, or P kill. And only if it ignores you, force it, with dash nine. Then find out why it happened, so it doesn't happen again.

keyvalue
Looktop, ps aux
IdentifyPID or name
Askkill / pkill
Forcekill -9, last resort

Summary

Top found the runaway process, and P kill stopped it. Look, identify, ask, then force. Next, environment variables.

Key takeaways
  • top -o %CPU
  • pkill name
  • kill -9 as a last resort
Next up: Environment Variables — settings for every program