【问题标题】:Suppress output of ftp in a shell script while executed in a cron job在 cron 作业中执行时抑制 shell 脚本中的 ftp 输出
【发布时间】:2018-12-20 12:21:32
【问题描述】:

我每天晚上都在运行一个备份 shell 脚本,我在其中 tar 一些数据,然后通过 sftp 将其发送到不同的服务器。由于这是非常重要的数据,如果出现问题,我希望通过电子邮件得到注意。这就是为什么我选择在 cron 作业(托管服务器)中发生错误时收到通知。

这是 sftp 连接的样子:

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

不幸的是,现在每次脚本运行时我都会收到一封邮件,显示 sftp 连接的输出,如下所示:

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

脚本有效并且文件被传输。那么有没有办法隐藏 sftp 连接的输出,但显示脚本可能出现的错误。非常感谢任何帮助!

【问题讨论】:

    标签: bash shell cron warnings sftp


    【解决方案1】:

    抑制这些进度条的示例方法是执行以下命令:

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

    这个命令会在STDERR上显示错误

    【讨论】:

    • 感谢这行,因为它删除了Connected to server.com. 行。 stats 表是由脚本稍后的 curl 请求创建的。我现在已经用curl --silent 静音了。
    【解决方案2】:

    您可以重定向 sftp 的输出:

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

    这样你就看不到输出了。

    然后,如果您想查看可能的错误,可以使用:

    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
    

    状态代码基于此列表:https://support2.microfocus.com/techdocs/2487.html

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-12-12
      • 1970-01-01
      • 2014-04-16
      • 2014-04-24
      • 2011-08-23
      • 2015-10-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多