更新(从服务器下载文件)Raspberry Pi 3
Update (download files from server) Raspberry Pi 3
我正在尝试将文件从另一台服务器下载到 RPI 并在 RPI 服务器上使用 运行 命令使用 exec 命令更新 RPI 上的数据。
我的第一个想法是检查服务器是否有一些带有 xhr 请求的新版本,然后使用 xhr 请求将它们下载到我想要使用 RPI 服务器但无法正常工作的文件。
那么有什么方法可以将一些文件下载到 RPI,然后在 RPI 服务器上通过 exec 使用它们(sudo move 或类似 taht 的东西)?
找到方法了。我正在使用 node.js 和 child_process 来执行 RPI 命令。我用来从服务器下载内容的命令是 wget http://ipaddress:port/path_to_file/
如果你想把它放在特定的文件夹中你必须用cd path_to_folder
打开它
完整代码:
var express = require("express");
var app = express();
var http = require("http").Server(app);
var io = require('socket.io')(http);
var exec = require('child_process').exec;
var request = require('request');
http.listen(6669);
io.on('connection', function(socket){
socket.on("Update",function(){
update();
}
}
//////////FUNCTIONS//////////
function update() {
try {
console.log("Start updating!");
if(checkServerFile('http://ip_address:port/path_to_file')){
console.log("Starting download...");
execute('cd /home/pi/server/update/ && sudo wget http://ip_address:port/update/update.sh && sudo bash /home/pi/server/update/update.sh');
}
}
catch (err) {
console.log(err);
}
}
function checkServerFile(path){
var result = false;
result = request(path, function(err, resp){
if(resp.statusCode === 200){
return true;
}
});
console.log(result);
return result;
}
function execute(command) {
var cmd = exec(command, function(error){
console.log("error: ", error);
});
}
函数
checkServerFile
检查文件是否存在于服务器上
我正在尝试将文件从另一台服务器下载到 RPI 并在 RPI 服务器上使用 运行 命令使用 exec 命令更新 RPI 上的数据。
我的第一个想法是检查服务器是否有一些带有 xhr 请求的新版本,然后使用 xhr 请求将它们下载到我想要使用 RPI 服务器但无法正常工作的文件。
那么有什么方法可以将一些文件下载到 RPI,然后在 RPI 服务器上通过 exec 使用它们(sudo move 或类似 taht 的东西)?
找到方法了。我正在使用 node.js 和 child_process 来执行 RPI 命令。我用来从服务器下载内容的命令是 wget http://ipaddress:port/path_to_file/
如果你想把它放在特定的文件夹中你必须用cd path_to_folder
完整代码:
var express = require("express");
var app = express();
var http = require("http").Server(app);
var io = require('socket.io')(http);
var exec = require('child_process').exec;
var request = require('request');
http.listen(6669);
io.on('connection', function(socket){
socket.on("Update",function(){
update();
}
}
//////////FUNCTIONS//////////
function update() {
try {
console.log("Start updating!");
if(checkServerFile('http://ip_address:port/path_to_file')){
console.log("Starting download...");
execute('cd /home/pi/server/update/ && sudo wget http://ip_address:port/update/update.sh && sudo bash /home/pi/server/update/update.sh');
}
}
catch (err) {
console.log(err);
}
}
function checkServerFile(path){
var result = false;
result = request(path, function(err, resp){
if(resp.statusCode === 200){
return true;
}
});
console.log(result);
return result;
}
function execute(command) {
var cmd = exec(command, function(error){
console.log("error: ", error);
});
}
函数
checkServerFile
检查文件是否存在于服务器上