I have a crontab job that call a simple script to backup a website:

15 0 1,10,20 * * /home/username/bin/backup_whatever

This is the script:

TODAY=$(date +"%Y%m%d")
rm -rf /home/username/filename.tar.gz
tar -zcvf /home/username/filename.tar.gz -C / var/www/website/
s3cmd put /home/username/filename.tar.gz s3://s3bucket/backups/$FILE_TO_PUT

As the tar command compress lots of files, the email I got when this is executed is really huge. How can I do to just display a message like compression successfully instead the full output of tar command?

Will be ok, if I do something like this? Can't find out the tar return codes.

EXITCODE=$(tar -zcvf /home/username/filename.tar.gz -C / var/www/website/)
if [ $EXITCODE -eq 0]
    echo "compression successfully"
    echo "compression unsuccessfully"
  • 3
    Why don't you just remove tar's verbose (v) option if verbose is not what you want? If it fails for any reason it will still output an error message which you'll get by email so you'll know about the problem.
    – Celada
    Dec 1 '14 at 8:54

The tar exit codes are documented in the "info" manual - use info tar to read all about tar. (You can read about "info" with info info). It seems there are 3 (major) exit codes. From the info tar documentation:

Possible exit codes of GNU 'tar' are summarized in the following

     'Successful termination'.

     'Some files differ'.  If tar was invoked with '--compare'
     ('--diff', '-d') command line option, this means that some files in
     the archive differ from their disk counterparts (*note compare::).
     If tar was given '--create', '--append' or '--update' option, this
     exit code means that some files were changed while being archived
     and so the resulting archive does not contain the exact copy of the
     file set.

     'Fatal error'.  This means that some fatal, unrecoverable error

   If 'tar' has invoked a subprocess and that subprocess exited with a
nonzero exit code, 'tar' exits with that code as well.  This can happen,
for example, if 'tar' was given some compression option (*note gzip::)
and the external compressor program failed.  Another example is 'rmt'
failure during backup to the remote device (*note Remote Tape Server::).

If you would like to only have a success or failure message of your choice, I'd recommend the following:

tar zcf /home/username/filename.tar.gz -C / var/www/website/ >/dev/null 2>&1
case $? in
        printf "Complete Success\n"
        printf "Partial Success - some files changed\n"
        printf "Disaster - tar exit code $?\n"

This invocation of tar pipes both the standard output and error to /dev/null, a.k.a. the bit bucket. Then the special parameter, $?, which holds the exit status of the most recently executed foreground pipeline, is parsed by the case statement to output the relevant message.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.