[英]Running a program in Windows Subsystem for Linux from Electron
我想知道是否可以從 Windows 上的 Electron 應用程序在 WSL 中運行程序。
情況是我有一個二進制文件,它只在我想在桌面 Electron 應用程序的后台執行的 *nix 系統上運行。 二進制文件本身可以作為 Electron package 的一部分包含在內,所以我知道它將可用。
我知道 WSL 並不總是安裝在 Windows 系統上,但在安裝的情況下,是否有可能以某種方式“調用它”來運行二進制文件?
我嘗試研究這個問題,但只能找到有關從 WSL 內部運行 Electron 本身的信息,這不是我要找的。
老實說,我對 Electron 不熟悉,但這應該不是問題。 WSL 本身可以作為標准 Windows .exe
啟動,然后可以指定Linux應用程序在命令行上運行。 結果返回到stdout
。
例如:
const { execFile} = require('child_process');
execFile('wsl.exe', ['--exec','ls','-la'], {}, (err, stdout, stderr) => {
console.log(err);
console.log(stdout);
console.log(stderr)
});
--exec
指定要運行的二進制文件。 這不是絕對必要的,但它避免了運行標准 shell(例如 Bash)然后讓 Bash 執行二進制文件的開銷。
ls
當然是二進制文件。 至於打包在 Electron 中的二進制文件的路徑,那是我有點迷路的地方。 那將是一個Windows路徑(盡管我不知道它在 Electron 應用程序中的確切捆綁位置。無論如何,您需要使用wslpath
命令將其轉換為等效的WSL /Linux 路徑。
例如,類似於上面的(並且沒有任何真正的錯誤處理):
const { execFile} = require('child_process'); execFile('wsl.exe', ['--exec','wslpath','C:\\Windows\\notepad.exe'], {}, (err, stdout, stderr) => { console.log(err); console.log(stdout); console.log(stderr) });
stdout
將包含/mnt/c/Windows/notepad.exe
。
請注意,如果您的 Linux 應用程序需要預先配置環境變量,則需要通過WSLENV
變量來完成。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.