Skip to content

Instantly share code, notes, and snippets.

@grieve54706
Last active August 14, 2023 05:48
Show Gist options
  • Save grieve54706/81e7c6c69b34d92f383ea151b6f28a94 to your computer and use it in GitHub Desktop.
Save grieve54706/81e7c6c69b34d92f383ea151b6f28a94 to your computer and use it in GitHub Desktop.

Linux watcher

Get the peak memory usage in the duration

How to install

// instal node 17
bash <(curl -sL https://gist.githubusercontent.com/grieve54706/81e7c6c69b34d92f383ea151b6f28a94/raw/e7edfb4d2ea514142ca045301e090270c3637457/upgrade-node.sh)

// reload env var
source ~/.bashrc

// run tool
npx https://gist.github.com/grieve54706/81e7c6c69b34d92f383ea151b6f28a94

// remove npx cache if need to use new version
cd ~/.npm/_npx/ && rm -rf *

How to use

TBD

#!/usr/bin/env node
import figlet from 'figlet';
import gradient from 'gradient-string';
import { confirm } from '@inquirer/prompts';
import shell from 'shelljs';
import schedule from 'node-schedule';
import chalk from 'chalk';
async function main() {
var banner = figlet.textSync('Linux Watcher');
console.log(gradient.mind.multiline(banner));
console.log('Welcome to use Linux Watcher')
let peakMemory = 0;
let job;
const wantToStart = await confirm({ message: 'Do you want to ' + chalk.green('START') + ' to watch memory usages of system?', default: true });
if (wantToStart) {
job = schedule.scheduleJob('0/1 * * * * ?', () => {
const { stdout } = shell.exec('cat /sys/fs/cgroup/memory/memory.usage_in_bytes', { silent: true })
const memory_usage = Number(stdout.trim());
if (memory_usage > peakMemory) {
peakMemory = memory_usage;
}
})
}
else {
return;
}
console.log('NOW, you can start your process to watch the peak memory usage of system, If your process is done, you can stop watcher to look at the results.');
const wantToLook = await confirm({ message: 'Do you want to ' + chalk.red('STOP') + ' watcher to look at the results?', default: true });
if (wantToLook) {
console.log('Peak of memory usage of system is: ' + bytesToMB(peakMemory));
}
job.cancel();
}
function bytesToMB(bytes) {
return `${parseFloat(bytes / (1024 ** 3)).toFixed(2)}Gi`
}
await main();
{
"name": "Linux-Watcher",
"version": "1.0.0",
"description": "",
"main": "main.js",
"bin": "./main.js",
"type": "module",
"scripts": {
"test": "echo \"Error: no test specified\" && exit 1"
},
"author": "Grieve",
"license": "ISC",
"dependencies": {
"@inquirer/prompts": "^3.0.0",
"chalk": "^5.3.0",
"figlet": "^1.6.0",
"gradient-string": "^2.0.2",
"node-schedule": "^2.1.1",
"shelljs": "^0.8.5"
}
}
#!/bin/bash
sudo apt-get remove nodejs
sudo apt-get remove npm
curl -o- https://raw.githubusercontent.com/nvm-sh/nvm/v0.39.2/install.sh | bash
chmod +x ~/.nvm/nvm.sh
source ~/.bashrc
export NVM_DIR="$HOME/.nvm"
[ -s "$NVM_DIR/nvm.sh" ] && \. "$NVM_DIR/nvm.sh"
nvm install 17
node -v
npm -v
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment