[SOLVED] Script failing to Run In a cron

General support questions
Post Reply
intermediatelinux
Posts: 56
Joined: 2014/01/25 09:32:09

[SOLVED] Script failing to Run In a cron

Post by intermediatelinux » 2017/06/28 08:18:34

So I have a Wordpress blog running on an AWS CentOS host. Every night at three AM, I run the script below. This is supposed to take a dump of the MySQL database used by the Wordpress user, and scp the resulting backup to my Mac sitting at home.

The script works flawlessly when run manually, but never when sitting in a cronjob:

Code: Select all

0 3 * * * $HOME/bin/wordpress.sql.backup.sh

Code: Select all

#!/bin/sh 

#### some variables 

date=`date +%Y%m%d`
localUser="john"
localDirectory="/home/john/backups/sql"
localFile="wordpress_backup"
key="$HOME/.ssh/tomac_rsa"
remoteHost="mydomain.com"
remotePort="XXXXX"
remoteUser="john"
remoteDirectory="/Users/john/Documents/backup"
db="wordpress"
dbUser="root"
dbPass="****************************************"

### usual stuff about 'if this doesn't exist, create it'

if [ ! $localDirectory ]; then
    /bin/mkdir $localDirectory
fi

### do the dump 

/usr/bin/mysqldump -u $dbUser -p$dbPass $db > $localDirectory/$localFile.$date.sql

### put the file on Mac

/usr/bin/scp -P $remotePort -i $key $localDirectory/$localFile.$date.sql $remoteUser@$remoteHost:$remoteDirectory

### remove anything older than 90 days 

/bin/find $localDirectory -mtime +90 | /usr/bin/xargs /bin/rm -rf > /dev/null 2&>1

exit 0 
Am I doing something wrong? It isn't just the scp part of the script that fails: there is no backup SQL file on the AWS host or on the Mac.

Thanks.
Last edited by intermediatelinux on 2017/06/28 12:29:19, edited 1 time in total.

tunk
Posts: 1206
Joined: 2017/02/22 15:08:17

Re: Script failing to Run In a cron

Post by tunk » 2017/06/28 09:42:15

It could be that $HOME is defined when run from command line, but not when run as a cron job.
You may also be missing the user name: 0 3 * * * user-name $HOME/bin/wordpress.sql.backup.sh

intermediatelinux
Posts: 56
Joined: 2014/01/25 09:32:09

Re: Script failing to Run In a cron

Post by intermediatelinux » 2017/06/28 11:38:25

tunk wrote:It could be that $HOME is defined when run from command line, but not when run as a cron job.
You may also be missing the user name: 0 3 * * * user-name $HOME/bin/wordpress.sql.backup.sh
I replaced $HOME with the absolute path to the script both in the crontab and in the script. Still no luck.

I don't think it's the username either. I have that in no other scripts, and they work perfectly on cron.

intermediatelinux
Posts: 56
Joined: 2014/01/25 09:32:09

Re: Script failing to Run In a cron

Post by intermediatelinux » 2017/06/28 12:11:31

tunk wrote:It could be that $HOME is defined when run from command line, but not when run as a cron job.
You may also be missing the user name: 0 3 * * * user-name $HOME/bin/wordpress.sql.backup.sh
OK, so I changed the crontab to get the script to run in five minutes, and inserted various `touch` commands in the script, to create files in my home directory at various points. None of the files were created. I ran the script manually, and it duly created the files.

So for some reason, the script isn't even being run. I'm not in cron.deny. So I've tried explicit PATH in the script, and it's due to run in five minutes from now..............

intermediatelinux
Posts: 56
Joined: 2014/01/25 09:32:09

[SOLVED] Re: Script failing to Run In a cron

Post by intermediatelinux » 2017/06/28 12:26:43

Someone, please hit me.

crond wasn't running.

How the heck did I miss that????

Anyway, thank you very much for the help.

Post Reply