Cron Job Help Needed

Hi Folks,

Need some with getting a cron job to run.

I’m using this script that found in another CRON related thread:

#!/bin/bash
# script needs these to use Hestia Tools
export HESTIA=/usr/local/hestia
export PATH=$PATH:/usr/local/hestia/bin

# Loop through  Users
for USER in $( /usr/local/hestia/bin/v-list-users plain | awk '{print $1}' )
do
	DIR="/home/$USER/dbbackup"
	echo "= Processing $USER "
        # Make dir if this is the first time
	if [ ! -d "$DIR" ] ; then
		echo "Creating dir"
		mkdir -p "$DIR"
		chown -f "$USER":"$USER" "$DIR"
	fi

	# Loop through hestia DBs for that user
	for DB in  $( /usr/local/hestia/bin/v-list-databases "$USER" plain | awk '{print $1}' )
	do
		# Dump DB
		/usr/bin/mysqldump --defaults-extra-file=/root/.my.cnf --quick --single-transaction --skip-add-locks "$DB" | gzip > "$DIR"/"$(date +%F-%H-%M)"_"$DB".tar.gz
		# Pause to give the server a break
		sleep 10
	done
	# fix perms (silently)
	chown -f "$USER":"$USER" "$DIR"/*.tar.gz
	# Keep only last three days' files locally. Older can be recovered from offsite backup
	echo "Deleting old backups in $DIR"
	find "$DIR" -name '*tar.gz' -mtime +3 -delete
done

I have the file located in /usr/local/hestia/bin.

When logged in as root in SSH, I can run the file manually with the command:

sudo /usr/local/hestia/bin/sqlbackup.sh > /home/nolatron/web/domain.com/private/sqlbackups/myjob.log 2>&1

And this works fine.

So I created a CRON job under the admin account:

Screenshot 2024-09-12 at 1.11.54 PM

But nothing ever runs. syslog shows it being logged:

Sep 12 14:08:01 ghibli CRON[3080146]: (admin) CMD (sudo /usr/local/hestia/bin/sqlbackup.sh > /home/nolatron/web/domain.com/private/sqlbackups/myjob.log 2>&1)

I don’t have email setup on this server so I don’t think I can get it to send any email results.

I’m just not sure what else I’m missing to figure out why the CRON can’t run it.

Thanks!
Shaun

Keep in mind that when using sudo, the sudo command affects the command in that context but redirection is not performed by sudo so it is redirecting the output of the command using admin user perms, and if the admin user doesn’t have the right perms to access and write to /home/nolatron/web/domain.com/private/sqlbackups/myjob.log it can’t be able to write the output.

Change it to /tmp/myjob.log to test it.

Bingo! This did it. The SQL files are now getting saved.

Thank you!!

1 Like

I’d add that putting the script in /usr/local/hestia/bin is probably not advisable, as hestia may decide to do things in that directory.
I’d put it in the admin home directory, or maybe root and run it as root, and all your sudo problems go away!

This topic was automatically closed 30 days after the last reply. New replies are no longer allowed.