Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8430857
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T05:41:59+00:00 2026-06-10T05:41:59+00:00

Most server-side-scripting languages have an exec function ( node , php , ruby ,

  • 0

Most server-side-scripting languages have an exec function (node, php, ruby, etc). This allows the programming language to interact with the shell.

I wish to use exec() in node.js to run large processes, things I used to do with AJAX requests in the browser. I would like a simple progress / loading bar to display to the user the progress.

I found out the hard way in the example below that the callback / asynchronous nature of the exec function will make this example take over 5 seconds to load.

What I want is some way to get the content of browser to be updated (ajax) with the current state of the execution like a loading bar. But I don’t want the ran file to be dependent on the browser.

Any idea?

my route

exports.exec = function(req,res){
  // http://nodejs.org/api.html#_child_processes
  var sys = require('sys')
  var exec = require('child_process').exec;
  var child;
  child = exec("node exec/test.js", function (error, stdout, stderr) {
    var o = {
      "error":error,
      "stdout":stdout,
      "stderr":stderr,
    };
    o = JSON.stringify(o);
    res.send(o);
  });
};

my test.js file

var sys = require('sys');

var count = 0;

var interval = setInterval(
    function(){
        sys.print('hello'+count);
        count++

        if(count == 5){
            clearInterval(interval);
        }

    },
1000);
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T05:42:00+00:00Added an answer on June 10, 2026 at 5:42 am

    You should use socket.io for this.
    Here is a demo app to get you started with socket.io

    You emit an event for every 1% using socket.io, the browser listen to it and update a bar.

    You can’t use exec you need a streamed output.
    Therefore you’d rather use child_process.

    On the server.

    var spawn = require('child_process').spawn,
        exec  = spawn('node exec/test.js');
    
    exec.stdout.on('data', function (message) {
      socket.emit('process', message);
    });
    

    On the sub process:

    console.log('10%')
    // ...
    console.log('20%')
    // ...
    console.log('30%')
    

    If your sub process is a node script you could do something a lot more elegant.
    Rather than having to talk with a stream stdout you could use the cluster module of node to send messages between the master and the slaves process.

    I made a fork of the previous demo app adding a route /exec which demonstrate how to achieve this.

    When I’ll have more time I’ll make another demo app, this is a quite interesting and educational test. Thanks for the idea :D.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I understand that most of the languages support server side redirects (asp.net: Response.Redirect, PHP:
I'm about to write server side aplication(most probably it would be PHP but JAVA
I'll be implementing this for Node.js (server side Javascript), but this question is about
I found several answers and solutions, but most of it somehow integrated server-side helper.
I'm trying to implement a socket server that will run in most shared PHP
Most Java web application frameworks use server-side session objects to store session state. Because
Through the past few years I've been busy with server side programming and I
I'm familiar with web programming, both client and server side, and I'd like to
I've done a lot of server-side HTML programming and used a number of different
All the time, I use PHP on server side to generate HTML/XHTML directly or

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.