简体   繁体   English

这个bash脚本(cron + mysql)有什么问题

[英]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. 我正在使用cron使用的bash脚本(sync.sh),该脚本应该将文件同步到MySQL数据库。 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. 它的工作方式是从自动上传的位置复制文件,通过调用SQL脚本进行解析(调用其他内部存储的其他MySQL脚本),最后通过电子邮件将报告文本文件作为附件发送。

But, seems like something is not working as nothing happens to MySQL databases. 但是,似乎没有任何作用,因为MySQL数据库没有任何反应。 All other commands are executed (first line and last line: copy initial file and e-mail sending). 执行所有其他命令(第一行和最后一行:复制初始文件和电子邮件发送)。 MySQL command when run separately works perfectly. 单独运行时,MySQL命令运行良好。 Server is Ubuntu 16.04. 服务器是Ubuntu 16.04。 Cron job is run as root user and script is part of crontab for root user. Cron作业以root用户身份运行,脚本是root用户crontab的一部分。

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 -- recipient@mydomain.com

cron will execute the script using a very stripped environment. cron将在非常剥离的环境中执行脚本。 you probably want to add the full path to the mysql command to the cron script 您可能想将mysql命令的完整路径添加到cron脚本

you can find the full path by 您可以通过以下方式找到完整路径

which mysql

at the prompt, or you can add an expanded path to the cron invocation 在提示时,也可以将扩展路径添加到cron调用中

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM