Curl check if file is newer and instead of downloading – execute a bash (or python) script

bashcurlopenelec

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.

Best Answer

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.
Related Question