Eric Bergman-Terrell's Blog

Node.js Programming Tip: Run Shell Commands and Capture Output
August 27, 2015

Node's child_process module allows an app to issue shell commands, and capture the output, including errors. I use child_process to determine the server's uptime, by issuing the "uptime" shell command.

The process uptime is available without running a shell command. The process.uptime() method returns the number of seconds that your app has been running.

If you use child_process in this way, please consider the security implications. After all, you don't want to allow bad guys to issue arbitrary shell commands on your server!

var childProcess = require('child_process');

function format(seconds){
    function pad(s){
        return (s < 10 ? '0' : '') + s;
    }

    var secondsPerDay = 86400.0;

    var days = Math.floor(seconds / secondsPerDay);

    seconds -= days * secondsPerDay;

    var hours = Math.floor(seconds / (60*60));
    var minutes = Math.floor(seconds % (60*60) / 60);
    var seconds = Math.floor(seconds % 60);

    return days + 'd ' + pad(hours) + ':' + pad(minutes) + ':' + pad(seconds);
}

function setupGet(app) {
    app.get('/admin/uptime', function(req, res) {
        childProcess.exec('uptime', function(error, stdout, stderr) {
            global.logger.info('uptime: ' + stdout);

            res.render('../views/admin/uptime', {
                title: 'Uptime',
                serverUptime: stdout,
                processUptime: format(process.uptime())
            })
        })
    })
}

exports.setup = function(app) {
    global.logger.info("exports controller setup");

    setupGet(app);
};

I use the following code snippet to redeploy this website, after I've copied updated files to the server. Note, the "&>" syntax ensures that errors and ordinary output are both logged, to the /logs/deploy.txt file.

        childProcess.exec('. /home/ericbtco/deploy.sh &> /home/ericbtco/logs/deploy.txt', function(error, stdout, stderr) {
            if (!error) {
                global.logger.info('app deployed');
            }
            else {
                global.logger.error('error during app deployment: ' + error);
            }

            global.logger.info('app deployment stdout: ' + stdout);
            global.logger.info('app deployment stderr: ' + stderr);
        });

uptime
EricBT.com: Proudly serving the Internet since almost 5 minutes ago!

Keywords: Node.js, Shell Commands, stdout, stderr, uptime, Linux, Express, server uptime, process uptime, process.uptime(), stdout, stderr

Reader Comments

Comment on this Blog Post

Recent Posts

TitleDate
Java Programming Tip: SWT Photo Frame ProgramOctober 31, 2016
Vault 3 (Desktop) Version 1.63 ReleasedSeptember 9, 2016
"Compliance with Court Orders Act of 2016"April 9, 2016
Disable "Visual Voicemail" on Android / T-MobileJanuary 17, 2016
IPv6 HumorDecember 10, 2015
Java Programming Tip: Specify the JVM time zoneDecember 7, 2015
Node.js / Express Programming Tip: Detect and Fix Memory LeaksOctober 27, 2015