[英]Using sudo command in a child process using node
我正在嘗試將bundle目錄復制到遠程服務器的根目錄中。 我嘗試使用節點執行此操作,到目前為止,我實現了將tar內容傳遞給服務器並解壓縮它。 但是,當我嘗試將目錄移動到根文件夾時,它需要sudo
訪問權限,而我卻找不到辦法。 我試過-t
選項用於偽終端,但我想這可以從shell運行。 這是我到目前為止所做的,任何幫助都非常感謝:
const path = require("path");
const exec = require('child_process').exec;
var absolutePath = path.resolve(__dirname, "../");
const allCommands = [];
/*
*
*
* 1-) cd to the root folder of the app
* 2-) tar dist folder and pipe the result to the ssh connection
* 3-) connect to server with ssh
* 4-) try to create dist and old_dists folder, if not existing they will be created otherwise they will give an error and rest of the script will continue running
* 5-) cp contents of dist folder to old_dists/dist_$(dateofmoment) folder so if something is wrong somehow you have an backup of the existing config
* 6-) untar the piped tar content into dist folder, untar only files under the first parent directory --strip-components=1 flag, if it was 2 it will dive 2 level from the root folder
*
*
*/
allCommands.push("cd " + absolutePath);
allCommands.push("tar -czvP dist | ssh hostnameofmyserver 'mkdir dist ; mkdir old_dists; cp -R dist/ old_dists/dist_$(date +%Y%m%d_%H%M%S) && tar -xzvP -C dist --strip-components=1'");
//I would like to untar the incoming file into /etc/myapp for example rather than my home directory, this requires sudo and don't know how to handle it
exec(allCommands.join(" && "),
(error, stdout, stderr) => {
console.log(`stdout: ${stdout}`);
console.log(`stderr: ${stderr}`);
if (error !== null) {
console.log(`exec error: ${error}`);
}
});
還有什么是在ubuntu服務器中存儲web應用程序文件夾的最佳位置,其中多個用戶可以部署應用程序,這是一個很好的做法,使目錄root
用戶的所有者,或者它無關緊要?
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.