4

I am having a bit of an issue.

I have a file, hosted on a remote server (http://mywebsite/file.zip). I also have a few embedded linux boxes (running openelec OS). The boxes have fairly limited commands, but they still have the basic - curl, bash, etc. They all have the file stored on them (/storage/file.zip)

What I am trying to do is - I need to set up a script, which executes in the first minute after the device is fully booted, and it will probably use curl to check if the remote server file (mywebsite/file.zip) is newer than the local one (/storage/file.zip), and instead of downloading it if it is newer - it needs to execute a bash script (/storage/scripts/script.sh)

I generally use this command "curl -o /storage/file.zip -z /storage/file.zip http://website/file.zip" but I have no idea how to make it execute the script, instead of download the file. Not even sure if it is possible at all.

All help is greatly appreciated!

Also, just to make sure - it needs to execute only if localfile is OLDER than remotefile. If localfile is newer than remotefile - it does not need to execute the script, as the script that is executed is also downloading the remote file from server, hence after it is executed - localfile will be with a newer timestamp, which if not specified that the script is executed only on remotefile newer - it might end in a endless loop.

  • Is there a version number in the file or in the file name? (might be ugly, but it it the first thing I thought off, combined with storing and old copy/version and comparing these). – Hennes Mar 5 '16 at 22:40
  • Unfortunately there are not. And adding a number or a version to the file would also mean rewriting around 200 other scripts, which is unfortunately not an option at the moment. I believe -z check for modified date, and it has been working good for upgrading the files, however I want to just use the file difference as a reference on whether it should execute the bash script or not. – Stoyan Mihov Mar 5 '16 at 22:46
5

Instead of looking at file name, you could trust your HTTP server to tell you when was the last time the file was changed and act accordingly.

#!/bin/bash

remote_file="http://mywebsite/file.zip"
local_file="/storage/file.zip"

modified=$(curl --silent --head $remote_file | \
             awk '/^Last-Modified/{print $0}' | \
             sed 's/^Last-Modified: //')
remote_ctime=$(date --date="$modified" +%s)
local_ctime=$(stat -c %z "$local_file")
local_ctime=$(date --date="$local_ctime" +%s)

[ $local_ctime -lt $remote_ctime ] && /storage/scripts/script.sh

# end of file.
  • Hey there, as much as this sounds great - whenever I execute it I get an error - stat: invalid option -- 'c' Apparently stat on openElec is very limited. Is there another option that I can use? Thanks! – Stoyan Mihov Mar 6 '16 at 13:53
  • I did a little change, changed the first local_ctime=$(stat -c %z "$local_file") to local_ctime=$(curl --silent --head file://$local_file | awk -F: '/^Last-Modified/ { print $2 }') But now I get this when I run the script: date: invalid date ' Sun, 06 Mar 2016 14' date: invalid date ' Fri, 04 Mar 2016 23'. It executes script.sh even if the local file is NEWER than the remote file. And since a part of the script is to download the file eventually - that will add the different timestamp, newer than server, and it will end up in a loop, since localfile will almost always be newer – Stoyan Mihov Mar 6 '16 at 14:43
  • Running curl against a local file makes no sense. Just stat it. – Sami Laine Mar 6 '16 at 15:13
  • Since OpenELEC does not have stat -c - I had to go a different route. However your answer works on right about every other linux machine I own, so I will pick it as the answer. Thank you so much! I decided to compare by size, and if size is same - close. If size of local and remote files is different - it executes a script. Here is what worked for me '#!/bin/bash Local=$(wc -c < file.zip) Remote=$(curl -sI server/file.zip | awk '/Content-Length/ {sub("\r",""); print $2}') if [ $Local != $Remote ]; then /bin/bash /storage/scripts/script.sh else echo "Same size." fi ' – Stoyan Mihov Mar 6 '16 at 16:33
0

Other answer was great for regular linux, but did not work for OpenELEC. So I decided to compare by size instead, and it works like a charm! Here is the code:

#!/bin/bash
Local=$(wc -c < file.zip)
Remote=$(curl -sI http://server/file.zip | awk '/Content-Length/ {sub("\r",""); print $2}')
if [ $Local != $Remote ]; then
/bin/bash /storage/scripts/script.sh
else
echo "Same size."
fi

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.