首頁>Program>source

意圖:使用指定的引數呼叫外部應用程式,然後退出指令碼。

以下指令碼無法正常工作:

#!/usr/bin/node
 var cp = require('child_process');
 var MANFILE='ALengthyNodeManual.pdf';
 cp.spawn('gnome-open', ['\''+MANFILE+'\''], {detached: true});

尝試過的事情: exec -不分离.預先非常感谢

最新回復
  • 11天前
    1 #

    来自node.js文件:

    By default, the parent will wait for the detached child to exit. To prevent the parent from waiting for a given child, use the child.unref() method, and the parent's event loop will not include the child in its reference count.

    When using the detached option to start a long-running process, the process will not stay running in the background unless it is provided with a stdio configuration that is not connected to the parent. If the parent's stdio is inherited, the child will remain attached to the controlling terminal.

    您需要修改代碼,如下所示:

    #!/usr/bin/node
    var fs = require('fs');
    var out = fs.openSync('./out.log', 'a');
    var err = fs.openSync('./out.log', 'a');
    var cp = require('child_process');
    var MANFILE='ALengthyNodeManual.pdf';
    var child = cp.spawn('gnome-open', [MANFILE], { detached: true, stdio: [ 'ignore', out, err ] });
    child.unref();
    

  • 11天前
    2 #

    我對這个問题的解決方案:

    app.js

    require('./spawn.js')('node worker.js');
    

    spawn.js

    module.exports = function( command ) {
        require('child_process').fork('./spawner.js', [command]); 
    };
    

    spawner.js

    require('child_process').exec(
        'start cmd.exe @cmd /k "' + process.argv[2] + '"', 
        function(){}
    );
    process.abort(0);
    

  • android:如何以程式設計方式將selectableItemBackground添加到ImageButton?
  • asp.net mvc:如何在ASP MVC中自定義HtmlValidationMessageFor