简体   繁体   English

cron作业中的脚本未生成sql文件

[英]script in cron job not generating an sql file

I have this script in afbackupdb.sh : 我在afbackupdb.sh中有此脚本:

mysqldump  -u user -ppassword --host IP DBNAME > /var/www/html/af/af_core/af_core_cron/afdb/af.sql

When I run this directly in terminal it returns the correct sql file with contents. 当我直接在终端中运行此命令时,它将返回包含内容的正确sql文件。

When the cron does it it returns the same file with same size but I cant open it. 当cron执行此操作时,它将返回相同大小的相同文件,但我无法打开它。 I get the error : 我得到错误:

System Error.  Code: 123.
The filename, directory name, or volume label syntax is incorrect

iamge

Above image shows the first file from running in terminal the rest is from cron jobs which I cant open 上图显示了在终端中运行的第一个文件,其余的是我无法打开的cron作业

UPDATE : 更新:

In crontab -e: 在crontab -e中:

*/5 *  *  *  *   /var/www/html/af/af_core/af_core_cron/affiles/afbackupdb.sh

Running my script directly then running : head af.sql gets me 直接运行我的脚本然后运行: head af.sql让我

-- MySQL dump 10.13  Distrib 5.6.37, for Linux (x86_64)
--
-- Host: IP    Database: dbname
-- ------------------------------------------------------
-- Server version       5.6.37

/*!40101 SET @OLD_CHARACTER_SET_CLIENT=@@CHARACTER_SET_CLIENT */;
/*!40101 SET @OLD_CHARACTER_SET_RESULTS=@@CHARACTER_SET_RESULTS */;
/*!40101 SET @OLD_COLLATION_CONNECTION=@@COLLATION_CONNECTION */;
/*!40101 SET NAMES utf8 */;

While tail .af.sql gets me: 虽然尾巴.af.sql让我知道:

/*!40101 SET SQL_MODE=@OLD_SQL_MODE */;
/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */;
/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */;
/*!40101 SET CHARACTER_SET_CLIENT=@OLD_CHARACTER_SET_CLIENT */;
/*!40101 SET CHARACTER_SET_RESULTS=@OLD_CHARACTER_SET_RESULTS */;
/*!40101 SET COLLATION_CONNECTION=@OLD_COLLATION_CONNECTION */;
/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */;

-- Dump completed on 2017-10-26 10:40:02
You have new mail in /var/spool/mail/root

You mention cron, but your image looks to me be a Windows UI. 您提到了cron,但您的图片在我看来是Windows UI。 I assume you are attempting to open the file via Samba/CIFS? 我假设您正在尝试通过Samba / CIFS打开文件? I suspect the issue is an iteration of permissions, whether in Windows' ACLs or Unix standard ugo . 我怀疑问题是权限的迭代,无论是在Windows的ACL中还是在Unix标准ugo中 The right-caret ( > ) is the likely pain point. 右尖号( > )是可能的疼痛点。 Without more information, my answer below assumes it's a Unix root vs non-root user issue. 没有更多信息,我在下面的回答中假设这是Unix root用户非root用户的问题。

The right caret redirects output from stdout to a file owned by the current shell user . 正确的插入符号会将输出从stdout重定向到当前shell用户拥有的文件。 So, even though you're logging into MySQL with your options of -u and -p , the shell is still writing the file (not MySQL), and the file is therefore owned by the UID of the user running the shell. 因此,即使使用-u-p选项登录到MySQL,shell仍在写入文件(不是MySQL),因此该文件归运行Shell的用户的UID所有。

When you log in and manually execute your script, your user/UID is the one creating the files. 登录并手动执行脚本时,您的用户/ UID是创建文件的用户/ UID。 When running cron, I suspect the shell user is root . 运行cron时,我怀疑shell用户是root Do you edit the crontab via: 您是否通过以下方式编辑crontab:

$ su -
# crontab -e

$ sudo su -
# crontab -e

$ sudo -s
# crontab -e

$ sudo crontab -e

All of those end up editing root 's crontab, and the resulting execution (and file ownership) will be by the root user. 所有这些最终都将编辑root的crontab,最终执行(和文件所有权)将由root用户执行。

If this indeed is the issue, either use your user's crontab, or consider running your cronjob as your user: 如果确实是问题所在,请使用用户的crontab或考虑以用户身份运行cronjob:

*/5 *  *  *  *   su <username> -c "/var/www/html/af/af_core/af_core_cron/affiles/afbackupdb.sh"

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

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