shell script to download remote file and launch and other script write in PHP
I made a shell script that get a remote file with ftp protocol, if the file is well downloaded, it launch another script in php with curl.It's kind of working right now but i have a few questions to improve:
- Do the script is waiting the end of the download to execute the rest of the script ?Or during the time of download the script do the following instructions ?
- I receive the first mail of beginning instruction but never the last ones (the one that get the result of the curl, and the one at the end of the script) how come ?
- I would like to find a good way to disallow the script to be run more than once (if the archive has been downloaded) even if it's launch every hours with crontab ?
- what is the difference between quit/bye/by at the end of the ftp connection ?
This is the shell script:
echo start of the script | mail -s "beginning of the script" krifur@krifur.com
cd /my_rep
HOST='domaine.com'
PORT='21'
USER='admin'
PASSWD='pass'
jour=$(date "+%Y%m%d")
FILE="file_"$jour".txt";
ftp -i -n $HOST $PORT <<EOF
quote USER $USER
quote PASS $PASSWD
cd firstlevel
cd end
get $FILE
quit
EOF
if test -f $FILE
then
CurlRes="$(curl "http://doma.com/myfile.php")"
echo debug CURL : $CurlRes | mail -s "debug" krifur@krifur.com
else
echo no file : $FILE | mail -s "no file" krifur@krifur.com
fi
echo this is the end of the script download | mail -s "end of script download" 开发者_如何学编程krifur@krifur.com
Do the script is waiting the end of the download to execute the rest of the script ?Or during the time of download the script do the following instructions ?
If you mean "Will the FTP command block until finished?" , the answer is yes.
I receive the first mail of beginning instruction but never the last ones (the one that get the result of the curl, and the one at the end of the script) how come ?
Take a look at your code:
then
CurlRes="$(curl "http://doma.com/myfile.php")"
echo debug CURL : $CurlRes | mail -s "debug" krifur@krifur.com
else
echo no file : $FILE | mail -s "no file" krifur@krifur.com
fi
What are the contents of $CurlRes and $FILE respectively? Try ${CurlRes} and ${FILE}. I'd also suggest quoting strings when using echo
.
There is also a good chance that spam filters don't like the message, have you checked on that?
I would like to find a good way to disallow the script to be run more than once (if the archive has been downloaded) even if it's launch every hours with crontab ?
That could be done in a number of ways. Perhaps, upon success echo the file name to the bottom of something like successfully_downloaded.txt , then use grep to see if the file name is in the list. Depending on use, that file might get rather large .. so I'd also implement some means of rotating it.
From man (3) ftp
:
bye Terminate the FTP session with the remote server and exit ftp.
An end of file will also terminate the session and exit.
quit A synonym for bye.
by
is also a synonym for bye
, as far as I know.
This should be avoided at all costs:
USER='admin'
PASSWD='pass'
Use ssh's scp with keys (no password prompt required):
Linux Journal article howto
精彩评论