在CentOS系统上进行Node.js故障排查可以通过以下几个步骤进行:
检查日志:
journalctl
命令查看系统日志,特别是与Node.js服务相关的日志。例如:journalctl -u your-nodejs-service-name -t
cat logs/app.log
less logs/app.log
tail -f logs/app.log
检查进程和服务:
ps
、top
和 systemctl
等工具检查Node.js进程的状态和资源使用情况。例如:ps aux | grep node
top -p <pid>
systemctl status your-nodejs-service
检查网络连接:
ping
、traceroute
和 netstat
等工具检查网络连接和端口状态。例如:ping google.com
netstat -an | grep <port>
检查系统资源:
top
、free
和 df
等工具检查CPU、内存和磁盘空间使用情况。例如:free -m
df -htop
使用调试工具:
debugger;
语句,然后使用 node inspect your_script.js
命令运行脚本。性能监控:
process
模块获取进程信息,如CPU使用率和内存使用情况。例如:const os = require('os');
const cpuUsage = process.cpuUsage();
console.log(`CPU 使用率: ${cpuUsage.user} ${cpuUsage.system}%`);
const totalMemory = os.totalmem();
const freeMemory = os.freemem();
console.log(`内存使用情况: ${(totalMemory - freeMemory) / totalMemory * 100}%`);
express-status-monitor
提供实时的服务器性能指标和健康检查。内存泄漏排查:
heapdump
模块导出内存快照,然后在Chrome DevTools中进行分析。例如:const heapdump = require('heapdump');
heapdump.writeSnapshot('/tmp/heapdump.heapsnapshot');
通过以上步骤,可以系统地排查CentOS系统中Node.js应用程序的故障,从日志分析、进程状态检查到性能监控和内存泄漏排查,确保应用程序的稳定运行。