Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

using wget to overwrite file but use temporary filename until full file is received, then rename

I'm using wget in a cron job to fetch a .jpg file into a web server folder once per minute (with same filename each time, overwriting). This folder is "live" in that the web server also serves that image from there. However if someone web-browses to that page during the time the image is being fetched, it is considered a jpg with errors and says so in the browser. So what I need to do is, similar to when Firefox is downloading a file, wget should write to a temporary file, either in /var or in the destination folder but with a temporary name, until it has the whole thing, then rename in an atomic (or at least negligible-duration) step.

I've read the wget man page and there doesn't seem to be a command line option for this. Have I missed it? Or do I need to do two commands in my cron job, a wget and a move?

like image 978
Martin Moops Avatar asked Oct 24 '13 18:10

Martin Moops


1 Answers

There is no way to do this purely with GNU Wget.

wget's job is to download files and it does that. A simple one line script can achieve what you're looking for:

$ wget -O myfile.jpg.tmp example.com/myfile.jpg && mv myfile.jpg{.tmp,}

Since mv is atomic, atleast on Linux, you get the atomic update of a ready file.

like image 109
darnir Avatar answered Oct 23 '22 02:10

darnir