Reputation: 492
I have a backup script for my servers, and there is a switch to backup mysql databases if mysqld
is installed.
# If flag is set and executable is installed
if [[ "$flag_s" -eq 1 ]]; then
printf "STATE: Detected flag -s\n" >> "$logfile"
if [[ -x "$(command -v mysqld)" ]]; then
printf "STATE: Starting backup of MySQL databases\n" >> "$logfile"
mysqldump --defaults-extra-file=/custom/files/my.cnf --single-transaction -u root --all-databases | gzip > "$backup_dir"/"$hostname"_"$datetime"_all_mysql_dbs.gz;
else
printf "ERROR: mysqld not installed\n" >> "$logfile"
fi
fi
However, when running from root's crontab, it's running the else statement. In the log, I see ERROR: mysqld not installed
, even though mysqld
is definitely installed. The rest of the script runs fine (e.g., backing up directories, etc...).
If I run the code below as root, it correctly shows mysqld
as being installed.
root@db03:~# if [[ -x "$(command -v mysqld)" ]]; then
> printf "STATE: Starting backup of MySQL databases\n"
> else
> printf "ERROR: mysqld not installed\n"
> fi
STATE: Starting backup of MySQL databases
In addition, if I run the entire script as root, it works correctly. It's only failing when running from crontab. What am I missing? Do I need an environment variable? Is it an issue with root's PATH?
root@db03:~# echo $PATH
/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games
This is running on Ubuntu 18.04 LTS
with the bash package 4.4.18-2ubuntu1.1
.
Upvotes: 0
Views: 63
Reputation:
Try this -- add:
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin
somewhere near the top of the crontab file.
Upvotes: 1