Webdev сохраненки
13 subscribers
130 photos
17 videos
7 files
347 links
Tips and tricks и прочие полезности из области веб-разработки, а также репосты понравившихся материалов
加入频道
How to run a Windows cmd.exe command using Node.js and detach the process?


const { spawn } = require('child_process');

// Command to run (replace it with your actual command)
const command = 'cmd.exe';
const args = ['/c', 'your_command_here'];

// Spawn the process
const child = spawn(command, args, {
detached: true,
stdio: 'ignore', // This option redirects the stdio of the child process to /dev/null
});

// Unref the child process to allow the parent to exit independently
child.unref();


Replace 'your_command_here' with the actual command you want to run. The detached: true option ensures that the child process runs independently of the parent process. The stdio: 'ignore' option redirects the standard input/output/error of the child process to /dev/null (or equivalent on Windows), which is suitable for a detached process.

Keep in mind that detaching the process means that it will continue running even if the parent Node.js process exits. Ensure that your Node.js script handles any necessary cleanup or waits for the child process to complete if needed.

#coding
#node