0

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]
8
  • Could you add the crontab line that you use to call the script? Commented Feb 14, 2018 at 16:02
  • Have you tried to put /usr/bin/mysql instead of mysql? Commented Feb 14, 2018 at 16:09
  • @A.Villegas Crontab command is: 55 2 * * * /home/admin/web/mydomain.com/sync/sync.sh Commented Feb 14, 2018 at 16:12
  • @nacho I didn't. Should I try it? Commented Feb 14, 2018 at 16:14
  • 1
    @Wed did you add your cron in a user who has the permissions? Could you try to log your cronjob output as following 55 2 * * * /home/admin/web/mydomain.com/sync/sync.sh >> /var/log/test.log 2>&1 and share file output? Commented Feb 14, 2018 at 16:26

1 Answer 1

3

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
Sign up to request clarification or add additional context in comments.

2 Comments

It should be /usr/bin/mysql
yes, I was attempting to make the point about cron's environment being stripped so you need to give full paths, by giving an incorrect path, the poster will see an error, and by giving the solution to finding the correct path the poster will correct the error. Give the poster the tools, not the answer.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.