Please Help me out on Bash script CRON

Hey folks how are you doing. Need a help.
I have a bash script to backup MySQL databases, that runs on a daily cronjob. Everything is working fine as intended but only there is an error on S3 upload part. The script is working good on manual execution but the cronjob execution says:
s3: command not found
The S3 upload code is below.

S3_BUCKET="s3-bucket-name"
AWS="$(which aws)"
$AWS s3 cp backups.tar.gz s3://${S3_BUCKET}

Any help would be appreciated. Thanks all.

Did you find this post useful? Show some love!
DISCUSSION (6)

I think which aws won't work here. It's because CRON jobs run in other ENV than the user's. So you may want to use exact path like /home/USER/aws/installation/folder s3 cp ... or what ever your aws installation it.

Or problem may be in passing ENV variables.

Just try both of the above and do let me know if it works!

Though it's working, I am curious if those work on cron

which mysql
which mysqldump
which gzip

why which aws this is not working, I mean what I am missing?
aws is on /usr/local/bin/aws

It's because CRON Jobs(crontab) runs in different environment than the user. So the CRON doesn't know where users applications are installed(like aws, mysql, gzip etc.)

But CRON by default includes some paths and ENV variables like $HOME , /usr/bin and some others.(Quick google search will tell you everything CRON includes...)

So this way CRON recognizes mysql, mysqldump as they are installed on /usr/bin/. But it doesn't include aws as it's installed in /usr/local/bin/aws/ so you should manually specify it.

I know this is little complicated, even I had similar problem when I was working with whenever gem.

Yeah, it's complicated but you explain well thanks a lot.

Classic DEV Post from Nov 26

Google to pay JavaScript frameworks to implement performance-first code

Photo care of Unsplash user Sharon McCutcheon Interesting initiative by Google...

Mahfuz Shaikh
Join dev.to

The fastest growing software community.
Free forever.