Nodejs中执行的shell命令的代码分享
(福利推荐:你还在原价购买阿里云服务器?现在阿里云0.8折限时抢购活动来啦!4核8G企业云服务器仅2998元/3年,立即抢购>>>:9i0i.cn/aliyun)
虽然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命令内容请搜索程序员之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持程序员之家!
相关文章
Linux Centos7.2下安装nodejs&npm配置全局路径的教程
今天小编就为大家分享一篇Linux Centos7.2下安装nodejs&npm配置全局路径的教程,具有很好的参考价值,希望对大家有所帮助。一起跟随小编过来看看吧2018-05-05Nodejs中使用phantom将html转为pdf或图片格式的方法
这篇文章主要介绍了Nodejs中使用phantom将html转为pdf或图片格式的方法,需要的朋友可以参考下2017-09-09完美解决node.js中使用https请求报CERT_UNTRUSTED的问题
下面小编就为大家带来一篇完美解决node.js中使用https请求报CERT_UNTRUSTED的问题。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧2017-01-01
最新评论