Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cron Job - How to send an output file to an email

Tags:

cron

crontab

I have this line in crontab:

* * * * * /var/www/dir/sh/mysql_dumb.sh | mail -s "mysql_dump" [email protected] 

(every minute only a sample)

So, all works fine, but the email is empty.

UPDATE:

The output from mysql_dumb.sh is a *.sql file and they save the file in a directory.

How can I send a copy (*.sql file) from this output -> mysql_dumb.sh to my email?

mysql_dumb.sh:

#!/bin/bash PATH=/usr/bin:/bin SHELL=/bin/bash /usr/bin/mysqldump -u USER -pPASS DATABASE > /var/www/dir/backup/backup_DB_`date +%d_%m_%Y`.sql 
like image 847
Black Sheep Avatar asked Apr 05 '14 21:04

Black Sheep


People also ask

How do I email a shell script output?

Run `mail' command by '-s' option with email subject and the recipient email address like the following command. It will ask for Cc: address. If you don't want to use Cc: field then keep it blank and press enter. Type the message body and press Ctrl+D to send the email.

Where does output from cron jobs go?

Like most daemons running on our system, the cron daemon logs its output somewhere under /var/log.

What is the use of * * * * * In cron?

It is a wildcard for every part of the cron schedule expression. So * * * * * means every minute of every hour of every day of every month and every day of the week .


2 Answers

If the script is reporting errors, they may be going to stderr, but you're only redirecting stdout. You can redirect stderr by adding 2>&1 to the command:

* * * * * /var/www/dir/sh/mysql_dump.sh 2>&1 | mail -s "mysql_dump" [email protected] 
like image 168
Barmar Avatar answered Sep 17 '22 19:09

Barmar


From a crond perspective more accurate is to place in to your cron:

[email protected] * * * * * /var/www/dir/sh/mysql_dumb.sh * * * * * /var/www/dir/sh/other.sh * * * * * /var/www/dir/sh/other2.sh 
like image 45
Dimitrios Avatar answered Sep 18 '22 19:09

Dimitrios