在 Windows 上生成并分离 "pwsh.exe" 子进程无效
Spawn and detach a "pwsh.exe" child process on Windows was not working
const { spawn } = require('child_process')
const spawnOptions = { detached: true, stdio: 'ignore' }
const pwshProcess = spawn('pwsh.exe', [
'-nop', '-nol', '-c', 'Set-Content -Path "D:\outTest.txt" -Value "ok" -NoNewline'
], spawnOptions)
pwshProcess.unref()
process.exit()
文件 D:\outTest.txt
将不会创建。
如何使代码工作?
这是我在 Windows 上运行它的唯一方法,但生成的进程并未分离。我尝试操纵 pwsh.exe 的路径、进程的工作目录和不同的参数组合。
const { spawn } = require('child_process')
const pwshProcess = spawn('pwsh.exe', [
'-nop', '-nol', '-c', 'Set-Content -Path "./outTest.txt" -Value "ok" -NoNewline -Verbose;'
])
pwshProcess.stdout.on('data', (data) => {
console.log(data.toString());
});
pwshProcess.stderr.on('data', (data) => {
console.error(data.toString());
});
pwshProcess.on('exit', (code) => {
console.log(`Child exited with code ${code}`);
});
// pwshProcess.unref()
// process.exit()
生成分离的 pwsh
进程在 Linux(Ubuntu) 上按预期工作:
const { spawn } = require
('child_process')
const spawnOptions = { detached: true, stdio: 'ignore' }
const pwshProcess = spawn('pwsh', [
'-nop', '-nol', '-c', 'Set-Content -Path "outTest.txt" -Value "ok" -NoNewline -Verbose;'
], spawnOptions)
pwshProcess.unref()
process.exit()
经过几个小时的测试。我发现一个解决方案是使用 Windows Shell start
命令创建一个新的可分离进程。
const { spawn, exec } = require('child_process')
const startPwshWindow = 'pwsh -nop -nol -noe'
const startPwshHidden = '/b pwsh -nop -nol -c Set-Content -Path ./outTest.txt -Value ok -NoNewline'
// using '/b' switch to create "no" new window
spawn('start', [startPwshWindow], {shell: true}).on('exit', process.exit)
exec(`start ${startPwshWindow}`).on('exit', process.exit)
spawn('start', [startPwshHidden], {shell: true}).on('exit', process.exit)
exec(`start ${startPwshHidden}`).on('exit', process.exit)
const { spawn } = require('child_process')
const spawnOptions = { detached: true, stdio: 'ignore' }
const pwshProcess = spawn('pwsh.exe', [
'-nop', '-nol', '-c', 'Set-Content -Path "D:\outTest.txt" -Value "ok" -NoNewline'
], spawnOptions)
pwshProcess.unref()
process.exit()
文件 D:\outTest.txt
将不会创建。
如何使代码工作?
这是我在 Windows 上运行它的唯一方法,但生成的进程并未分离。我尝试操纵 pwsh.exe 的路径、进程的工作目录和不同的参数组合。
const { spawn } = require('child_process')
const pwshProcess = spawn('pwsh.exe', [
'-nop', '-nol', '-c', 'Set-Content -Path "./outTest.txt" -Value "ok" -NoNewline -Verbose;'
])
pwshProcess.stdout.on('data', (data) => {
console.log(data.toString());
});
pwshProcess.stderr.on('data', (data) => {
console.error(data.toString());
});
pwshProcess.on('exit', (code) => {
console.log(`Child exited with code ${code}`);
});
// pwshProcess.unref()
// process.exit()
生成分离的 pwsh
进程在 Linux(Ubuntu) 上按预期工作:
const { spawn } = require
('child_process')
const spawnOptions = { detached: true, stdio: 'ignore' }
const pwshProcess = spawn('pwsh', [
'-nop', '-nol', '-c', 'Set-Content -Path "outTest.txt" -Value "ok" -NoNewline -Verbose;'
], spawnOptions)
pwshProcess.unref()
process.exit()
经过几个小时的测试。我发现一个解决方案是使用 Windows Shell start
命令创建一个新的可分离进程。
const { spawn, exec } = require('child_process')
const startPwshWindow = 'pwsh -nop -nol -noe'
const startPwshHidden = '/b pwsh -nop -nol -c Set-Content -Path ./outTest.txt -Value ok -NoNewline'
// using '/b' switch to create "no" new window
spawn('start', [startPwshWindow], {shell: true}).on('exit', process.exit)
exec(`start ${startPwshWindow}`).on('exit', process.exit)
spawn('start', [startPwshHidden], {shell: true}).on('exit', process.exit)
exec(`start ${startPwshHidden}`).on('exit', process.exit)