简体   繁体   中英

Suppress output of ftp in a shell script while executed in a cron job

I'm running a backup shell script every night where i tar some data and then send it to a different server by sftp. As this is really important data i would like to get noticed by email if something goes wrong. Thats why i opted in to get notified if errors occur in the cron job (managed server).

This is how the sftp connection looks like:

sftp -i ~/.ssh/id_rsa server.com <<EOF
put $file
rm $file_old
EOF

Unfortunately right now i get a mail every time the script runs showing me the output of the sftp connection like this:

Connected to server.com.
% Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                               Dload  Upload   Total   Spent    Left  Speed
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0
  0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0

The script works and the files are transferred. So is there a way to hide the output of the sftp connection but yet show possible errors of the script. Any help is greatly appreciated!

The samples way to suppress those progress bars is to exec command as follow:

sftp -q -i ~/.ssh/id_rsa server.com <<EOF
put $file
rm $file_old
EOF

And this command will display errors on STDERR

You can redirect the output of the sftp:

sftp -i ~/.ssh/id_rsa server.com > /dev/null 2>&1 <<EOF

This way you will not see the output.

Then, if you want to see the possible errors, you can use:

sftp -i ~/.ssh/id_rsa server.com > /dev/null 2>&1 <<EOF
put $file
rm $file_old
EOF
sftp_status=$?
case $sftp_status in
    0) sftp_error="";;
    1) sftp_error="Error 1: Generic error - Undetermined error in file copy";;
    2) sftp_error="Error 2: Remote host connection failure";;
    3) sftp_error="Error 3: Destination is not directory, but it should be";;
    4) sftp_error="Error 4: Connecting to host failed";;
    5) sftp_error="Error 5: Connection lost for some reason";;
    6) sftp_error="Error 6: File does not exist";;
    7) sftp_error="Error 7: No permission to access file";;
    8) sftp_error="Error 8: Undetermined error from sshfilexfer";;
    9) sftp_error="Error 9: File transfer protocol mismatch";;
    65) sftp_error="Error 65: Host not allowed to connect";;
    66) sftp_error="Error 66: Protocol error";;
    67) sftp_error="Error 67: Key exchange failed";;
    68) sftp_error="Error 68: Host authentication failed";;
    69) sftp_error="Error 69: MAC error";;
    70) sftp_error="Error 70: Compression error (not used in SSH2)";;
    71) sftp_error="Error 71: Service not available";;
    72) sftp_error="Error 72: Protocol version not supported";;
    73) sftp_error="Error 73: Host key not verifiable";;
    74) sftp_error="Error 74: Connection lost";;
    75) sftp_error="Error 75: Disconnected by application";;
    76) sftp_error="Error 76: Too many connections";;
    77) sftp_error="Error 77: Cancelled by user";;
    78) sftp_error="Error 78: No more auth methods available";;
    79) sftp_error="Error 79: Illegal user name";;
    255) sftp_error="Error 255: Error occurred in SSH";;
    *) sftp_error="Unknown sftp Error";;
esac
echo $sftp_error

The Status Codes are based on this list: https://support2.microfocus.com/techdocs/2487.html

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM