我在 a.exe中使用spawn打开了b.exe并退出自己,并在b.exe中试图删除a.exe。 node文档地址:https://nodejs.org/dist/latest-v5.x/docs/api/child_process.html#child_process_child_process_spawn_command_args_options. 我在nodejs官方文档中看到:
在windows环境下,如果配置了detached ,当父进程退出后,子进程将以一个新的窗口运行。 默认情况,父进程将等待分离的子进程退出。为避免父进程等待给定的子进程,使用child.unref()方法,然后父进程的事件循环将不再包括对子进程的引用计数。
我把他们理解为:如果配置了options.detached参数,则父进程退出,子进程自动分离,使用child.unref()方法之后可以进一步的分离父子进程。之后,子进程将脱离于父进程。 代码如下。 a.exe function run(path, args, options){ var opts = { detached: true }; for(var key in options){ opts[key] = options[key]; } var sp = spawn(path, args, opts); sp.unref(); return sp; } run('C:\Users\zhaojm\Desktop\dd2\a.exe',null); process.exit();
b.exe del(to, {force: true}, function(err){ console.log(err); alert(err); });