簡體   English   中英

如何使用javascript child_process.execFile從文件中獲取退出代碼

[英]How to get the exit code from a file ran using javascript child_process.execFile

這是我的python代碼:

#!/bin/python
import sys
sys.exit(4)

這是我的javascript

var exec = require('child_process').execFile
exec('./script.py', (err, data) => { if(err) { console.log(err.status) } })

但這是行不通的,因為沒有err.status這樣的東西,我想要在控制台日志中顯示的是“ 4”。

沒有err.status ,但是有err.code

這應該工作:

var exec = require('child_process').execFile
exec('./script.py', (err, data) => { if(err) { console.log(err.code) } })

還有err.signalerr.killed

從nodejs文檔:

成功后,錯誤將為null。 出現錯誤時,error將是Error的一個實例。 error.code屬性將是子進程的退出代碼,而error.signal將設置為終止進程的信號。 除0以外的任何退出代碼均被視為錯誤。

https://nodejs.org/api/child_process.html#child_process_child_process_execfile_file_args_options_callback

execFile的回調獲取3個參數:

  • error ,如果有的話。
  • stdout ,進程的標准輸出。
  • stderr ,進程的標准錯誤。

因此,通過檢查stderr ,您應該能夠達到預期的結果:

var exec = require('child_process').execFile
exec('./script.py',(err,stdout,stderr)=>{console.log(stderr)})

請參閱Node.js文檔

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM