Server Fault is a question and answer site for system and network administrators. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I'm retrieving an xml file from an offsite third-party server and copying it to my local server. Problem is, once in a while the offsite server is down for maintance and during this time I don't want the crontab command to overwrite my local file. Is there a way I can check to make sure the file exists first, before copying?

Current crontab

0 * * * * username wget -O /home/www/inc/xml/wufoo.xml
share|improve this question

You could download to a different file and overwrite wufoo.xml only if wget's exit code is OK:

wget -O /home/www/inc/xml/ https://url/entries.xml && 
mv /home/www/inc/xml/ /home/www/inc/xml/wufoo.xml 

You can move this to a separate script for clarity and invoke that script from your crontab.

Edit: or try curl, which doesn't seem to truncate the output file by default: curl -o /home/www/inc/xml/wufoo.xml https://url/entries.xml

share|improve this answer
Good answer. If the server at the other end is not behaving properly, it might return a success status code, but a bogus file. If you split the logic out into another script (as you suggest), you could perform further sanity checks: is the resulting file a sensible length (starting with 'not 0 bytes' and perhaps moving onto something more sophisticated like 'not more than N bytes different to the original'), is the new file's contents of the right format, etc. – jmtd May 20 '11 at 12:55

This should only download the file if it doesn't exist.

if [ ! -f /home/www/inc/xml/wufoo.xml ]; then wget -O /home/www/inc/xml/wufoo.xml; fi
share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

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