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

I have this line in crontab:
* * * * * /var/www/dir/sh/mysql_dumb.sh | mail -s "mysql_dump" example#mail.com
(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

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" example#mail.example

From a crond perspective more accurate is to place in to your cron:
MAILTO=example#mail.com
* * * * * /var/www/dir/sh/mysql_dumb.sh
* * * * * /var/www/dir/sh/other.sh
* * * * * /var/www/dir/sh/other2.sh

Look at the last line of mysql_dumb.sh:
/usr/bin/mysqldump -u USER -pPASS DATABASE > /var/www/dir/backup/backup_DB_`date +%d_%m_%Y`.sql
The > is redirecting the output of mysqldump to the file /var/www/dir/backup/backup_DB_date +%d_%m_%Y.sql
Do you want to store a backup of the database locally?
If not, take out the the > /var/www/dir/backup/backup_DB_`date +%d_%m_%Y`.sql and put the crontab entry back to
* * * * * /var/www/dir/sh/mysql_dump.sh 2>&1 | mail -s "mysql_dump" example#mail.example
If you do want a copy of the file locally, I would suggest using tee which will write the output to the file and put the output back out on stdout, which will later be picked up by crontab.
I would change the last line of mysql_dumb.sh to be:
/usr/bin/mysqldump -u USER -pPASS DATABASE | tee /var/www/dir/backup/backup_DB_`date +%d_%m_%Y`.sql
Again I would change the crontab entry back to:
/usr/bin/mysqldump -u USER -pPASS DATABASE > /var/www/dir/backup/backup_DB_`date +%d_%m_%Y`.sql
The advantage here is mail can read the information from stdout and isn't dependent on the file being written and then read correctly. While that may be a small difference, in my experience using tee will be more reliable.

If you want to store your cron job's output on the file on server and also want to send that output file to your mail address then you can use below command.
And you can use it on cronjob to automatically run on specific interval of time, here i am dumping mysql db and sending it to my mail with attachment of dumped sql file.
* * * * * mysqldump --user username --password='p#ssword' DBNAME | gzip > /home/user/db_backup/db_backup_`date +\%d-\%m-\%Y-\%H-\%M`.sql.gz && mailx -a /home/user/db_backup/db_backup_`date +\%d-\%m-\%Y-\%H-\%M`.sql.gz -s 'database backup date:'`date +\%d-\%m-\%Y-\%H:\%M` example#gmail.com

Related

Crontab with delete before sync

I have a question, i build on this moment a loadbalancer server with 2 servers. Now i have the sync with crontab.
But if i delete a file or directory on server 1 than stay the file on server 2. and if i delete a file or directory on server 2 than stay the file on server 1.
This my crontab from server 1
SHELL=/bin/bash
HOME=/
*/1 * * * * date >> /var/log/rsync_log
*/1 * * * * rsync -avrhe --delete-before 'ssh -p SSHPORTNUMBER' USERNAME#IPTOSERVER2:/home/ploi/ /home/ploi/ >> /var/log/rsync_log
This my crontab from Server 2
SHELL=/bin/bash
HOME=/
*/1 * * * * date >> /var/log/rsync_log
*/1 * * * * rsync -avrhe --delete-before 'ssh -p SSHPORTNUMBER' /home/ploi/ USERNAME#IPSERVER1:/home/ploi/ >> /var/log/rsync_log
Can anyone help me to fix this problem?
Thanks.
Willem
you can add a shell script , which write delete command, and use crontab to exec it;
when you want to delete a file, you just write command to this sh file, sh file will sync to other server, then use crontab to exec this sh file to delete the real file.

Cron job doing MySQL database back up not working when append date to the file name

I'm using shared cPanel hosting.
When I enter the following cron job command it works:
*/5 * * * * /usr/bin/mysqldump -u user -p'pazzword' my_db > /backup/database.sql
But when I try to add date stamp to the file name, it doesn't generate .sql files.
*/5 * * * * /usr/bin/mysqldump -u user -p'pazzword' my_db > /backup/database_$(date +"%Y_%m_%d").sql
I can't find cron job error logs, have I made an error in my command?
Okay. Finally I managed to fix the problem.
You need to escape % with **** .
So the working cron job command will look like this:
*/5 * * * * /usr/bin/mysqldump -u user -p'pazzword' my_db > /backup/database_$(date +"\%Y_\%m_\%d").sql

Crontab - simple echo not running

I've got such situation:
I want to schedule a job with crontab on a linux server. I'm not super-user, so I'm editing (with crontab -l, editor vim) only my crontab file. For testing, I put there:
* * * * * echo asdf
And the job is not running. Is the restart of the server needed? Or maybe some administrator move?
May be it is, cron jobs will run in their own shell. So you can't expect to see asdf on your console.
What you should try is
* * * * * echo asdf > somefile_in_your_home_directory_with_complete_path.log
Next check the file by doing a tail:
tail -f somefile_in_your_home_directory_with_complete_path.log
And if it's not, check if the cron daemon itself is running or is down:
# pgrep crond
OR
# service crond status
If you want to echo something on your shell you could use wall:
* * * * * wall <<< "Hello from cron"
* * * * * echo "Hello from cron" | wall
These two lines basically do the same but the first one might not work on older shell, just choose your favorite.
Anyway, be aware that wall will send your message to every user currently connected.
For me * * * * * /bin/echo text > file is not working...I don't know why, previleges and everything is set.
(This command is running normaly when I execute it as the particular
root user, just to clarify this.)
This can be solved by injecting the path PATH=$PATH:/bin in my example.
Instead * * * * * echo text > file is working fine, probably path issue.
Hope I helped

How-To prevent Cronjob from creating a file

I have the following problem:
I have this cronjob:
* 1-23 * * * /usr/bin/wget http://yannick-w.de/Test/getData.php
and this cronjob always creates a new logfile. I don't need this logfile, so I want to prevent cronjob from creating it. How is this possible?
Cheers
Just redirect stdout and stderr, for example
* 1-23 * * * /usr/bin/wget http://yannick-w.de/Test/getData.php > /tmp/getdata.out 2>&1
If no output is produced (e.g. because it has been redirected), it is not logged and not emailed to you by cron
BTW if you don't want any output at all replace /tmp/getdata.out with /dev/null
See crontab(5), cron(8)

Crontab without crontab -e

I would like to add a crontab schedule by doing this in my server:
echo "30 * * * * /home/my/var/dir/to/script /var/etc/etc/etc/" > crontab -e
Is there a way to do this without going doing crontab -e and then typing in the command?
Could try
1)nano /etc/crontab (or any other editor, e.g. emacs)
2)echo "30 * * * * /home/my/var/dir/to/script /var/etc/etc/etc/" > /etc/crontab
3)or put the contents of this into a file, then do "file > /etc/crontab"
like root:
echo "30 * * * * /home/my/var/dir/to/script /var/etc/etc/etc/" > /var/spool/cron/crontabs/username
We have the following setup in production on RHEL:
- a custom software starting sh in init.d , which
- handles cron start , stop , reload
- writes cron tasks into separate tmp file and loads this file with crontab -e
I have been only maintaining it for several months but it works like a charm ...
The proper fix is probably to use a file as specified in https://stackoverflow.com/a/4421284/377927, but it is possible to use tee to append a line to the crontab by doing e.g.:
echo "* * * * * ls" | EDITOR="tee -a" crontab -e
tee -a will append stdin to the file it gets specfied, the EDITOR variable tells crontab to use tee -a as editor.
If you have the whole crontab in a text file, you can upload a whole crontab to replace the old crontab by doing:
cat <crontab_text_file> | crontab -
This will wipe out your old crontab. Using '-' allows you to use standard input into the crontab.

Resources