I'have a process created with a spawn in nodejs with the option shell:true so the process starts in a real shell. So when I try to kill this process with streamingTask.kill() it's not working. Without the option shell:true everything works fine.
This is how my code looks:
var options = {shell:true};
streamingTask = spawn('gst-launch-1.0',args,options);
...
streamingTask.kill()
So how can I kill this process now?
This doesn't work because you are killing the shell process itself, not the child process(es) spawned by the shell (gst-launch-1.0 in your case).
There's a package on npm called tree-kill that provides an easy one-line solution:
var kill = require('tree-kill');
kill(streamingTask.pid);
Related
let cp = require("child_process");
console.log("Opening Chrome");
cp.execSync("start chrome");
console.log("Chrome Opened");
I have opened the chome using above command but don't know how to close this using similar one.
If anyone of you know let me know!!
Well here in your program you are relying on some platform command-line tools, so you could try to achieve the same result with a similar platform-specific tool.
For example, there is kill -9 PID command that can kill any process, but you need to know the PID of your process.
I would advice you to use spawn command instead of execSync if you just want to spawn some program and have more controll over it.
Using CLI I try to close/send to background a process which loads at startup - the first thing I see is that process and not the regular shell.
Ctrl x/c/z don't work. Kill -9 does (connecting with ssh and send the signal)
If I kill the process (which loads at startup) and then execute it again , it will reapond to each signal.
I try to figure out what could be the problem
I can't post the program's source (related to workplace). Lets say it's a simple printf("hello world\n") , scanf and return. since this program autostart during kernel startup (with inittab script..) is it possible that it starts before the bash shell and thats why it can't recieve these signals?
I have this command in my shell script that runs forever- it wouldn't finish unless I do ctrl-c. I have been trying to look up how to send ctrl-c signal to script and all the answers have been some sort of kill $! or kill$$ or such. My problem is that the command never finishes, so it never goes on to the next command like my "kill" commands or anything else. I have to manually hit the ctrl-C in my terminal for it to even execute kill $!. I'm sure there is a way to work around this but I am not sure what. Thanks in advance!
There are several approaches to this problem. The simplest (but not most robust) is (perhaps) to simply run your long running command in the background:
#!/bin/sh
long-running-command & # run in the background
sleep 5 # sleep for a bit
kill %1 # send SIGTERM to the command if it's still running
For reference: https://nodejs.org/api/child_process.html#child_process_options_detached
Hey guys,
So I need to spawn a child child-process, spawn because exec doesn't allow a options.detached & child.unref();, meaning it can be decoupled with the parent allowing the child to run and finish on it's own, vice-versa also the parent (in our particular case, the parent process can die before a long-running child, update in that case, is finished without needing to wait for the child like with exec).
We have a long connected ("… ; … ; …") command that is built by the node (parent) app, but as like spawn("echo stuff >>stderr.log") doesn't work, only spawn('ls', [-l]), I obviously can't chain commands (as it's also referenced in he docu and multiple times on SO.
TLDR;
We need to use spawn, but spawn can't process chained shell commands.
Do I really now need to write my command in a bash and execute that then, is this REALLY the ONLY option??
THX
Notice the shell option for spawn:
If true, runs command inside of a shell. Uses '/bin/sh' on UNIX, and 'cmd.exe' on Windows. A different shell can be specified as a string. The shell should understand the -c switch on UNIX, or /d /s /c on Windows. Defaults to false (no shell).
So:
let child = child_process.spawn('foo; bar; blah', { shell : true });
EDIT: if you're using a Node version that doesn't support this option, here's an alternative:
let child = child_process.spawn('/bin/sh', [ '-c', 'foo; bar; blah' ]);
As far as I know, when you run a command, like
> sleep 3
The shell process will fork another process and run the command with the child process.
However when you do
> (sleep 3)
you launch a subshell and execute the command. Essentially what it does is also fork another process to execute the command and wait the command to complete.
In this case, the behavior of the two commands looks the same, the parent shell will wait the sleep command to complete.
However sometime I noticed things are different with subshell:
For example, if I run some command like:
> virtualbox &
If I accidentally close the terminal the virtualbox will close at the same time. I already screwed my ongoing work several time in this way.
However if I do it this way it the program won't be killed even if I exited the terminal:
> (virtualbox &)
So I am not sure what's going on under the hood? How are the tasks started and managed by the shell with the two different approach?
As others write, using nohup will allow you to run the process without it being terminated when your shell is terminated. What happens in the two cases you describe is the following.
In the virtualbox & case virtualbox becomes a child of your shell. When your controlling terminal is closed all processes associated with it receive a SIGHUP signal, and are terminated.
In the (virtualbox &) case the command is executed within a subshell. When the subshell terminates, the command is disassociated from the shell and the terminal. (You can see this by running ps.) In this case the SIGHUP will not be sent to virtualbox, and therefore your command will not be terminated when the controlling terminal is closed.
The nohup command achieves the same result by specifying that the SIGHUP signal must be ignored.