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.
Related
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' ]);
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);
I'm trying to launch a process such the same way that I do in OS X with /usr/bin/open like this: open -a /Applications/Firefox.app --args -profile "blah blah" -no-remote.
As I learned from this topic here: launchd from terminal to start app.
However Linux doesn't have this open as I thought it did. I verified this with my searching. But in my searching I couldn't find an alternative. How can I launch process so that the launching process doesn't share its file descriptors with the launched process as explained in this SO topic: Close all File Handles when Calling posix_spawn
This is a video showing my desktop files. I'm trying to launch them somehow so that the file descriptors don't mix between each other here is my screen cast: https://www.youtube.com/watch?v=Yc19BzLTnDE
This video shows the PIDs are mixing: https://www.youtube.com/watch?v=YJsyV6tK7FA
Use xgd-open.
xdg-open is a desktop-independent tool for configuring the default applications of a user.
You can launch X11 applications in Linux simply by running the binary, so the open command is unnecessary for this use. (Another use of open would be to launch documents with the associated application, for which you can use either a desktop-manager-specific command or xdg-open.)
To avoid sharing file descriptors you can simply close them from the shell, e.g., in bash /usr/bin/x11/firefox 3>&- 4>&- … (up to 9) or if it's just the standard ones then perhaps you can redirect them: </dev/null >/dev/null 2>/dev/null. Or maybe you just want to use nohup to avoid closing the program on SIGHUP when the terminal closes.
Solution found that launches the .desktop file with the custom icon it used. I couldn't get xdg-open to work on i, no clue why.
https://askubuntu.com/questions/591736/any-c-functions-to-simulate-double-click-on-file/592439
this is my code:
var vlc = spawn("cvlc", [file], {uid:1000,gid:1000});
The program in node is executed through an instance of forever launched by root user. As cvlc does not permit to be executed as root I need it to be executed as normal user.
This is the way node.js explains how to do it: http://nodejs.org/api/child_process.html#child_process_child_process_spawn_command_args_options but does not work.
Maybe try
var vlc = spawn("su -c cvlc -s /bin/sh otheruser"....)
The command string is just a string, I have no idea if the spawn function will try to parse it or not, or straight up fail. I'm just trying to give you something to try, so give it a shot.
Let me explain better. What is gonna happen if I run a command in Linux and before it's done and you could enter another command I close the terminal. Would it still do the command or not?
Generally, you must expect that closing your terminal will hangup your command. But fear not! Linux has a solution for that too!
To ensure that your command completes, use the nohup argument first. Simply place it before whatever you are trying to do:
nohup ./some_program
nohup ./do_a_thing -frx -file input_file.txt
nohup grep "something" giant_list_of_files/* > temp_file.txt
The nohup command stands for "no hangup" and it will ensure that the command you execute continues to run, even if you close your terminal.
It depends on the process and your environment (job control shell options, VNC, etc). But typically, no. The process will get a "hangup" signal (message) from the operating system, and upon receiving that, will quit.
The nohup command, for example, arranges for processes to ignore the hangup signal from the OS. There are many ways to achieve the same result.
I would say it will abort att the status you are in just before the session close.
If you want to be sure to complete the job, you will need to use the nohup command.
http://en.wikipedia.org/wiki/Nohup
Read about nohups and daemons (-d)...
A good link is [link]What's the difference between nohup and a daemon?
Worth look at screen command, Screen command offers the ability to detach a long running process (or program, or shell-script) from a session and then attach it back at a later time.