هذا الجزء لم يُترجم بعد. أنت ترى النص الإنجليزي.
$ مستخدم عادي# بصلاحية root (أو مع sudo)
المهارات في هذا الفصل
- إيجاد العمليات وإيقافها
Know what a process is
A process is a program in memory. It has a PID, a parent (PPID), an owner (user), and a state.
echo $$
4321
ps -o pid,ppid,user,stat,cmd -p $$
PID PPID USER STAT CMD
4321 4320 student Ss -bash
pstree -p | head
systemd(1)─┬─NetworkManager(812)─┬─{NetworkManager}(830)
├─sshd(1022)───sshd(4318)───sshd(4320)───bash(4321)───pstree(4400)- PID 1 is
systemd, the first process. All others are its children or grandchildren. $$is the PID of your own shell.
| State | Letter | Meaning |
|---|---|---|
| Running | R | On the CPU or ready to run |
| Sleeping | S | Waits for something (input, network, a timer) — most processes |
| Uninterruptible | D | Waits for the disk or NFS; can not be killed until the wait ends |
| Stopped | T | Paused (for example with Ctrl+Z) |
| Zombie | Z | Finished, but the parent did not read its exit status yet |
List processes with ps
ps shows a snapshot. ps aux shows all processes of all users; ps -ef is the same in another style.
ps
ps aux | head -5
USER PID %CPU %MEM VSZ RSS TTY STAT START TIME COMMAND
root 1 0.0 0.4 174168 16840 ? Ss Oct25 0:04 /usr/lib/systemd/systemd --switched-root --system
root 2 0.0 0.0 0 0 ? S Oct25 0:00 [kthreadd]
ps -ef | grep -v grep | grep httpd
ps aux --sort=-%cpu | head -6
ps aux --sort=-%mem | head -6
ps -u apache -o pid,%cpu,%mem,cmd
ps -eo pid,user,ni,stat,lstart,cmd --sort=-pcpu | head
pgrep -a sshd| Column | Meaning |
|---|---|
%CPU / %MEM | Share of CPU / of memory |
RSS | Real memory in use (KiB) |
TTY | Terminal (? = no terminal: a service) |
STAT | State (s = session leader, + = foreground, < / N = high / low priority) |
TIME | CPU time used so far |
[name] | A kernel thread |
Watch live with top
top shows the processes live, sorted by CPU, with the load and the memory at the top.
Read the load average
The load average is the number of processes that want the CPU (or wait for the disk), as an average over 1, 5 and 15 minutes.
uptime
11:42:01 up 2 days, 5:23, 2 users, load average: 3.85, 2.10, 1.05
nproc
4
lscpu | grep '^CPU(s):'
CPU(s): 4Compare the load with the number of CPU cores. On 4 cores, a load of 4.0 means "fully busy". Here 3.85 (1 min) is higher than 1.05 (15 min): the load is growing.
تذكّر
Load / cores below 1 = fine. About 1 = full. Clearly above 1 = processes wait. A high load with a low %CPU often means processes wait for a slow disk or NFS (state D).
Control jobs in the shell
A shell can run commands in the background (&) and move them between background and foreground.
sleep 600 &
[1] 4512
sleep 900
^Z
[2]+ Stopped sleep 900
jobs
[1]- Running sleep 600 &
[2]+ Stopped sleep 900
bg %2
fg %1
^C
nohup ./long-backup.sh > backup.out 2>&1 &| Command / key | What it does |
|---|---|
CMD & | Start in the background |
Ctrl+Z | Stop (pause) the foreground job |
jobs | List the jobs of this shell |
bg %N / fg %N | Go on in the background / bring to the foreground |
Ctrl+C | End the foreground job |
nohup CMD & | Keep running after you log out |
ملاحظة
Jobs belong to one shell. A long task on a server is better in tmux, in a systemd service, or with systemd-run.
Stop processes with signals
kill sends a signal to a process. Most signals ask it to stop; the program can clean up first. SIGKILL stops it at once, with no clean-up.
| Signal | Number | Meaning |
|---|---|---|
SIGHUP | 1 | "Hang up" — many services read their config again |
SIGINT | 2 | Interrupt, same as Ctrl+C |
SIGKILL | 9 | Kill at once; can not be caught or ignored |
SIGTERM | 15 | Please end (the default of kill) |
SIGCONT / SIGSTOP | 18 / 19 | Continue / pause |
kill 4512
kill -15 4512
kill -9 4512
kill -l
pkill -u anna sleep
pkill -HUP rsyslogd
killall -s TERM httpd
pkill -KILL -t pts/2
kill %1تذكّر
First kill PID (TERM). Wait a few seconds. Only if it still runs, use kill -9 PID. With -9 the program can not save data or remove its lock files.
انتبه
pkill name matches part of the name: pkill ssh also kills sshd and every SSH session. Check first with pgrep -a name.
Log out other users
To end the session of another user, kill the processes on his terminal or of his user.