Shell-scripts,ftp Automated upload script, loop until upload is successful

Source: Internet
Author: User

Scripting Features:

The script is run by a timed scheduled task, and the specified file is uploaded periodically.

Before writing this script, sometimes the network is unstable, always upload failed. Write a loop to pass, but do not know how to get the error state of the FTP return, later Niang to the FTP can be run error log records written to the file, because this point from sharing. So now let's share the whole script and give you a clue.

By detecting this error day record and returning the status, go through the loop to upload the specified file and append the script to run the log records, so as to keep track of the script running.


#!/bin/bash#upload gonet log to ftp_server#by colin on 2015-07-23scripts_ run_log= '/var/log/cron_scripts_run.log ' echo  ' date +%f '   '%T ': '%N '  script : $0  will runing ... " >> ${scripts_run_log}gonetdir= '/data/logs/www/' ftp_log_dir= '/ Nginxlog/testnet ' ############################ #t_month = ' date +%y%m ' t_last_hour= ' date -d  ' 1  Hour ago '  +%h ' t_yesterday= ' date -d  "Yesterday"  +%d ' t_today= ' date +%d ' T= ' echo  $ (date +%k)  |sed  ' s/ //g ' #凌晨的时候, file directory is yesterday's if [  $T  -eq 0 ]; thentemp_gonetdir=${gonetdir}${t_month}/${t_yesterday}/elsetemp_gonetdir=${gonetdir}${t_month}/${t_today}/ Fiput_log_name= "Yjww_${t_last_hour}.log" ftp_err_dir= "/tmp/ftp_err/" [ -d ${ftp_err_dir} ] | |  mkdir -p ${ftp_err_dir}ftp_err_log= "${ftp_err_dir}ftp_temp_yjww_err.log" #FTP自动化上传函数send_gonet_ Log () {ftp -i -n  FTP Server IP address  21 2>${ftp_err_log} << _EOF_user  user name   password passivebinlcd $ {temp_gonetdir}cd  ${ftp_log_dir}put ${put_log_name}bye_eof_# Statistics the number of error logging lines before FTP run output log_count= ' cat  ${ftp_err_log}|wc -l ' [ ${log_count} -eq 0 ] && return 0  | |  return 1}########################## #调用FTP上传函数, returns the status value required for until # 120 seconds per sleep, total cycle three times Function run_send_ Gonet () {send_gonet_logif [ $? -eq 0 ];thenecho  "' Date +%F"   "%T": "%N"  send : ${put_log_name} to ftp_server was successfully. "  >> ${scripts_run_log}return 0elseecho  "' date +%f"   "%T": "%N '  send  : ${put_log_name} more than  $x  time. " &NBSP;&GT;&GT;&NBSP;${SCRIPTS_RUN_LOG}SLEEP&NBSP;120RETURN&NBSP;1FI} #循环3次, total 6 minutes x=1i=1until [  "$i"  -eq 0 ];d o[  $x  -gt 3 ] && {echo  "' date +%f"   "%T": "%N '  send : ${put_log_name} to ftp_server  was failed, please check ... " >> ${scripts_run_log}exit}run_send_goneti=" echo $? ' x= ' expr  $x  + 1 ' done# Delete temporary error log files generated by FTP [ -f ${ftp_err_log} ]  && rm ${ftp_err_log}echo  "' date +%f"   "%T": "%N '  script : $0  done. "  >> ${scripts_run_log}


This article is from the "Mountain Road 18 Bend" blog, please be sure to keep this source http://kongzi68.blog.51cto.com/1432619/1678236

Shell-scripts,ftp Automated upload script, loop until upload is successful

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.