在 Ubuntu 上“进程启动慢”可能指系统启动慢、某个服务启动慢,或某个应用程序打开慢。下面按常见场景给你一套排查与优化思路。
systemd-analyze
示例输出:
Startup finished in 3.2s (kernel) + 12.5s (userspace)
如果 userspace 很大,继续看:
systemd-analyze blame
会列出最慢的启动服务。
systemd-analyze critical-chain
看关键路径上的瓶颈服务。
禁用不常用服务(如打印、蓝牙、快照等):
systemctl list-unit-files --type=service | grep enabled
禁用示例:
sudo systemctl disable bluetooth.service
⚠️ 不确定用途的服务不要乱关。
查看磁盘类型:
lsblk -d -o name,rota
ROTA=1 → 机械硬盘ROTA=0 → SSD机械硬盘启动明显慢,建议:
noatime 挂载参数大磁盘开机 fsck 会拖慢启动。 查看:
systemctl status systemd-fsck*
可调整:
sudo tune2fs -c 30 /dev/sdX
内存不足时会频繁 swap,导致进程启动极慢。
查看:
free -h
swapon --show
建议:
echo 'vm.swappiness=10' | sudo tee -a /etc/sysctl.conf
sudo sysctl -p
time ./your_program
很多程序启动慢是因为:
测试:
ping localhost
如果慢,检查 /etc/hosts:
127.0.0.1 localhost
ldd your_program
库太多或路径慢会影响启动。
可用:
strace -f -e trace=openat,connect ./your_program
iostat -x 1
或:
iotop
systemd-analyze blame如果你能补充:
我可以给你更精准的优化方案。