当前位置: 代码网 > it编程>前端脚本>Node.js > Nodejs中执行的shell命令的代码分享

Nodejs中执行的shell命令的代码分享

2024年05月18日 Node.js 我要评论
虽然nodejs运行时提供了和os交互的诸多api命令,但是有些操作(例如:特定系统信息获取)还是使用shell命令更加方便一些,本文列举了一些宜在nodejs中执行的shell代码的例子。获取 cp

虽然nodejs运行时提供了和os交互的诸多api命令,但是有些操作(例如:特定系统信息获取)还是使用shell命令更加方便一些,本文列举了一些宜在nodejs中执行的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}`);
});

上述代码会检查系统中可升级的软件包列表。

到此这篇关于nodejs中执行的shell命令的代码分享的文章就介绍到这了,更多相关nodejs执行shell命令内容请搜索代码网以前的文章或继续浏览下面的相关文章希望大家以后多多支持代码网!

(0)

相关文章:

版权声明:本文内容由互联网用户贡献,该文观点仅代表作者本人。本站仅提供信息存储服务,不拥有所有权,不承担相关法律责任。 如发现本站有涉嫌抄袭侵权/违法违规的内容, 请发送邮件至 2386932994@qq.com 举报,一经查实将立刻删除。

发表评论

验证码:
Copyright © 2017-2025  代码网 保留所有权利. 粤ICP备2024248653号
站长QQ:2386932994 | 联系邮箱:2386932994@qq.com