Introduction to ps-list
ps-list is a command-line tool that provides detailed information regarding the currently running processes on your system. It is particularly useful for system monitoring, process management, and debugging. By leveraging ps-list, developers and system administrators can efficiently gather vital information about processes and take appropriate actions.
Key APIs of ps-list
- psList(): Fetches the list of running processes.
- psList(options): Fetches the list of running processes with optional parameters.
Code Snippets
1. Basic Usage
const psList = require('ps-list'); psList().then(data => { console.log(data); });
2. Fetch Specific Process Details
const psList = require('ps-list'); // Optional parameters to fetch specific details const options = { filter: process => process.name.includes('node'), all: true }; psList(options).then(data => { console.log(data); });
3. Get Process Details by PID
const psList = require('ps-list'); psList().then(processes => { const processByPID = processes.find(p => p.pid === 12345); console.log(processByPID); });
App Example
Let’s create a Node.js application utilizing ps-list to monitor and log the CPU usage of Node.js processes:
const psList = require('ps-list'); const fs = require('fs'); const monitorProcesses = async () => { const processes = await psList(); const nodeProcesses = processes.filter(p => p.name.includes('node')); const logStream = fs.createWriteStream('process_log.txt', { flags: 'a' }); logStream.write(`Timestamp: ${new Date().toISOString()}\n`); nodeProcesses.forEach(p => { logStream.write(`PID: ${p.pid}, CPU: ${p.cpu}, Memory: ${p.memory}\n`); }); logStream.end(); }; setInterval(monitorProcesses, 60000); // Run every 60 seconds
This script will log the details of Node.js processes into a text file every minute, making it a simple yet effective monitoring tool.
Hash: 0c6e3c854100cfde4b4f186b2120c4bbe5ab61fdab6f312443daa50efe3155c2