I am trying to run a python command from Node.js v12.22.1 in a web application. This is the method that is being called in one of my controllers:
async start ({request,response,params}) {
Log.create(request.request.headers.user,request.url())
const { spawn } = require('child_process');
const command = spawn('python',[process.env.BUS_PATH+'inventario/SystemDManager.py', params.name, "start"]);
var responseData = "teste";
command.stdout.setEncoding('utf-8');
command.stdout.on('data', function (data){
responseData += data.toString();
});
command.stderr.on('data', function (data){
responseData += data.toString();
});
command.stdout.on('end',function(data){
console.log(responseData);
});
return responseData
}
Variables are correctly instantiated, I have already printed them:
process.env.BUS_PATH = /home/ubuntu/
params.name = witty
So that the request command is:
python /home/ubuntu/inventario/SystemDManager.py witty start
This command runs perfectly when I type it directly into the shell console. However, nothing happens when running it through spawn. Moreover, when I change spawn arguments to:
spawn('cp',[process.env.BUS_PATH+'inventario/SystemDManager.py', "dummy.txt"]);
It works fine, it makes the file copy … Note that my responseData variable ends with “teste”, as if nothing had happened. I have even tried to change “python” to “/usr/bin/python”, but everything remains the same.
Does anyone has a tip of what is going wrong that python command is no being executed?