Question

I have a crontab that fires a PHP script that runs the AWS CLI command "aws ec2 create-snapshot".

When I run the script via the command line the php script completes successfully with the aws command returning a JSON string to PHP. But when I setup a crontab to run the php script the aws command doesn't return anything.

The crontab is running as the same user as when I run the PHP script on the command line myself, so I am a bit stumped?

Was it helpful?

Solution

I had the same problem with running a ruby script (ruby script.rb). I replace ruby by its full path (/sources/ruby-2.0.0-p195/ruby) and it worked. in you case, replace "aws" by its full path. to find it: find / -name "aws"

OTHER TIPS

The reason it's necessary to specify the full path to the aws command is because cron by default runs with a very limited environment. I ran into this problem as well, and debugged it by adding this to the cron script:

set | sort > /tmp/environment.txt

I then ran the script via cron and via command line (renaming the environment file between runs) and compared them. This led me to see that I needed to set both the PATH and the AWS_DEFAULT_REGION environment variables. After doing this the script worked just fine.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top