Part 2 · System administration · Lesson 05 · 50 minutes
Find, stop and prioritise processes
- List processes, read their parent and state, and stop one with a signal
- Run jobs in the background and change the priority of a process
- Read a process from /proc and spot one that should not be there
The lab folder as it stands after this lesson. Unzip it in ~/krlab.
More
The zip holds every file that the course has printed up to here. To start this lesson with the files of the earlier lessons only, take the zip of the lesson before. Type the code yourself first: you learn more.
Scenario
A monitoring alert says that a server uses CPU although nobody works on it. Your lead gives you a shell on the box and asks three things: what is running, who started it, and from which file. In your lab, a process with an odd name waits for you. You find it, read its record, stop it, and write down what you saw. A second process of a batch job must keep running, but it must not slow down the others, so you lower its priority.
Recall
In /proc/PID/fd, a link ends with "(deleted)". What does it mean?
The process still has a file open that no longer has a name. The blocks stay in use until the process closes the file or ends. This is one reason why df and du can disagree.
umount says "target is busy". Which links in /proc/PID help you to find the guilty process?
/proc/PID/cwd shows the folder where the process works, and /proc/PID/fd shows its open files. A process with a link into the mounted folder is the one that you must stop first.
The idea
A process is a running program. It has a PID (process ID), a parent (the PID of the process that started it, shown as PPID) and an owner. PID 1 is the first process, systemd on most systems, and every other process descends from it. A daemon is a process that runs in the background without a terminal.
You talk to a process with a signal. The shell asks the kernel to deliver it:
- You → Shell: kill 4188
- Shell → Kernel: send SIGTERM to PID 4188
- Kernel → Process: deliver the signal
- Process: close files and exit
- Process → Kernel: report the exit
| Signal | Number | Effect |
|---|---|---|
SIGTERM | 15 | Asks the process to finish and exit. The default of kill. |
SIGKILL | 9 | The kernel ends it at once. It cannot clean up. |
SIGSTOP / SIGCONT | 19 / 18 | Pause it, and continue it. |
SIGHUP | 1 | "Hang up": a terminal closed. Many daemons reload their settings on it. |
Always try SIGTERM first. Every process also has a nice value from -20 to 19: the higher the number, the "nicer" the process is to the others, and the less CPU time it gets when the CPU is busy. The default is 0. A normal user may only raise the value of their own processes. Root may lower it.
Worked example
1. Find your shell and its children
$$ is the PID of your shell. sleep 300 waits for 300 seconds, so it is a safe test program. The sign & at the end of a line starts it in the background, and jobs lists the background jobs of your shell. ps (process status) lists processes: -o chooses the columns, and --ppid shows only the children of a PID.
mkdir -p ~/krlab/linux-admin && cd ~/krlab/linux-admin
echo "my shell is $$"
sleep 300 &
sleep 400 &
jobs
ps -o pid,ppid,stat,cmd --ppid $$my shell is 1054
[1]- Running sleep 300 &
[2]+ Running sleep 400 &
PID PPID STAT CMD
1055 1054 S sleep 300
1056 1054 S sleep 400
1057 1054 R ps -o pid,ppid,stat,cmd --ppid 1054All three have the PPID of your shell, even the ps that you ran for this list. STAT S means "sleeping" (waiting), R running, T stopped, Z a zombie (finished, but the parent has not collected it). bg %1 lets a stopped job run in the background and fg %1 brings job 1 to the front. Ctrl+Z stops the job at the front. nohup COMMAND & (no hang-up) keeps a job alive when you close the terminal.
2. Send signals
kill sends a signal. Despite its name, it does not always kill. -STOP pauses a process and -CONT lets it go on. %1 means job number 1 of your shell, so you need no PID. kill -l lists the signal names.
kill -STOP %1
ps -o pid,stat,cmd --ppid $$
kill -CONT %1
kill %1
kill -l | head -n 3 PID STAT CMD
1055 T sleep 300
1056 S sleep 400
1058 R ps -o pid,stat,cmd --ppid 1054
1) SIGHUP 2) SIGINT 3) SIGQUIT 4) SIGILL 5) SIGTRAP
6) SIGABRT 7) SIGBUS 8) SIGFPE 9) SIGKILL 10) SIGUSR1
11) SIGSEGV 12) SIGUSR2 13) SIGPIPE 14) SIGALRM 15) SIGTERMAn interactive shell tells you Terminated after kill %1. If a process ignores SIGTERM, use kill -9 PID, and do it last. End the other job too: kill %2.
3. List all processes
ps -ef shows all processes with their parent. ps -ef --forest draws the tree. The option --sort=-%mem puts the biggest memory user first. top shows the same list live (lesson 2). The output below comes from a test virtual machine. Your list differs.
ps -eo pid,ppid,user,%mem,comm --sort=-%mem | head -n 4
ps -ef --forest | head -n 5 PID PPID USER %MEM COMMAND
385 1 root 0.6 incus-agent
249 1 root 0.4 systemd-journal
1 0 root 0.3 systemd
UID PID PPID C STIME TTY TIME CMD
root 2 0 0 19:32 ? 00:00:00 [kthreadd]
root 3 2 0 19:32 ? 00:00:00 \_ [pool_workqueue_release]
root 4 2 0 19:32 ? 00:00:00 \_ [kworker/R-rcu_g]
root 5 2 0 19:32 ? 00:00:00 \_ [kworker/R-rcu_p]A name in square brackets is a kernel thread, and they all descend from kthreadd (PID 2). incus-agent is a helper of the test machine, and you will not see it on yours. pgrep -a NAME (process grep) finds processes by name and prints the PID and the command line. pkill signals by name: it hits every process with that name, so read the pgrep list first.
4. Change a priority
nice -n 5 COMMAND starts a command with the nice value 5. ps -o ni shows the value. renice -n 12 -p PID changes it for a running process.
nice -n 5 sleep 500 &
ps -o pid,ni,cmd -p $!
renice -n 12 -p $!
ps -o pid,ni,cmd -p $!
renice -n 2 -p $! PID NI CMD
1061 5 sleep 500
1061 (process ID) old priority 5, new priority 12
PID NI CMD
1061 12 sleep 500
renice: failed to set priority for 1061 (process ID): Permission denied$! is the PID of the last background job. The last command failed: a user may make a process nicer, and may not make it less nice. sudo renice -n 2 -p PID works, because root may do both. top has the same function: press r, and type the PID and the new value. Clean up with kill and the PID that ps showed.
5. Read a process from /proc
/proc is a folder that does not exist on the disk. The kernel fills it with a folder /proc/PID for every process. To play the attacker, copy bash under a hidden name (it starts with a dot), and start it with -c, which runs the given text as a command. exe and cwd are links in the process folder: the file that the process runs, and its working folder.
cp /bin/bash ./.cachefix
./.cachefix -c 'sleep 300; true' &
P=$!
ps -o pid,ppid,user,comm,args -p $P
ls -l /proc/$P/exe /proc/$P/cwd PID PPID USER COMMAND COMMAND
1067 1054 you .cachefix ./.cachefix -c sleep 300; true
lrwxrwxrwx 1 you you 0 Nov 21 21:06 /proc/1067/cwd -> /home/you/krlab/linux-admin
lrwxrwxrwx 1 you you 0 Nov 21 21:06 /proc/1067/exe -> /home/you/krlab/linux-admin/.cachefixA real hunt asks: is the name normal, is the file in a normal place, and does the owner make sense? A hidden name in a user folder fails all three. Now delete the file, as an intruder does to hide, and clean up. pkill -P signals the children of a PID.
rm .cachefix
ls -l /proc/$P/exe
pkill -P $P
kill $Plrwxrwxrwx 1 you you 0 Nov 21 21:06 /proc/1067/exe -> /home/you/krlab/linux-admin/.cachefix (deleted)The word (deleted) is a strong sign of trouble: the program runs from a file that no longer exists. The file /proc/PID/status holds the name, the state and the owner in plain text, and /proc/PID/cmdline holds the command line.
6. Know a zombie and a runaway
A zombie is a process that has ended, but its parent has not yet read its exit code, so its entry stays in the table. It uses no CPU and no memory. It is shown as Z and <defunct>.
You cannot kill a zombie, because it is already dead: you end the parent, or you make the parent call wait. The command below makes one. exec replaces the shell by sleep 100, which never reads the exit code of the child sleep 1.
- Parent → Child: starts sleep 1
- Child: ends after one second
- Child → Parent: waits for someone to read its exit code
- Parent: never calls wait: Child stays a zombie
bash -c 'sleep 1 & exec sleep 100' &
P=$!
sleep 2
ps -o pid,ppid,stat,cmd --ppid $P
kill $P PID PPID STAT CMD
1076 1074 Z [sleep] <defunct>A runaway process uses CPU or memory without end. In lesson 2, yes > /dev/null was one. Find it with top or ps -eo pid,%cpu,cmd --sort=-%cpu | head, read its parent and its exe, and end it with kill. Find out why it started, or it returns.
Your turn
Another odd process waits on your lab, and a batch job runs next to it. Run this block once. It builds the program, starts both in the background of your shell, and is not the task.
mkdir -p ~/krlab/linux-admin/ps-task && cd ~/krlab/linux-admin/ps-task
cp /bin/bash ./.updated
./.updated -c 'sleep 888; true' &
echo "started, PID $!"
nice -n 7 sleep 777 &
echo "batch job, PID $!"There are no steps here. Use the tools of the worked example to find the first process, to see which file it runs and from which folder, and to see how long it waits. Then pause it with a signal and read its state. Read the nice value of the batch job, then raise it to 15 with renice. Answer from your own terminal.
Now delete the file .updated, as an intruder would, and read the exe link again.
Last, let the process continue, end both processes with a signal (by PID, not by name), and check with ps that they are gone.
Check yourself
Why is "kill PID" better than "kill -9 PID" as a first try?
SIGTERM lets the program close files, save data and end its children. SIGKILL cannot be caught, so none of that happens. Use -9 only when the process ignores SIGTERM.
A process shows the name "sshd" in ps, but its exe is /tmp/x/sshd. What do you conclude?
It is almost certainly not the real SSH server, which lives in /usr/sbin. A process can set any name for itself, but /proc/PID/exe shows the real file.
You kill a parent process. What happens to its children?
They keep running. The system hands them to PID 1 (or to a "subreaper" process), so their PPID becomes 1. To end a whole group, signal the children as well, as pkill -P does.
You see 40 processes with the state Z. Does "kill -9" help?
No. A zombie is already dead and only waits for its parent. Find the parent with ps -o ppid, and fix or restart it. When the parent ends, PID 1 collects the zombies.
Field notes
- Look at the parent chain, not at one process. A web server whose child is
shorbashis a classic sign of a web shell.ps -ef --forestshows it. - Do not run
pkill NAMEorkillall NAMEon a production server without apgrep -a NAMEfirst. They match every process with that name, including those of other users that you may signal as root. - A priority is a hint to the scheduler, not a limit. For a hard limit, give a service CPU and memory limits in its unit file (
CPUQuota=andMemoryMax=; lesson 8 shows a unit file, not these two lines).ionicedoes the same for disk access. - Without root,
ls -l /proc/PID/exeof another user's process is denied. A full check of all processes needs root or a tool such asauditd. - Do not trust one tool alone. A rootkit can hide a process from
psby changing what the tool reads. Comparepswith the folders of/proc, and look at the machine from outside, for example with network logs.