Wed
Wed

Reputation: 342

What is wrong with this bash script (cron + mysql)

Im using a bash script (sync.sh), used by cron, that is supposed to sync a file to a MySQL database. It works by copying a file from automatically uploaded location, parse it by calling SQL script which calls other MySQL internally stored scripts, and at the end emails a report text file as an attachment.

But, seems like something is not working as nothing happens to MySQL databases. All other commands are executed (first line and last line: copy initial file and e-mail sending). MySQL command when run separately works perfectly. Server is Ubuntu 16.04. Cron job is run as root user and script is part of crontab for root user.

Here is the script:

#!/bin/bash
cp -u /home/admin/web/mydomain.com/public_html/dailyxchng/warehouse.txt /var/lib/mysql-files
mysql_pwd=syncit4321
cd /home/admin/web/mydomain.com/sync
mysql -u sync -p$mysql_pwd --database=database_name -e "call sp_sync_report();" > results.txt
echo "<h2>Report date $(date '+%d/%m/%Y %H:%M:%S')</h2><br/><br/> <strong>results.txt</strong> is an attached file which contains sync report." | mutt -e "set content_type=text/html" -s "Report date $(date '+%d/%m/%Y %H:%M:%S')" -a results.txt -- [email protected]

Upvotes: 0

Views: 866

Answers (1)

Cwissy
Cwissy

Reputation: 2156

cron will execute the script using a very stripped environment. you probably want to add the full path to the mysql command to the cron script

you can find the full path by

which mysql

at the prompt, or you can add an expanded path to the cron invocation

1 2 * * * PATH=/usr/local/bin:$PATH scriptname

Upvotes: 3

Related Questions