I have a RHEL server that I am running a MySQL database on. I have a Bash script that executes mysqldump
that creates a backup file. The backup file created when executing the script directly in Bash is 754259 bytes in size. If the same script is run via cron, it's only 20 bytes in size.
As far as I know, cron is running with the same user context that I use when logged in to run the script manually. However, given the size differential, that does not appear to be true.
Why are the file sizes different when running the same script?
The shell script contents:
backup_path=/var/custom/db_backups
configFile=/var/custom/auth.cnf
db_name=[db_name]
date=$(date +"%d-%b-%Y")
sudo /opt/rh/mysql55/root/usr/bin/mysqldump --defaults-extra-file=$configFile $db_name | gzip -9 > $backup_path/$db_name-$date.sql.gz
To edit cron:
sudo crontab -e
cron file contents:
12 21 * * * /var/custom/maint_plan
This executes the script daily at 9:13 PM.
sudo
from the script: you are running it from root's crontab anyway, and sudo may not like running without a tty - see Why does cron silently fail to run sudo stuff in my script?