1

I've been going through all the various questions about why a specific script run in cron and nothing has helped me thus far. So I'm just trying to run a mysqldump script that now looks like this:

#!/bin/bash
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# you must pass arguments to this script
if [ $# -eq 0 ]
        then
                echo "No arguments supplied"
                exit 1
fi

# if a path is supplied, perform the mysqldump to that directory
today=`date +%m-%d-%Y`
mysqldump --defaults-file=/home/<user>/.my.cnf --databases 360_projects --single-transaction --add-drop-database --triggers --routines -u BACKUPUSER > $1pmbackup${today}.sql

Notice that I've added explicit paths, let it know that I want to run it in bash, and in mysqldump that I'm manually specifying the path to my config file in case cron doesn't pick it up. None of this has helped thus far.

And finally my crontab, which I invoke using crontab -e so that it automatically installs when I quit my editor.

SHELL=/bin/sh
PATH=/usr/local/sbin:/usr/local/bin:/sbin:/bin:/usr/sbin:/usr/bin

# Backup of MySQL Database every 4th Hour, Locally, Remotely
6 11 * * * /bin/bash /home/<user>/Scripts/sql-backup.sh /media/mysqlBackup-Daily/

I've made sure of course that my script is owned by the user to which this crontab belongs and the permissions are set to rwxr so I'm really not sure where this is going wrong.

Output of my /var/log/cron for that specific time when I was trying to test it:

Aug 21 14:06:01 www5 crond[1305]: (<user>) RELOAD (/var/spool/cron/<user>)

EDIT: Perhaps the best question for the time being is simply how to get more verbose output from a cron job. Right now, all I'm getting is a line telling me it reloaded (see above).

EDIT 8-27-18: I noticed the suggestion to use logger to get more verbose output but that doesn't seem to be working. My error now changed to:

Aug 27 13:55:01 www5 crond[1305]: (php) ERROR (getpwnam() failed)

From the command:

*/5 10 * * * /bin/bash /home/<user>/Scripts/sql-backup.sh /media/vantecBackup1/mysqlBackup-Daily/ 2>&1 | /usr/bin/logger -t sql_daily_backup

What do you make of this error? It doesn't make much sense to me since the script does not use php. Thanks.

9
  • Should it be --defaults-extra-file instead of --defaults-file. Also you should specify full path for anything you are running, e.g., /usr/bin/mysqldump Aug 21, 2018 at 18:26
  • I attempted your recommendation but it doesn't make a difference. Aug 21, 2018 at 18:31
  • Have you tried 1) running the script manually, or 2) it with sudo crontab -e so it has root privileges?
    – phandolin
    Aug 21, 2018 at 22:33
  • @phandolin As memtioned in my question, this script is working perfectly when run from the CLI as the user that will run this cron job. So pretty sure it's not a permissions thing, but I haven't tried it as root yet, so I will look. Aug 21, 2018 at 22:42
  • @phandolin Running as root didn't produce any results either. Cron log continues to not be too useful saying Aug 21 19:28:01 www5 crond[1305]: (root) RELOAD (/var/spool/cron/root) Aug 21 19:28:02 www5 CROND[20320]: (pcp) CMD ( /usr/libexec/pcp/bin/pmie_check -C) Aug 21, 2018 at 23:35

3 Answers 3

0

In the file script you must add option passs( -p PASSWORD) on command mysqldump because when run script you can enter password so script not running. You re-add command to script with option -p.

mysqldump --defaults-file=/home/<user>/.my.cnf --databases 360_projects --single-transaction --add-drop-database --triggers --routines -u BACKUPUSER -p PASSWORD > $1pmbackup${today}.sql

I think script and cron auto running.

4
  • This will not work. As described in man mysqldump "The password to use when connecting to the server. If you use the short option form (-p), you cannot have a space between the option and the password. If you omit the password value following the --password or -p option on the command line, mysqldump prompts for one. Specifying a password on the command line should be considered insecure. You can use an option file to avoid giving the password on the command line." So not only is it insecure, but it completely voids out the point of me providing an options file. Aug 22, 2018 at 14:56
  • Yes, example script is: mysqldump --defaults-file=/home/<user>/.my.cnf --databases 360_projects --single-transaction --add-drop-database --triggers --routines -u<BACKUPUSER> -p<PASSWORD> > $1pmbackup${today}.sql
    – Kotler
    Aug 24, 2018 at 1:34
  • NEVER use -p; it will be plain text shown in ps . Mysql had an option where you can store username and password. user and password should be in --defaults-file so -u and -p are useless it the command.
    – Rinzwind
    Aug 27, 2018 at 19:12
  • Why, because if you not add option -p<Password>, you cannot run script on cron
    – Kotler
    Aug 28, 2018 at 4:04
0

Use logger to output the result of your cron script. See https://serverfault.com/a/434902/470077

That will give you some clues as to what you should actually be doing. Otherwise we're simply making guesses as to what is wrong.

0

Huge thanks to everyone who helped on this one, it took me a lot of debugging to fix this one. Issue 1): At some point seeing everyone's advice here I changed my script to read

mysqldump --databases 360_projects --single-transaction --add-drop-database --triggers --routines -u$user -p$password > $1pmbackup${today}.sql 2>&1 >> /var/log/cron

or something like this, I deleted it as soon as I realized the idiocy. What was happening there was that I was getting a file creation, but at 0 bytes. Removing that fixed that. However after resolving everything you all had mentioned I was having this error in /var/log/cron:

Aug 28 10:27:50 www5 crontab[12101]: PAM pam_end: NULL pam handle passed

This lead me to see that it was a bug in cronie in CentOS (I guess I should have mentioned this askUbuntu is just more frequented/friendly than other Linux QA Sites). So updated that and it's working fine now. Here are my final scripts for reference.

#!/bin/bash

# you must pass arguments to this script
if [ $# -eq 0 ]
        then
                echo "No arguments supplied"
                exit 1
fi

# if a path is supplied, perform the mysqldump to that directory
today=`date +%m-%d-%Y`
mysqldump --defaults-file=/home/achamberlain/.my.cnf --databases 360_projects --single-transaction --add-drop-database --triggers --routines -u BACKUPUSER > $1pmbackup${today}.sql

And my crontab looks like this:

# Backup of MySQL Database every 4th Hour, Locally, Remotely
5 8-20/4 * * * /home/<user>/Scripts/sql-backup.sh /media/vantecBackup1/mysqlBackup-Daily/

EDIT: My solution originally used all the PATH variables that you provided as answers, but it works without it, so it might just have been the bug in cronie.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .