Send email about status of cron job completion

0

I have some scripts that do usual stuff like backing up MySql database, gz, tar some files and put them on FTP or sync with some other backup/mirror system. Some scripts are running quite frequently (like twice/thrice per hour). I am sending email with attached log output from commands after completion of job. It is quite a lot of email to keep track of.

I want to send email only when script fails to do something, that is when some command in script fails. How can I accomplish this?

TheVillageIdiot

Posted 2011-06-29T19:33:25.513

Reputation: 1 187

Answers

1

Check the return value of the command and proceed accordingly. Normally all the Linux commands return 0 on success and some other number(specifying the reason) on failure.

Update:
Forgot to mention that the status of last command is saved in variable "$?". Thanks @bbaja42

ssapkota

Posted 2011-06-29T19:33:25.513

Reputation: 1 213

suppose I run a command using curl like curl --upload-file..... in a script how will I check status? i = curl --upload-file... then if [i eq 0]? – TheVillageIdiot – 2011-06-29T19:43:28.913

2Status of last command is saved in variable "$?", so you can use [ $? -eq 0 ] – bbaja42 – 2011-06-29T19:49:10.147

2

@TheVillageIdiot: Example.

– user1686 – 2011-06-29T20:04:20.690

@bbaja42 and @grawity thanks for clearing up the things. I'm accepting @ssapkota's answer. – TheVillageIdiot – 2011-06-30T05:42:51.707

0

here is an example of script that I wrote for my work that's being used on daily basis

[alexus@wcmisdlin02 ~]$ sudo cat /usr/local/uftwf/mysql/_mysql.sh
#!/bin/sh
# $Id: _mysql.sh,v 1.2 2011/05/31 16:02:56 alexus Exp $

if [ -d /var/lib/mysql/ ]; then
    cd /var/lib/mysql/
    for i in `ls`; do
            /usr/bin/mysqldump \
                --user=root \
                $i > /usr/local/uftwf/mysql/$i.sql
            /bin/gzip \
                --force \
                --quiet \
                /usr/local/uftwf/mysql/$i.sql
    done
    /usr/bin/mysqladmin \
        --user=root \
        flush-logs
    /usr/bin/mysql \
        --user=root \
        --execute='PURGE BINARY LOGS BEFORE subdate(curdate(), INTERVAL 1 DAY);'
fi
[alexus@wcmisdlin02 ~]$ 


[alexus@wcmisdlin02 ~]$ sudo crontab -l -u mysql
@daily      /usr/local/uftwf/mysql/_mysql.sh
[alexus@wcmisdlin02 ~]$ 

it will only email you once you get some sort of an error, it will not mail if job ran ok. This shell script resides in /usr/local/uftwf/mysql and that's where backups goes as well. Modify this script to FTP/rsync files to your remote location.

alexus

Posted 2011-06-29T19:33:25.513

Reputation: 2 484

This has a number of issues which http://shellcheck.net/ can easily diagnose.

– tripleee – 2018-03-06T15:14:38.100

@Linker3000 if there are no errors, nothing will be emailed – alexus – 2018-03-06T15:47:17.777

@tripleee script works and has been since 2011, didn't fail even once... – alexus – 2018-03-06T15:47:44.087

Good for you, I'm cautioning others that it might not work for them because of undocumented assumptions and portability issues. – tripleee – 2018-03-06T16:46:18.023

1Where's the conditional emailing bit? – Linker3000 – 2011-06-29T21:30:12.903