OFFSET偏移
← All articlesOFFSET / FIELD NOTESNO. 040

Process and job management

Understand program execution and termination through process snapshots, signals, and foreground and background jobs.

English translation · Code and commands follow the Chinese original.

IN THESE READING PATHSLinux command-line fundamentals and composition ↗
About 4 min readStart reading

Viewing processes#

ps — a process snapshot

ps              # 当前终端启动的进程,极少用
ps aux          # 全系统所有进程,最常用
ps aux | grep nginx   # 筛选含 nginx 的进程

What each column in ps aux means:

Column Meaning How to read it
USER The user that owns the process root or your username
PID The process's unique ID You will need it for kill later
%CPU CPU usage Which process is consuming CPU
%MEM Memory usage Which process is consuming memory
VSZ/RSS Virtual / physical memory Usually RSS is the value to check
STAT Process state S=sleeping R=running Z=zombie
COMMAND The command that started the process This is what you are running

top — real-time process monitoring

top              # 进入交互界面,按 q 退出

Common keys inside top:

  • P — sort by CPU usage
  • M — sort by memory usage
  • k — enter a PID to kill the process
  • q — quit

htop — a colorful upgrade to top

htop             # 更直观,支持鼠标点击

Terminating processes#

kill — send a signal to a process

kill PID            # 礼貌地请进程退出(SIGTERM,信号 15)
kill -9 PID         # 强制杀死(SIGKILL,信号 9)
kill -l             # 列出所有信号类型

The sequence: first find the PID with ps aux | grep 进程名, then use kill PID. Use kill -9 only if the process does not respond.

pkill — kill processes by name

pkill nginx          # 杀掉所有名字匹配 nginx 的进程
pkill -9 -f "python script.py"   # 强制杀,匹配完整命令行

There is no need to look up the PID first, making this more direct than kill.

killall — kill by exact name

killall firefox      # 杀所有 firefox 进程

Foreground, background, and job control#

This is one of the most confusing topics for beginners, but the model is simple:

前台 = 霸占终端,运行期间你不能敲别的命令
后台 = Shell 不等待它结束;仍可能向终端输出,也可能因读取终端而暂停

& — start in the background

python train.py &       # 直接在后台启动
sleep 100 &             # 后台睡 100 秒

After starting, the shell prints [作业号] PID, for example [1] 12345.

jobs — list background jobs in the current terminal

jobs                # 列出所有后台作业
jobs -l             # 附带 PID

Example output:

[1]  - running    python train.py
[2]  + running    sleep 100

+ marks the most recently operated-on job (the default target of fg with no arguments).

fg — bring a background job to the foreground

fg          # 调回最近的那个
fg %1       # 调回作业号 1
fg %sleep   # 调回名字含 sleep 的

bg — resume a suspended job in the background

bg %1       # 把作业 1 放到后台继续

Ctrl+Z — suspend the current foreground task

Press Ctrl+Z in the terminal to suspend the current task and return to the shell. You can then:

bg          # 让它在后台继续跑
fg          # 或者调回前台继续

nohup — ignore hangup signals

nohup python train.py &     # 忽略 SIGHUP,通常可在终端关闭后继续;不保证跨注销策略存活
nohup python train.py > output.log 2>&1 &   # 输出写到文件

Working with systemctl#

systemctl manages systemd units and system state. By default, it connects to the system manager; systemctl --user connects to the user manager. User programs can also be managed as user services:

Scenario Use this
A program you run as a user ps / kill / jobs / &
System services (nginx/ssh/docker) systemctl start/stop/status
Start automatically at boot systemctl enable 服务名

System power management

systemctl also handles system-level operations such as shutdown, reboot, and suspend:

systemctl poweroff    # 关机(等价于直接敲 poweroff)
systemctl reboot      # 重启(等价于直接敲 reboot)
systemctl suspend     # 挂起/睡眠
systemctl hibernate   # 休眠(写盘后断电)

You can enter poweroff and reboot directly (without the systemctl prefix) because they existed historically as standalone commands. systemd retains them as compatibility entry points. On systemd systems, they are usually handled through those entry points; the specific file or symlink layout depends on the distribution.

This article uses systemctl suspend and systemctl hibernate. Standalone entry points and compatibility commands vary by distribution and installed tools. Hibernation also requires support from the kernel, swap space, and resume configuration.

For more on system services, see Linux login and sessions.


Everyday tasks: quick reference#

I want to... Command
See what is consuming CPU top or htop
Check whether a program is running ps aux | grep 关键词
Kill an unresponsive program pkill 程序名; if that fails, pkill -9
Run a script in the background ./script.sh &
Keep running after the terminal closes nohup ./script.sh &
See what is running in the background jobs
Bring a background job to the foreground fg
Suspend the current program Ctrl+Z


References & further reading

  1. GNU Coreutils manual ↗
  2. GNU Findutils manual ↗

Adapted from technical notes. Examples use generic names.

Continue along a reading path

Linux command-line fundamentals and composition

  1. 01File and directory operations
  2. 02Process and job management
  3. 03Text processing and system queries
  4. 04Shell fundamentals
  5. 05Fuzzy finding with fzf
  6. 06Passing arguments with xargs
CONTINUE READING / NEXT ARTICLEText processing and system queries

Move from paged viewing and text filtering to queries about memory, disks, networks, and ports.