
#!/bin/bash?标准回答:
指定脚本解释器,告诉系统用哪个 shell 来执行脚本
面试官加分点:
/bin/sh/bin/sh 可能指向 dash / bash$0 $1 $@ $* $# $? 各是什么意思?面试必说:循环参数时优先用
$@
if [ -f file ]; thenecho"file exists"fi常用判断:
-f 普通文件-d 目录-z 空字符串-eq -ne -gt -lt 数值比较awk '{print $1}' access.log | sort | uniq -c | sort -nr | head面试官关注点:
ps aux | sort -k3 -nr | head -5ls -l | grep '^-' | wc -l进阶说法:
find . -maxdepth 1 -type f | wc -lpkill -u username或:
ps -u username -o pid= | xargs kill -9生产环境要说明:慎用 -9
awk 'pattern {action}' file内置变量(必考):
NR:行号NF:字段数$1~$n:字段awk '{sum+=$2} END {print sum}' fileawk -F ':''{print $1}' /etc/passwd高分回答:
sed 's/old/new/g' filesed -i 's/8080/80/g' nginx.confsed '/^#/d' file模板回答:
把重复、易错、人工操作的事情,用脚本或工具固化下来
常见自动化场景:
#!/bin/bashTHRESHOLD=80df -P | awk 'NR>1 {print $5,$6}' | whileread use mount; do rate=${use%%%}if [ $rate -ge $THRESHOLD ]; thenecho"$mount usage $rate%" | mail -s "Disk Alert" adminfidone面试官更看 思路是否完整,不是语法完美
bash -x script.sh答:
systemd 是 Linux 新一代系统和服务管理器(PID=1),替代传统 SysVinit
核心能力:
一句话面试回答:
systemd 统一了启动、服务、日志和依赖管理,是现代 Linux 的“中枢调度器”。
**答:**Unit 是 systemd 管理的最小对象
常见类型:
systemctl start nginx
systemctl stop nginx
systemctl restart nginx
systemctl reload nginx
systemctl status nginx
面试追问:restart 和 reload 区别?
systemctl enable nginx
systemctl disable nginx
systemctl is-enabled nginx
底层原理:
systemctl list-units --type=service
systemctl list-unit-files
systemctl --failed
面试加分点:
systemctl --failed 是排查服务器“莫名异常”的第一条命令
[Unit]
Description=Nginx Service
After=network.target
[Service]
Type=forking
ExecStart=/usr/sbin/nginx
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s quit
PIDFile=/run/nginx.pid
Restart=on-failure
[Install]
WantedBy=multi-user.target
[Unit]
[Service]
Type(面试必问):
ExecStart:启动命令
Restart:自动重启策略
[Install]
systemctl daemon-reload
答:
journalctl
journalctl -u nginx
journalctl -xe
journalctl --since "2024-01-01"
journalctl -f
面试官最爱:
服务起不来,第一步:journalctl -xe
现象:
排查步骤:
常见原因:
现象:
原因:
处理:
排查思路:
systemctl is-enabled 服务名
systemctl --failed
服务异常 → systemctl status → journalctl -xe → 手动执行命令 → 查端口 / 权限 / 配置
因为 systemd 把“服务状态、依赖、日志、自恢复”全部收敛在一起, 一个熟练 systemd 的运维,可以不慌、不乱、可复盘。

点个赞吧
点击关注公众号,阅读更多精彩内容

以上内容仅为作者个人观点,仅供交流与探讨,欢迎各位读者在留言区理性讨论与交流。