首页 > 解决方案 > 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!

标签: bashshellcronwarningssftp

解决方案


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

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

此命令将显示错误STDERR


推荐阅读