FTP script timeout settings

Source: Internet
Author: User
Tags ftp connection

Today, I learned a little about shell script programming and wrote an FTP script. If no directory is set, I set the Directory and upload the file to the second-level directory on the server. As follows:

#!/bin/shcp $1 $2ftp -n $3 <<EOFquote USER $4quote PASS $5mkdir $6cd $6mkdir $7cd $7binaryput $2 $1quitEOF

$1, $2, $3, $4, $5, $6, and $7 are input parameters, indicates the files in use, the backup files to be uploaded, the remote host IP address, the FTP server user name, the FTP server password, the FTP server level-1 directory, and the FTP server level-2 directory.

The C program code that calls the script is:

Pid_t PID; If (pid = fork () <0) perror ("fork error"); else if (pid = 0) {If (execl ("/root/projects/ftp_test/upload", "Upload", "a.txt", "bk_a.txt", "192.168.16.150", "root", "root ", "record", "0001", null) <0) perror ("execl error");} waitpid (PID, null, 0); // call the sub-process in blocking mode, until FTP returns the result

The problem is that the program does not know whether FTP is successfully executed. If not, waitpid (PID, null, 0) takes about three minutes to know the result. I checked a lot of FTP information on the Internet, and there is no way to set the timeout time on the client. Later, I thought of using the program timing method and the non-blocking wait method to call waitpid. The modified code is as follows:

Pid_t PID, pidrtn; struct timeval tvstart, tvend; float timeuse = 0; gettimeofday (& tvstart, null); If (pid = fork () <0) perror ("fork error"); else if (pid = 0) {If (execl ("/root/projects/ftp_test/upload", "Upload", "a.txt ", "bk_a.txt", "192.168.16.150", "root", "root", "record", "0001", null) <0) perror ("execl error ");} // call the FTP script. For the wnohang option of waitpid, see waitpid. c While (timeuse <10) {pidrtn = waitpid (PID, null, wnohang); If (pidrtn = 0) {// printf ("the child process has not exited \ n"); sleep (1);} else break; gettimeofday (& tvend, null ); timeuse = 1000000 * (tvend. TV _sec-tvstart. TV _sec) + tvend. TV _usec-tvstart. TV _usec; timeuse/= 1000000;} If (pidrtn = 0) {char cmd [200]; // kill the child process, in the parent process, fork returns the child process PID sprintf (CMD, "Kill % d", pid); System (CMD ); // still unable to close the ongoing FTP connection attempt printf ("ftp-connect timeout \ n");} else printf ("pidrtn: % d", pidrtn );

This ensures that the FTP timeout can also be known to the program. You can also set the timeout time by yourself, and you can execute subsequent programs without waiting for FTP timeout.

But there are still some imperfections. In most cases, the programmer knows how much time it takes to upload and sets a reasonable timeout time. However, there may still be a sudden large file size, and beyond the time-out period, the actual upload is successful after the time-out period, and the program shows a time-out. Therefore, you can dynamically set the timeout time based on the size of the file to be uploaded. Of course, the best thing is to immediately disable FTP execution after the set timeout time is exceeded. This is not enough to close the sub-process. Currently, no good method is found.

Related Article

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.