本文介绍了如何在 Node.js 环境中通过 child_process 模块执行常见的 Linux Shell 命令。内容涵盖获取 CPU 温度、硬盘 SMART 信息、网络接口、系统日志、内存使用、最大文件查找、启动时间、服务状态检查、公网 IP 及软件包更新等实用场景,提供基于 exec 的完整代码示例与说明。
尽管 Node.js 运行时环境提供了多种与操作系统交互的 API,但在某些特定场景下(例如获取特定的系统信息),直接调用 Shell 命令往往更加便捷。本文整理了一些适合在 Node.js 中执行的 Shell 命令示例。
获取 CPU 温度
const { exec } = require('child_process');
exec('sensors', (err, stdout, stderr) => {
if (err) {
console.error(`Error: ${err}`);
return;
}
console.log(`CPU Temperature:\n${stdout}`);
console.error(`stderr: ${stderr}`);
});
上述代码通过调用 sensors 命令来获取 CPU 的温度信息。
获取硬盘 SMART 信息
const { exec } = require('child_process');
exec('smartctl -a /dev/sda', (err, stdout, stderr) => {
if (err) {
console.error(`Error: ${err}`);
return;
}
console.log(`Hard Disk SMART Information:\n${stdout}`);
console.error(`stderr: ${stderr}`);
});
通过执行 smartctl 命令,可以获取硬盘的 SMART 健康状态信息。
获取网络接口信息
const { exec } = require('child_process');
exec('ifconfig', (err, stdout, stderr) => {
if (err) {
console.error(`Error: ${err}`);
return;
}
console.log(`Network Interfaces:\n${stdout}`);
console.error(`stderr: ${stderr}`);
});
使用 ifconfig 命令可以获取当前系统的网络接口配置信息。
获取系统日志
const { exec } = require('child_process');
exec('journalctl', (err, stdout, stderr) => {
if (err) {
console.error(`Error: ${err}`);
return;
}
console.log(`System Journal:\n${stdout}`);
console.error(`stderr: ${stderr}`);
});
利用 journalctl 命令,可以读取并输出系统的日志信息。
获取系统内存使用情况
const { exec } = require('child_process');
exec('free -h', (err, stdout, stderr) => {
if (err) {
console.error(`Error: ${err}`);
return;
}
console.log(`Memory Usage:\n${stdout}`);
console.error(`stderr: ${stderr}`);
});
通过 free 命令,可以查看系统当前的内存使用状况。
查找系统中最大的文件
const { exec } = require('child_process');
exec('find / -type f -exec du -h {} + | sort -rh | head -n 1', (err, stdout, stderr) => {
if (err) {
console.error(`Error: ${err}`);
return;
}
console.log(`Largest File:\n${stdout}`);
console.error(`stderr: ${stderr}`);
});
该命令会遍历系统,查找体积最大的文件并返回相关信息。
获取系统启动时间
const { exec } = require('child_process');
exec('uptime -s', (err, stdout, stderr) => {
if (err) {
console.error(`Error: ${err}`);
return;
}
console.log(`System Start Time:\n${stdout}`);
console.error(`stderr: ${stderr}`);
});
使用 uptime 命令可以获取系统的启动时间。
检查系统是否在运行特定服务
const { exec } = require('child_process');
exec('systemctl is-active apache2', (err, stdout, stderr) => {
if (err) {
console.error(`Error: ${err}`);
return;
}
const isActive = stdout.trim() === 'active';
console.log(`Apache2 Status: ${isActive ? 'Running' : 'Inactive'}`);
console.error(`stderr: ${stderr}`);
});
上述代码用于检查 Apache2 服务当前是否处于运行状态。
获取系统 IP 地址
const { exec } = require('child_process');
exec('curl ifconfig.me', (err, stdout, stderr) => {
if (err) {
console.error(`Error: ${err}`);
return;
}
console.log(`Public IP Address:\n${stdout}`);
console.error(`stderr: ${stderr}`);
});
通过 curl 命令,可以获取系统的公共 IP 地址。
检查系统中的软件包更新
const { exec } = require('child_process');
exec('apt list --upgradable', (err, stdout, stderr) => {
if (err) {
console.error(`Error: ${err}`);
return;
}
console.log(`Upgradable Packages:\n${stdout}`);
console.error(`stderr: ${stderr}`);
});
上述代码用于检查系统中有哪些软件包可以进行升级。
以上就是关于在 Node.js 中执行 Shell 命令的代码分享。如需了解更多 Node.js 执行 Shell 命令的相关内容,欢迎搜索本站历史文章或继续阅读后续内容,感谢大家的支持!