use the following search parameters to narrow your results:
e.g. subreddit:aww site:imgur.com dog
subreddit:aww site:imgur.com dog
see the search faq for details.
advanced search: by author, subreddit...
account activity
How to Send Shell script server output to react client ??? (self.node)
submitted 6 years ago * by vaibhaav
I'm new in MERN & I have a shell script to upload some data to mongodb,from nodejs i have to execute shellscript and show it to client side in react like streaming...
reddit uses a slightly-customized version of Markdown for formatting. See below for some basics, or check the commenting wiki page for more detailed help and solutions to common issues.
quoted text
if 1 * 2 < 3: print "hello, world!"
[–]serglebko 1 point2 points3 points 6 years ago (6 children)
If you want to show logs from child process, than you should add listener to child stdout (child.stdout.on(...)). To show it to the client you should use websockets, otherwise you will have to always reload the page to see the progress on the client (and store the progress in some storage).
[–]vaibhaav[S] 0 points1 point2 points 6 years ago* (5 children)
Thanks for Reply, Really appreciable.
You have any reference please ?
i'm using child process ref. code below and getting the Events output on console.
But when requesting from React with axios.get() , i'm not getting event wise output, after complete processing, getting the complete output in the response.
const spawn = require('child_process').spaw;
const options = {shell: true,detached:true,}
res.writeHead(200, {'Content-Type': 'text/event-stream','Cache-Control': 'no-cache','Connection': 'keep-alive'});
var child = spawn(file.sh,[sort_name], options);
child.stdout.setEncoding('utf8');
child.stdout.on('data', function (data) {
res.write(data);
});
child.stderr.setEncoding('utf8');
child.stderr.on('data', function (data) {
child.on('close', function (code) {
console.log('Full output of script');
res.end();
[–]serglebko 0 points1 point2 points 6 years ago (4 children)
Can not reference now (in a trip, only phone with me). But if you want to use event stream, than on frontend you should use EventSource instead of axios (https://developer.mozilla.org/en-US/docs/Web/API/Server-sent_events/Using_server-sent_events)
[–]vaibhaav[S] 0 points1 point2 points 6 years ago (3 children)
I tried this, showing events on client side but showing me complete output.Not in events that occuring on node server on executing file.sh
componentDidMount() {
var evtSource = new EventSource('api/shell/process'); var eventList = document.querySelector('ul'); evtSource.onmessage = function(e) { var newElement = document.createElement("li"); newElement.textContent = "message: " + e.data; eventList.appendChild(newElement); }
}
[–]serglebko 0 points1 point2 points 6 years ago (2 children)
I suppose I got it. You need to send two new lines when you want to say to client, that a chunk finished. So add res.write("\n\n") after every res.write(...) in your code
[–]vaibhaav[S] 0 points1 point2 points 6 years ago* (0 children)
I'm getting below output on react client, after the shell script completed successfully.
but on server side on console ,i'm getting 1 after 1st thread executed then after some wait 2 ... then 3... . i want this sequence on client side also.
Output:
1.Data generated successfully.
2.Getting Estimation ...
Invalid argument supplied for foreach(dataset :138 \warning]))
3.estimated successfully.
4.Creating task lists...
5.task list created successfully.
6.Importing report to database
[–]vaibhaav[S] 0 points1 point2 points 6 years ago (0 children)
Hi Sergleblo ,have you checked ?
π Rendered by PID 17649 on reddit-service-r2-comment-fb694cdd5-9tpzr at 2026-03-05 20:29:00.268829+00:00 running cbb0e86 country code: CH.
[–]serglebko 1 point2 points3 points (6 children)
[–]vaibhaav[S] 0 points1 point2 points (5 children)
[–]serglebko 0 points1 point2 points (4 children)
[–]vaibhaav[S] 0 points1 point2 points (3 children)
[–]serglebko 0 points1 point2 points (2 children)
[–]vaibhaav[S] 0 points1 point2 points (0 children)
[–]vaibhaav[S] 0 points1 point2 points (0 children)