跪拜 Guibai
← All articles
Shell

The Shell Engineer's Toolkit, Part 2: Processes, Networks, and System State

By 柒号华仔 ·
Read original on juejin.cn ↗ Google Translate ↗ Alt translation

These commands form the operational half of shell scripting. Knowing how to safely inspect and signal processes, test network reachability at the port level, and pull system metrics without forking extra processes separates a brittle script from one that runs reliably in production.

Summary

The second installment of a shell command series shifts from static files to the dynamic, running system. It catalogs the tools for process management—ps, top, jobs, nohup, kill, and nice—and explains when to reach for each, from one-off snapshots to real-time monitoring and signal-based control. The network section covers connectivity testing with ping and nc, HTTP operations with curl, remote access via ssh and rsync, and DNS debugging with dig. A final block on system information details commands like uname, free, df, and direct reads from /proc, which avoids forking a new process in scripts. The emphasis throughout is on safe defaults, practical combinations, and the observation-first, action-second mindset required when commands can affect live services.

Takeaways
Use ps -ef for cross-platform process snapshots; ps -eo with --sort=-pcpu quickly identifies CPU hogs.
top provides a real-time dashboard, while htop adds mouse support and tree view for a better interactive experience.
Long-running tasks belong in the background with &, but nohup is needed to survive terminal hangup; for real daemons, use systemd.
Always send TERM before KILL, and verify targets with pgrep before using pkill to avoid collateral damage.
curl -f (fail on HTTP errors) paired with set -e is the standard pattern for robust API calls in shell scripts.
ss replaces netstat for viewing connections and offers cleaner, machine-parseable output.
dig is the go-to for DNS troubleshooting; nc -z tests port availability more reliably than ping for service readiness.
Reading from /proc (e.g., /proc/uptime) is more efficient than calling external commands because it avoids a fork.
Conclusions

The article frames shell proficiency not as memorizing flags but as pattern recognition—knowing which tool fits a given operational moment. This mirrors how experienced engineers actually work: they recall a capability, then look up the exact syntax.

The distinction between 'observation' and 'control' commands is a useful safety heuristic. Commands that can mutate system state (kill, pkill) demand a verification step first, a practice that is often skipped in hurried debugging.

Preferring direct /proc reads over command calls is a small optimization that compounds in scripts with many system checks, yet it remains underused outside of advanced Linux environments.

Concepts & terms
SIGHUP
A signal sent to a process when its controlling terminal is closed. By default, it terminates the process. nohup makes a process ignore this signal so it survives terminal disconnection.
Niceness
A value from -20 (highest priority) to 19 (lowest priority) that influences kernel CPU scheduling. Only root can assign negative values; regular users can only lower their processes' priority.
/proc filesystem
A virtual filesystem on Linux that exposes kernel and process data as files. Reading from /proc is faster than running utilities like ps or free because it avoids spawning a new process.
Source: juejin.cn ↗ Google Translate ↗ Backup ↗