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
EBT Weather is now available for Windows and LinuxMay 30, 2026
Node.js + Express: How to Block Requests by User-Agent HeadersJanuary 7, 2026
Vault 3 is Now Available for Windows on ARM Machines!December 13, 2025
Vault 3: How to Include Outline Text in Exported PhotosOctober 26, 2025
.NET Public-Key (Asymmetric) Cryptography DemoJuly 20, 2025
Raspberry Pi 3B+ Photo FrameJune 17, 2025
EBTCalc (Android) Version 1.53 is now availableMay 19, 2024