Answer the question
In order to leave comments, you need to log in
How to start and stop a .sh file through nodejs?
Colleagues good time of day, actually a question in a subject.
Here is the code:
const { exec, spawn } = require('child_process');
this.start_comand = `./testApp/headless.sh`
this.process = exec(this.start_comand, () => {});
exec(`kill ${this.process.pid}`, () => {});
Answer the question
In order to leave comments, you need to log in
From what you can do yourself - update the BIOS of the laptop.
It would be useful to voice the model of the laptop in the question.
Use spawn
https://nodejs.org/api/child_process.html#child_pr...
exec invokes your program via a shell invocation,
first it invokes a shell in which the entered command is already invoked, i.e. your program.
Accordingly, the returned PID does not belong to the program, but to the shell.
If you want to call a program directly without calling a shell, then use execFile.
----
Or use spawn as suggested above.
Comrades, good day, I tried all the options, but here's what happens, all the exec, execFile methods. spawn return PID 5029 for example, but if you print netstat -tpln port 5031 is obtained. Some kind of mysticism !!! Here is the code, maybe I'm dumb:
// this.process = exec(this.start_comand, () => {});
this.process = execFile('sh', ['./testApp/headless.sh', '- port 35000'], () => {});
// this.process = spawn('sh', ['./testApp/headless.sh', '- port 35000']);
console.log(this.process.pid);
A small necropost, but since no one has given an answer so far, I think that someone may find it useful.
Stopping a child process is done by the kill method of the process object itself in the code.
For example:
let ffmpegProcess = spawn("ffmpeg", args);
let stopRecording = function() {
ffmpegProcess.kill();
};
setTimeout(stopRecording, 20000);
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question