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 https://AAAA-BBBB-CCCC-DDDD:x@myaccount.wufoo.com/api/v3/reports/123456/entries.xml
link|improve this question
feedback

2 Answers

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/wufoo.xml.new https://url/entries.xml && 
mv /home/www/inc/xml/wufoo.xml.new /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

link|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
feedback

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 https://AAAA-BBBB-CCCC-DDDD:x@myaccount.wufoo.com/api/v3/reports/123456/entries.xml; fi
link|improve this answer
feedback

Your Answer

 
or
required, but never shown

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