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); });
EricBT.com: Proudly serving the Internet since almost 5 minutes ago!
Title | Date |
Vault 3 Security Enhancements | October 24, 2023 |
Vault 3 is now available for Apple OSX M2 Mac Computers! | September 18, 2023 |
Vault (for Desktop) Version 0.77 Released | March 26, 2023 |
EBTCalc (Android) Version 1.44 is now available | October 12, 2021 |
Vault (Desktop) Version 0.72 Released | October 6, 2021 |
EBT Compass is Now Available for Android Devices | June 2, 2021 |
Convert a Windows 10 Notebook into a High-Capacity Photo Frame | April 3, 2021 |