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

StackOverflow https://stackoverflow.com/questions/19573790

  •  01-07-2022
  •  | 
  •  

Вопрос

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?

Это было полезно?

Решение

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.

Лицензировано под: CC-BY-SA с атрибуция
Не связан с StackOverflow
scroll top