[英]Update (download files from server) Raspberry Pi 3
I'm trying to download files from another server to RPI and run command on RPI server with exec command to update data on RPI. 我正在尝试将文件从另一台服务器下载到RPI,并使用exec命令在RPI服务器上运行命令以更新RPI上的数据。
My first though was to check if server has some new versions with xhr request and then to download them with xhr request to a file I want with use of RPI server but couldn't get it work. 我的第一个方法是检查服务器是否具有一些带有xhr请求的新版本,然后将它们与xhr请求一起下载到我想使用RPI服务器但无法正常工作的文件中。
So is there any way to download some files to RPI and then use them with exec on RPI server (sudo move or something like taht)? 那么,有什么方法可以将某些文件下载到RPI,然后在RPI服务器上与exec一起使用(sudo move或taht之类的东西)?
Found a way. 找到了办法。 I'm using node.js with child_process to execute command to RPI. 我正在使用带有child_process的node.js来执行RPI的命令。 The command I use to download something from a server is wget http://ipaddress:port/path_to_file/
我用来从服务器下载内容的命令是wget http://ipaddress:port/path_to_file/
If You want to put it in specific folder You have to open it with cd path_to_folder
如果要将其放在特定的文件夹中,则必须使用cd path_to_folder
打开它
Whole code: 整个代码:
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);
});
}
Function 功能
checkServerFile checkServerFile
checks if the file exists on the server 检查文件在服务器上是否存在
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.