2

I want calculate and display the free space of the home filesystem but there are 3-4 users, and all should be in javascript, how can we do ?

I know in linux shell , we can do :

df -h

But in javascript it's not

  • You cannot access the file system from JavaScript. You'll have to check it from PHP. – durbnpoisn Aug 7 '14 at 15:13
  • 1
    That's not the point of javascript ... It's mainly used for DOM manipulation not system commands ... why do you want to achieve this ? What if your client runs windows ? – Ko2r Aug 7 '14 at 15:13
  • Because for my friends , I have create a VPS, and in a manager, they want to see the free space in their home . It's only on my server ( debian ) – Kévin Bobo Aug 7 '14 at 15:19
3

Node diskusage will do this.

Copying their example code:

#!/usr/bin/env node
var disk = require('diskusage');

// get disk usage. Takes mount point as first parameter
disk.check('/', function(err, info) {
    console.log(info.free);
    console.log(info.total);
});

You'll need to install node (if you don't already have it) and fetch the node-diskfree package from npm of course.

Edit: switched to a cross platform package, that runs on all OSs and doesn't scrape command line tools.

| improve this answer | |
  • Ok, thanks, but where the free space are they display ? In my html page ? – Kévin Bobo Aug 7 '14 at 15:22
  • @KévinBobo In a command line terminal. JS in web pages normally doesn't get access to your full disk, only part of it (about 50MB right now). This is for security reasons. You have to install native programs (eg, like node and this script) to access your full hard disk. – mikemaccana Aug 7 '14 at 15:24
  • Yes, but the best is the resulat display on my webUI :/ because my friends aren't access to the terminal ( for security ) – Kévin Bobo Aug 7 '14 at 15:27
  • You could still show the output - eg, run the script from cron on your friends machines, and HTTP POST the results as JSON to a web server. But your friends will have to install the script themselves (or you could log in to their Linux machine via SSH and do it for them). – mikemaccana Aug 7 '14 at 15:30
1

What JavaScript environment are you using? NodeJS has a childprocess module that you can use to spawn a df command, see http://nodejs.org/api/child_process.html for more details.

I imagine that you're not attempting this in a browser based JavaScript sandbox.

| improve this answer | |
  • I use jquery too . I need to install NodeJS ? – Kévin Bobo Aug 7 '14 at 15:18
  • Yeah, or some sort of server side language to return the value and server it up as a value or object to your jQuery. JavaScript runs in a browser sandbox and doesn't have access to the resources of either the client or server natively, you'd have to make it aware of the information by using some other means. As another poster suggested Node DiskFree might be suitable. The advantage to node is that if you're already using JavaScript it does mean you don't mentally context switch from multiple languages, that and its modules. – Neil Munro Aug 7 '14 at 15:23
  • And, can you show me the childprocess for the df command ? How that work ? – Kévin Bobo Aug 7 '14 at 15:29
  • Using the docs here: Node I'd do it the way their example is. var spawn = require('child_process').spawn; var df = spawn('df', ['-h']); df.stdout.on('data', function (data) { console.log('stdout: ' + data); }); df.stderr.on('data', function (data) { console.log('stderr: ' + data); }); df.on('close', function (code) { console.log('child process exited with code ' + code); }); Have you used NodeJS before though, do you know how to set it up? – Neil Munro Aug 7 '14 at 15:42
0

shameless plug - https://www.npmjs.com/package/microstats

Can also be configured to alert the user when disk space crosses user defined threshold. works for linux, macOS and windows.

| improve this answer | |
0

You can either use diskusage npm and child_process.exec('df / -h') to get this parameter. but diskusage npm is more reliable and easy to use. if you use cp.exec('...') you should process the returned string by yourself to retrieve the desired parameters.

| improve this answer | |
0

You can do it by running the command df -h > df.txt which writes the df -h output to a file named df.txt. Then you could read the file and match a regex to it.

const { exec } = require('child_process');
const { readFileSync, unlinkSync } = require('fs')

var space;

exec('df -h > df.txt');

space = readFileSync('df.txt', (data, err) => {
  if (err) {
    throw err
  }
}).toString().match(/[0-9]+\.[0-9]+?../)[0]

console.log(space)
| improve this answer | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.