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
Vault 3 Security EnhancementsOctober 24, 2023
Vault 3 is now available for Apple OSX M2 Mac Computers!September 18, 2023
Vault (for Desktop) Version 0.77 ReleasedMarch 26, 2023
EBTCalc (Android) Version 1.44 is now availableOctober 12, 2021
Vault (Desktop) Version 0.72 ReleasedOctober 6, 2021
EBT Compass is Now Available for Android DevicesJune 2, 2021
Convert a Windows 10 Notebook into a High-Capacity Photo FrameApril 3, 2021