R
R
Roman Andreevich2019-11-18 13:00:22
laptops
Roman Andreevich, 2019-11-18 13:00:22

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, () => {});

The task is simple, on command, start and stop the execution of a third-party program. At startup, I can’t catch the program start event, but that’s okay, and through request you can track the correctness of the program launch. But stopping is not very good, this.process.pid seems to return pid for example 23145, but if you do netstat -tpln, then this program has pid 23147, and accordingly: it does not work. If anyone has done this please tell me what I'm doing wrong? Thank you in advance
exec(`kill ${this.process.pid}`, () => {});

Answer the question

In order to leave comments, you need to log in

6 answer(s)
R
res2001, 2018-11-09
@res2001

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.

V
Vladimir Skibin, 2019-11-18
@megafax

Use spawn
https://nodejs.org/api/child_process.html#child_pr...

T
Timokins, 2019-11-18
@timokins

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.

R
Roman Andreevich, 2019-11-19
@RomanDillerNsk

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);

with and without shell the same result. I've tried all possible options, maybe I'm doing something wrong.

N
Nikolay Alekseev, 2021-01-20
@VariusRain

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 question

Ask a Question

731 491 924 answers to any question