简体   繁体   中英

NodeJS child spawn exits without even waiting for process to finish

I'm trying to create an Angular11 application that connects to the NodeJS API that would run bash scripts when called and on exit it should either send an error or send a 200 status with a confirmation message.

here is one of the functions from that API. It runs a script called initialize_event.sh , gives it a few arguments when prompted and once the program finishes its course should display a success message (There is no error block for this function):

exports.create_event = function (req, res) {
  var child = require("child_process").spawn;
  var spawned = child("sh", ["/home/ubuntu/master/initialize_event.sh"]);
  spawned.stdout.once("data", function (data) {
    spawned.stdin.write(req.body.name + "\n");
  });
  spawned.stdout.once("data", function (data) {
    spawned.stdin.write(req.body.domain_name + "\n");
  });
  spawned.on("exit", function (err) {
    res.status(200).send(JSON.stringify("Event created successfully"));
  });
};

The bash script is a long one, but what it basically does is take two variables (event name and domain name) and uses that to create a new event instance. Here are the first few lines of code for the program:

#!/bin/bash
#GET EVENT NAME
echo -n "Enter event name: "; read event;
echo -n "Enter event domain: "; read eventdomain;
#LOAD VARIABLES
export eventdomain;
export event;
export ename=$event-env;
export event_rds= someurl.com ;
export master_rds= otherurl.com;
export master_db=master;
# rest of code...

When called on its own directly from the terminal, the process takes around 30-40 seconds after taking input to create an event and then exits once completed. I can then check the list of events created using another script and the new event would show up in the list. However, when I call this script from the NodeJS function, it manages to take the inputs and the exit within 5 or 6 seconds, saying the event has been created successfully. When I check the list of events there is no event created. I wait to see if the process is still running and check back after a few minutes, still, no event created.

I suspect that the spawn exits before the script can be run completely. I thought that maybe the stdio streams are still open so I tried to use spawned.on.close instead of spawned.on.exit , but still the program exits before it even runs completely. I don't see any exceptions or errors appearing in the Node express console, so I can't really figure out why the program exits successfully without running all the way through. I've used the same inputs when running from the terminal and on Postman, and have logged them as well to see if there are any empty variables being sent, but found nothing wrong with them either. I've double-checked the paths as well, literally copy-pasted from pwd to make sure I haven't been missing something, but still nothing. What am I doing wrong here??

So here's the problem I found and solved:

The folder where the Node Express was being served from, and the folder where the bash scripts were saved were in different directories.

Problem:

So basically, whenever I created a child process, it was created with the following current directory:
var/www/html/node/

But the bash scripts were run from:
var/www/html/other/bash/scripts/

so any commands that were added to the bash script that involved directory change (like cd ) were relative to the bash directory.

However, since the spawn's current directory was var/www/html/node the script being executed in the spawn also had the same current working directory as the node folder, and any directory changes within the script were now invalid since they didn't exist relative to node directory.

Eg
When run from terminal:
test.sh -> cd /savedir/ -> /var/www/html/other/bash/scripts/savedir/ -> exists

When run from spawn:
test.sh -> cd /savedir/ -> /var/www/html/node/savedir/ -> Doesn't exist!

Solution:

The easiest way I was able to solve this was to modify the test.sh file. ie during the start I added cd /var/www/html/other/bash/scripts/ . This allowed the current directory of my spawn to change to the right directory that would make all the mv cd and other path relevant commands valid.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM