Ravindra BagaleCourses & study guides मराठी Track your progress

AWS Cloud

Linux Process Management: ps, top, htop, pgrep, jobs, bg, fg, nohup, nice and renice

Every running program is a process with a number (PID), an owner, a parent, and a state. In this chapter you learn to list processes (ps, pstree, pgrep), watch them live (top, htop), run them in the background and bring them back (&, jobs, bg, fg, nohup, disown), change their priority (nice, renice), and look inside them (/proc, lsof).

In this chapter

  • List and filter processes with ps, pgrep and pstree
  • Read top and htop: load, CPU, memory, states
  • Use jobs, &, Ctrl+Z, bg, fg
  • Keep jobs alive after logout with nohup and disown
  • Change priority with nice and renice
  • Inspect a process with /proc and lsof

1. An everyday example: a busy wada kitchen

In a Sadashiv Peth wada during Ganpati, the kitchen runs many tasks at once: rice on one stove, modak steaming, a pressure cooker, and someone chopping. Aaji keeps a token on each pot with a number and the cook's name. Some tasks wait for the cooker whistle. The modak can keep steaming while the cook steps out. When guests arrive, Aaji says 'rice first, sweets can wait'.

Each pot = a process. Token number = PID. Cook's name = user. Who started it = parent (PPID). Waiting for the whistle = sleeping state. Steaming while the cook is out = a background job with nohup. 'Rice first' = nice/renice priority. Aaji watching everything = top.

Life of a processThe shell forks a copy of itself (new PID, PPID = shell)The copy execs the program (ls, nginx, python)It runs (R) or waits for disk/network (S or D)It may be stopped (T) by Ctrl+Z or a signalIt exits with a code; until the parent reads it, it is a zombie (Z)The parent reaps it and the PID is free again
PID 1 is the init process for its PID namespace. An orphan goes to an eligible subreaper or init.

2. ps aux: every process, BSD style

A program is the set of instructions on disk. A process is one running copy. Two people can start the same program and get two different process IDs, or PIDs. In the ps output, start by locating the command and its user, then read the PID from that same row. Use the same PID when comparing a second snapshot. A PID can be reused after a process exits, so recheck the row before you act on an old number from your notes.

When a server is slow or an app is 'stuck', the first question is: what is running, as whom, and using how much?

ps aux shows all processes with USER, PID, %CPU, %MEM, VSZ, RSS (real memory, KB), TTY, STAT, START, TIME and COMMAND. STAT letters: R running, S sleeping, D uninterruptible (disk), T stopped, Z zombie; + foreground, s session leader, < high priority, N low priority.

STATMeaning
RRunning or ready
SSleeping, waiting for an event
DWaiting on disk/IO, cannot be killed
TStopped
ZZombie, finished but not reaped
  1. All processes, first lines.
ps aux | head -3

Output:

USER  PID %CPU %MEM    VSZ   RSS TTY STAT START TIME COMMAND
root    1  0.0  0.1 171820 13200 ?   Ss   09:01 0:02 /usr/lib/systemd/systemd
root    2  0.0  0.0      0     0 ?   S    09:01 0:00 [kthreadd]

PID 1 is systemd; [brackets] are kernel threads.

  1. Top memory users.
ps aux --sort=-rss | head -4

Output:

USER   PID %CPU %MEM ... RSS ... COMMAND
mysql 1420  3.1 24.0 ... 1960000 ... /usr/sbin/mysqld
ravi  8812 40.2 12.5 ... 1020000 ... python train.py

mysqld uses about 1.9 GB.

  1. Find one program.
ps aux | grep [n]ginx

Output:

root   980 0.0 0.0 ... nginx: master process
nginx  981 0.0 0.1 ... nginx: worker process

[n] stops grep matching itself.

3. ps -ef and ps -o: parents and custom columns

To know who started a process, or to print only the columns a script needs.

ps -ef shows UID, PID, PPID, C, STIME, TTY, TIME, CMD. ps -o pid,ppid,user,stat,%cpu,%mem,etime,cmd picks columns. -p PID for one process, -u USER for a user, -C NAME by command name, --forest for a tree, --no-headers for scripts.

  1. See the parent.
ps -ef | grep [g]unicorn

Output:

app  2201     1  0 09:05 ?  00:00:03 gunicorn master
app  2210  2201  1 09:05 ?  00:00:40 gunicorn worker

Workers' PPID is the master.

  1. Custom columns for one PID.
ps -o pid,ppid,user,stat,etime,cmd -p 2201

Output:

  PID  PPID USER STAT     ELAPSED CMD
 2201     1 app  Ss    3-02:11:09 gunicorn master

Running for 3 days 2 hours.

  1. Processes of one user.
ps -u postgres -o pid,%cpu,cmd --sort=-%cpu | head -3

Output:

  PID %CPU CMD
 3310 22.0 postgres: app shop 10.0.1.5 SELECT
 1500  0.3 postgres: checkpointer

One query is busy.

  1. By name for scripts.
ps -C sshd -o pid= | wc -l

Output:

3

No header, easy to count.

4. pstree: the family tree

A tree shows which service owns which workers and helps find what spawned a strange process.

pstree shows processes as a tree from systemd. -p adds PIDs, -u shows user changes, -a shows arguments. pstree -p PID shows one branch. pstree -s PID shows the parents up to PID 1.

  1. One service branch.
pstree -p 980

Output:

nginx(980)─┬─nginx(981)
           └─nginx(982)

Master with two workers.

  1. Who started this?
pstree -s -p 8812

Output:

systemd(1)───sshd(800)───sshd(8700)───bash(8701)───python(8812)

Started from an SSH session.

  1. Show users too.
pstree -u | head -3

Output:

systemd─┬─NetworkManager───2*[{NetworkManager}]
        ├─chronyd(chrony)
        ├─mysqld(mysql)───38*[{mysqld}]

(name) shows a user change; {} are threads.

5. pgrep and pidof: find PIDs by name

Scripts need the PID to send signals or check if something runs, without parsing ps output.

pgrep NAME prints matching PIDs. -l adds names, -a full command, -u USER filters, -f matches the full command line, -x exact name, -c counts, -n newest, -o oldest. pidof PROGRAM prints PIDs of an exact program name. Exit code 1 means none found.

  1. PIDs with names.
pgrep -l nginx

Output:

980 nginx
981 nginx

Quick and clean.

  1. Match the full command line.
pgrep -af 'python.*train'

Output:

8812 python train.py --epochs 50

-f looks at arguments too.

  1. Count a user's processes.
pgrep -c -u deploy

Output:

27

Simple monitoring.

  1. pidof for an exact binary.
pidof sshd

Output:

8700 800

Used in many init scripts.

  1. Use the exit code.
pgrep -x redis-server >/dev/null || echo 'redis is down'

Output:

redis is down

Good for health checks.

6. top: live view

Find the process in two refreshes of top. A brief CPU spike may be ordinary. If the same worker is busy across many refreshes while users report delays, write down its PID, the command and the time. Check CPU and memory separately. CPU measures how much processor time it is using, while the resident-memory column estimates how much of its memory is currently in RAM. High memory by itself doesn’t explain which processor is busy. Use the parent process and service name to connect your measurement to the application before deciding what to stop.

To see what is using CPU and memory right now, and how loaded the system is.

The header shows uptime, users, load average (1, 5, 15 min), task states, CPU (us user, sy system, wa IO wait, st steal), and memory. Keys: P sort by CPU, M by memory, 1 per-CPU, c full command, u user filter, k kill, r renice, q quit. top -b -n 1 prints once for scripts.

FieldLook for
load averageHigher than CPU count for long = overloaded
waHigh = waiting on disk
stHigh = noisy neighbour on cloud
avail MemLow = memory pressure
  1. The header.
top -b -n 1 | head -5

Output:

top - 11:20:01 up 3 days,  2 users,  load average: 3.92, 2.10, 1.05
Tasks: 212 total,   3 running, 209 sleeping,   0 stopped,   0 zombie
%Cpu(s): 88.0 us,  6.0 sy,  0.0 ni,  5.0 id,  0.5 wa,  0.0 hi,  0.5 si,  0.0 st
MiB Mem :   7820.0 total,    410.2 free,   5200.1 used,   2209.7 buff/cache
MiB Swap:   2048.0 total,   2048.0 free,      0.0 used.   2310.5 avail Mem

4 CPUs at load 3.9: nearly full, CPU bound.

  1. Top 3 by CPU in batch mode.
top -b -n 1 -o %CPU | sed -n '8,10p'

Output:

8812 ravi  20 0 2.1g 1.0g  9m R 380.0 12.5 python
1420 mysql 20 0 3.0g 1.9g 20m S  12.0 24.0 mysqld
 980 root  20 0  12m  4m  3m S   0.3  0.1 nginx

380% = nearly 4 cores.

  1. Watch one user.
top -u postgres

Output:

(interactive view)

Press q to quit.

7. htop: friendlier top

Colours, bars per CPU, mouse, tree view and easy kill make quick triage faster.

Install with dnf/apt install htop. F2 setup, F3 search, F4 filter, F5 tree, F6 sort, F7/F8 nice down/up, F9 kill, F10 quit, Space tags. htop -u USER, htop -p PID1,PID2.

  1. Install and run.
sudo dnf install -y htop && htop

Output:

(full-screen view with CPU bars)

Green = user, red = kernel time.

  1. Only some PIDs.
htop -p 980,981,982

Output:

(only nginx processes)

Focus on one service.

  1. Tree view.
htop  # then press F5

Output:

(processes shown as a tree)

Like pstree, but live.

8. & and jobs: run in the background

Try sleep 20 & in a test terminal. The shell prints a bracketed job number and a PID. The job number belongs to this shell, so jobs and fg %1 use it here. The PID belongs to the running process and is the number you can look up from another terminal. Bring the job to the foreground before it finishes, press Ctrl+Z, and read Stopped in jobs. Use bg to continue it and watch it disappear after the time is up.

Long tasks (a backup, a download) should not block your terminal.

CMD & starts it in the background and prints [job] PID. jobs lists jobs of this shell (-l adds PIDs). $! is the PID of the last background job. wait waits for background jobs. Jobs belong to the shell: they get SIGHUP when you log out.

  1. Start a backup in the background.
tar czf /tmp/etc.tgz /etc 2>/dev/null &

Output:

[1] 9120

Job 1, PID 9120. The prompt is free.

  1. List jobs.
jobs -l

Output:

[1]+  9120 Running    tar czf /tmp/etc.tgz /etc 2> /dev/null &

+ is the current job.

  1. Run two and wait.
sleep 3 & sleep 5 & wait; echo all done

Output:

[1] 9201
[2] 9202
all done

wait blocks until both finish.

  1. Save the PID.
./server.sh & echo $! > server.pid; cat server.pid

Output:

9310

Use it later to stop it.

9. Ctrl+Z, bg and fg: pause and move jobs

You started a long command in the foreground and need the prompt back without killing it.

Ctrl+Z stops (pauses) the foreground job (SIGTSTP). bg resumes it in the background. fg brings it to the foreground. Use %N for job N: fg %2, bg %1. A stopped job does not run until bg or fg.

  1. Pause a running copy.
rsync -a /data/ /backup/   # press Ctrl+Z

Output:

^Z
[1]+  Stopped    rsync -a /data/ /backup/

It is paused, not dead.

  1. Continue in the background.
bg %1

Output:

[1]+ rsync -a /data/ /backup/ &

Now running behind the prompt.

  1. Bring it back.
fg %1

Output:

rsync -a /data/ /backup/

Now you can press Ctrl+C if needed.

10. nohup and disown: survive logout

If SSH drops, background jobs get SIGHUP and die. A 3-hour import must survive.

nohup CMD & ignores SIGHUP and writes output to nohup.out (redirect it yourself). disown -h %1 tells the shell not to send SIGHUP to an already-running job. For long or important work prefer tmux/screen or a systemd service (systemd-run).

  1. Start an import that survives logout.
nohup ./import.sh > import.log 2>&1 &

Output:

[1] 9400

Safe to close the terminal.

  1. Save a job you forgot to nohup.
jobs; disown -h %1

Output:

[1]+ Running  ./reindex.sh &

It will not get SIGHUP now.

  1. Better: a transient systemd unit.
sudo systemd-run --unit=reindex /usr/local/bin/reindex.sh && systemctl status reindex --no-pager | head -3

Output:

Running as unit: reindex.service
● reindex.service - /usr/local/bin/reindex.sh
     Active: active (running)

Logs go to journalctl -u reindex.

11. nice: start with a lower or higher priority

A backup or a compile should not slow down the web app on the same server.

Nice values go from -20 (most CPU priority) to 19 (least). Default is 0. nice -n 10 CMD starts lower. Only root can set negative values. Shows as NI in top and ps -o ni. For disk priority use ionice -c3.

  1. Run a backup gently.
nice -n 19 tar czf /backup/www.tgz /srv/www &

Output:

[1] 9500

It uses CPU only when others do not need it.

  1. Check the value.
ps -o pid,ni,cmd -p 9500

Output:

  PID  NI CMD
 9500  19 tar czf /backup/www.tgz /srv/www

NI is 19.

  1. Normal user cannot go negative.
nice -n -5 ./job.sh

Output:

nice: cannot set niceness: Permission denied

Needs sudo.

  1. CPU and disk both gentle.
nice -n 15 ionice -c3 rsync -a /data/ /backup/

Output:

(no output)

ionice class 3 = idle IO.

12. renice: change priority of a running process

A report job is already eating CPU and users complain. Lower it without stopping it.

renice -n 10 -p PID changes one process. -u USER changes all of a user's processes. -g PGID a process group. Normal users can only increase (make nicer) their own processes; root can do anything.

  1. Lower a busy job.
renice -n 15 -p 8812

Output:

8812 (process ID) old priority 0, new priority 15

The web app gets CPU first now.

  1. Raise a critical one.
sudo renice -n -5 -p 1420

Output:

1420 (process ID) old priority 0, new priority -5

Only root can do this.

  1. All jobs of a user.
sudo renice -n 10 -u batch

Output:

1003 (user ID) old priority 0, new priority 10

Every process of batch.

13. /proc/PID: look inside a process

When tools are missing (containers) or you need exact details: command line, environment, limits, open files, memory.

Each process has a folder /proc/PID: cmdline (arguments, NUL-separated), status (state, memory, threads), environ, cwd and exe (links), fd/ (open files), limits, io. /proc/loadavg, /proc/meminfo and /proc/cpuinfo show system-wide numbers.

  1. Full command line.
tr '\0' ' ' < /proc/2201/cmdline; echo

Output:

/usr/bin/python3 /srv/app/venv/bin/gunicorn -w 4 app:server

Exact arguments used.

  1. State and memory.
grep -E 'State|VmRSS|Threads' /proc/2201/status

Output:

State:	S (sleeping)
VmRSS:	  182340 kB
Threads:	5

About 178 MB of real memory.

  1. Where is it running from?
sudo ls -l /proc/2201/cwd /proc/2201/exe

Output:

/proc/2201/cwd -> /srv/app
/proc/2201/exe -> /usr/bin/python3.11

Folder and binary.

  1. How many open files?
sudo ls /proc/2201/fd | wc -l; grep 'open files' /proc/2201/limits

Output:

812
Max open files            1024                 1024                 files

Close to the limit: risk of 'Too many open files'.

14. lsof: open files, ports and deleted files

'Port already in use', 'disk full but du shows less', 'cannot unmount: busy': lsof finds the process behind it.

lsof -p PID lists one process's files. lsof -i :PORT shows who uses a port. lsof -u USER by user. lsof +D DIR files open under a folder. lsof +L1 finds deleted but still open files (space not freed). -n -P skip DNS and port names (faster). ss -ltnp and fuser are alternatives.

  1. Who holds port 8080?
sudo lsof -nP -i :8080

Output:

COMMAND  PID USER FD TYPE DEVICE NODE NAME
java    7710 app  45u IPv6  9921  TCP *:8080 (LISTEN)

An old java is still running.

  1. Deleted files still using disk.
sudo lsof -nP +L1 | head -3

Output:

COMMAND  PID USER FD TYPE SIZE/OFF NLINK NAME
java    7710 app   3w REG  9.8G       0 /var/log/app.log (deleted)

9.8 GB is freed only when java restarts or the fd is truncated.

  1. Why cannot I unmount?
sudo lsof +D /mnt/usb | head -2

Output:

COMMAND PID USER FD TYPE NAME
bash   9020 ravi cwd DIR  /mnt/usb

A shell is inside the folder.

  1. Same with fuser.
sudo fuser -vm /mnt/usb

Output:

USER PID ACCESS COMMAND
/mnt/usb: ravi 9020 ..c.. bash

c = current directory.

15. Real examples from many industries

The same Linux skill shows up in every kind of company. Seeing it in many places helps you remember it.

Each row is a made-up team. It shows who uses this and how.

Team and cityHow they use itCommand
Sameer, e-commerce SRE, PuneFinds the PHP worker eating CPU during a saleps aux --sort=-%cpu | head
Ingrid, research lab, OsloRuns a 10-hour simulation that survives logoutnohup ./sim > sim.log 2>&1 &
Kwame, telecom, AccraLowers priority of nightly reportsrenice -n 15 -u reports
Yuki, game backend, OsakaFinds who holds port 7777lsof -nP -i :7777
Priyanka, bank ops, MumbaiFrees disk held by a deleted loglsof +L1
Mateo, startup, Buenos AiresWatches memory with htop during load testshtop -u app

16. Common errors and how to fix them

Everyone hits these errors. Knowing the cause saves hours.

Read the exact message first. Then match it to a row below.

Error you seeWhy it happensHow to fix
bash: fg: current: no such jobNo job in this shell (started elsewhere or with nohup)Find it with ps/pgrep.
pgrep -af import
nohup: ignoring input and appending output to 'nohup.out'Normal message, output went to nohup.outRedirect output yourself.
nohup ./job.sh > job.log 2>&1 &
Background job died after SSH disconnectIt got SIGHUPUse nohup, disown, tmux or systemd-run.
tmux new -s work
nice: cannot set niceness: Permission deniedNegative value needs rootUse sudo or a positive value.
sudo nice -n -5 ./job.sh
renice: failed to set priority for 1420: Permission deniedNot your process or lowering the valueUse sudo.
sudo renice -n 5 -p 1420
Error: listen EADDRINUSE: address already in use :::3000Another process holds the portFind and stop it.
sudo lsof -nP -i :3000
Too many open filesProcess hit its fd limitCheck /proc/PID/limits and raise LimitNOFILE.
grep 'open files' /proc/PID/limits
umount: /mnt/usb: target is busyA process uses the folderFind it with lsof/fuser.
sudo fuser -vm /mnt/usb
df shows full disk but du shows much lessDeleted files still openFind and restart the holder.
sudo lsof +L1
High load but low CPU in topProcesses stuck in D state waiting on disk/NFSLook at wa and D processes.
ps -eo pid,stat,cmd | awk '$2 ~ /D/'

17. Troubleshooting step by step

For a slow server: measure load, find the top process, see what it waits for, then act (renice, restart, or fix).

Go top to bottom. Stop at the first step that shows the problem.

  1. How loaded is it, and how many CPUs?
uptime; nproc
  1. Top CPU and memory users.
ps aux --sort=-%cpu | head -5
  1. Any D-state or zombies?
ps -eo pid,stat,cmd | awk '$2 ~ /^(D|Z)/'
  1. Is it IO wait?
top -b -n 1 | sed -n 3p
  1. What files and ports does it hold?
sudo lsof -p PID | head
  1. Lower its priority or restart its service.
sudo renice -n 15 -p PID

18. Lab 1: Find and tame a CPU hog

Start a CPU-heavy job, find it three ways, lower its priority, then stop it.

A full hands-on lab. Run every step on a test Linux machine (an EC2 instance, a VM, or WSL).

  1. Start a fake hog.
yes > /dev/null & echo $!

Output:

[1] 9601
9601
  1. Find it with ps.
ps -o pid,ni,%cpu,cmd -p 9601

Output:

  PID  NI %CPU CMD
 9601   0 99.5 yes
  1. Find it with pgrep and top.
pgrep -l yes; top -b -n 1 -o %CPU | sed -n 8p

Output:

9601 yes
9601 ravi 20 0 8m 1m 1m R 99.7 0.0 yes
  1. Lower its priority.
renice -n 19 -p 9601 && ps -o ni= -p 9601

Output:

9601 (process ID) old priority 0, new priority 19
 19
  1. Stop it.
kill %1; jobs

Output:

[1]+  Terminated   yes > /dev/null

19. Lab 2: Jobs, logout survival and lsof

Move a job between foreground and background, keep one alive after logout, and find a deleted file holding space.

A full hands-on lab. Run every step on a test Linux machine (an EC2 instance, a VM, or WSL).

  1. Start in foreground and pause.
sleep 300   # press Ctrl+Z

Output:

^Z
[1]+  Stopped    sleep 300
  1. Background, then foreground.
bg %1; jobs; fg %1   # then Ctrl+C

Output:

[1]+ sleep 300 &
[1]+  Running    sleep 300 &
  1. Survive logout.
nohup sleep 600 > /dev/null 2>&1 & echo $!

Output:

9705
  1. Hold a deleted file open.
python3 -c 'import time;f=open("/tmp/big","w");f.write("x"*50000000);f.flush();time.sleep(600)' & sleep 1; rm /tmp/big

Output:

[2] 9710
  1. Find it and clean up.
sudo lsof -nP +L1 | grep /tmp/big; kill 9710 9705

Output:

python3 9710 ravi 3w REG 50000000 0 /tmp/big (deleted)

Ravindra Bagale's Tip

When you are about to start anything that takes more than a few minutes on a remote server, run it inside tmux (or with systemd-run). An SSH drop will then never kill your work.

20. Frequently asked questions

Q. What is a PID?

A unique number for a running process.

Q. ps aux or ps -ef?

Both list everything. aux shows %CPU/%MEM; -ef shows PPID. Use -o for exactly what you need.

Q. What is load average?

The average number of processes running or waiting (R and D) over 1, 5 and 15 minutes. Compare it with the CPU count.

Q. Why is %CPU over 100?

Multi-threaded processes can use several cores; 400% = 4 cores.

Q. What is a zombie?

A finished process whose parent has not read its exit code. It uses no CPU or memory, only a PID slot.

Q. nohup or tmux?

nohup for a single fire-and-forget command; tmux when you want to reattach and see it live.

Q. Does nice affect disk speed?

No, nice is CPU. Use ionice for disk.

Q. Why does grep show up in ps | grep?

grep itself is a process with the word in its command. Use pgrep or grep [n]ame.

21. Interview questions and answers

Beginner level

Q. How do you list all processes?

ps aux or ps -ef.

Q. How do you run a command in the background?

Add & at the end.

Q. What does Ctrl+Z do?

Pauses (stops) the foreground job.

Q. How do you find the PID of nginx?

pgrep nginx or pidof nginx.

Mid level level

Q. Load average 8 on a 4-core box, CPU idle 70%. What is going on?

Many processes in D state waiting on disk or NFS; check wa and ps for D.

Q. How do you keep a job running after logout?

nohup CMD &, disown -h, tmux/screen, or systemd-run.

Q. Lower the priority of a running PID 4000.

renice -n 10 -p 4000.

Q. Which process uses port 5432?

sudo lsof -nP -i :5432 or ss -ltnp | grep 5432.

Senior level

Q. df says 100% but du finds only 40%. Explain and fix.

A process holds deleted files open. lsof +L1 to find, then restart it, or truncate via /proc/PID/fd/N; fix log rotation (copytruncate or reopen signal).

Q. Explain fork and exec.

fork copies the parent into a new process; exec replaces that copy's program. The shell does both for every command.

Q. Many zombies keep growing. What do you do?

Zombies are reaped by the parent; find the PPID (ps -o ppid=), fix or restart the parent; killing zombies does nothing.

Q. How do you investigate a process in a minimal container without ps?

Use /proc: ls /proc/[0-9]*, read /proc/PID/cmdline, status, fd, environ; or nsenter from the host.

Try at home

  1. Start three sleep commands in the background, list them with jobs, bring the second to the foreground, stop it with Ctrl+Z, then resume it with bg.
  2. Pick any running service and write its PID, PPID, user, memory (VmRSS), open file count and listening port using only ps, /proc and lsof.

Practise this chapter

Interview questions and answers

A long job is visible in one shell’s jobs output. Why doesn’t a new terminal list it?

A shell keeps its own job table for commands it launched. That table uses local job numbers. The kernel still identifies the running process by PID, which you can look up from another terminal. Check its process and user before acting on the PID.

Check your understanding

Choose an answer, then compare your reasoning with the explanation.

0 of 3 answered correctly
1. Two students start the same executable. What distinguishes the two running copies?

Answer and explanation

B. Each process has its own PID

A program on disk can make many running process instances. Linux tracks each instance with a process ID while it runs.

2. What does Ctrl+Z ordinarily do to the foreground job?

Answer and explanation

A. Stops it so you can resume it with bg or fg

The terminal sends a stop signal. The shell can list the stopped job, and bg or fg can continue it.

3. What is a useful first response to one short CPU spike in top?

Answer and explanation

C. Observe more samples for the same process and note if the spike repeats during the problem

A short spike can be expected work. Repeated readings tied to a symptom give a better basis for diagnosis.

Practice Tasks

Practice checks run in your browser. They check the answer for this exercise; they do not create resources or connect to your AWS account.

About this scenario

Milan Furniture is a furniture business in Milan. Giulia Rossi looks after its IT and cloud work. They count money in EUR and run their AWS resources in the eu-south-1 Region. In the tasks below you do real jobs for them using what this lesson teaches. All names, account IDs, IP addresses and numbers are made up for practice.

0 / 17

Easy All processes

Server is slow at BIZ.

List every process with user, CPU and memory (BSD style), most total CPU time first.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Processes with parents

Who started it?

List the processes of user kavya in full format showing PPID.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy PIDs by name

Need redis PIDs.

Print PIDs and names of processes matching redis.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Resume job in background

You paused it with Ctrl+Z.

Continue job number 3 in the background.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Only job PIDs

Which jobs are running?

Print only the PIDs of this shell's jobs.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Easy Bring job 2 forward

You need to press Ctrl+C on it.

Bring job number 4 to the foreground.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Top memory users

Memory alert fired.

List all processes BSD style sorted by VSZ, highest first.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Custom columns

Scripts need exact fields.

Show pid, ppid, user and cmd for PID 2201.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Tree with PIDs

See workers under a master.

Show the process tree of user kavya with PIDs.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Match full command line

Many python processes.

Show PID and full command for processes whose command line matches train.py.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium top once for a script

Capture a snapshot in cron.

Run top in batch mode for three iterations.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Survive logout

SSH may drop.

Run ./reindex.sh so it ignores the hangup signal, (output goes to nohup.out).

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Start with low priority

Backup must not slow the site.

Start tar czf www.tgz /srv/www with nice value 19.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Lower a running process

A report eats CPU.

Change the nice value of PID 6634 to 15.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Medium Who holds a port

EADDRINUSE on 9090.

Show the process using TCP port 9090 with lsof, no name lookups.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Hard Memory from /proc

ps is missing in the container.

Show the VmRSS line from /proc/2201/status.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

Hard Deleted files using disk

df full, du not.

List open files that were deleted (link count below 1) on the /var file system with lsof.

  1. Find the Linux command that does this job.
  2. Add the options (like -r or -h) the task needs.
  3. Add the file names, folders or users in the right order.
  4. Use sudo if the job changes system files or users. Click Check answer.

In short

  1. ps aux / ps -ef / ps -o; STAT R S D T Z.
  2. pstree for families; pgrep/pidof for PIDs.
  3. top/htop: load vs CPUs, wa, memory.
  4. &, jobs, Ctrl+Z, bg, fg; nohup, disown, tmux.
  5. nice -n 19 to start low; renice to change.
  6. /proc/PID and lsof show files, ports and deleted files.

Revision, comparison and interview cards

Revise this lesson in a few minutes. Save the cards on your phone or download them as PNG files. The QR code on each card opens this lesson.

Find processes. Know what eats CPU and RAM. ps aux, sort by usage. pgrep finds PIDs by name. Top memory users.
Revision card: Find processes · Download PNG · Open card page
Jobs and priority. Run long work without blocking. & background, jobs list, fg back. nice 19 is the gentlest. Low priority backup.
Revision card: Jobs and priority · Download PNG · Open card page
ps vs top. One snapshot. / Live, refreshing view.
Comparison: ps vs top · Download PNG · Open card page
nohup vs disown. Before starting the job. / After the job already runs.
Comparison: nohup vs disown · Download PNG · Open card page
High load, low CPU?. Linux load includes runnable tasks and tasks in uninterruptible sleep. A high number alone does not prove the CPU or disk is the bottleneck. Compare later samples from vmstat 1 5. Check runnable and blocked counters, CPU idle time, and device latency. Name the next measurement that could confirm or reject your hypothesis before changing a service.
Interview card: High load, low CPU? · Download PNG · Open card page
Job dies when SSH drops?. Logout sends SIGHUP. Use nohup, disown or tmux. Survive logout.
Interview card: Job dies when SSH drops? · Download PNG · Open card page
nice vs renice?. nice sets priority at start. renice changes a running one. Lower PID 8812.
Interview card: nice vs renice? · Download PNG · Open card page
Port already in use?. Another process holds it. lsof or ss shows who. Find it.
Interview card: Port already in use? · Download PNG · Open card page

More cards: Ravi Sir's Cards.