简体   繁体   中英

script in cron job not generating an sql file

I have this script in 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.

When the cron does it it returns the same file with same size but I cant open it. 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

UPDATE :

In 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

-- 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:

/*!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. I assume you are attempting to open the file via Samba/CIFS? I suspect the issue is an iteration of permissions, whether in Windows' ACLs or Unix standard 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.

The right caret redirects output from stdout to a file owned by the current shell user . 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.

When you log in and manually execute your script, your user/UID is the one creating the files. When running cron, I suspect the shell user is root . Do you edit the crontab via:

$ 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.

If this indeed is the issue, either use your user's crontab, or consider running your cronjob as your user:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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