redis-cli works perfectly when bash script is executed manually but gives a "command not found" error when run from crontab

89 Views Asked by At
  • I have an Amazon Linux 2 powered EC2 instance that runs user data script and installs redis-cli as part of it. This redis instance runs inside Elasticache and therefore requires TLS support

I am following the documentation by Amazon Linux 2 to install it like this inside my user data script

yum install -y openssl-devel gcc
wget http://download.redis.io/redis-stable.tar.gz
tar xvzf redis-stable.tar.gz
cd redis-stable || exit
make distclean
make redis-cli BUILD_TLS=yes
install -m 755 src/redis-cli /usr/local/bin/
cd ..
rm -rf redis-stable
rm redis-stable.tar.gz

I added a crontab for the user ec2-user from my user data script whose definition goes like this

crontab -u ec2-user - <<EOF
0 0,4,8,12,16,20 * * * /home/ec2-user/utils/src/elasticache/backup-elasticache-to-s3.sh > /tmp/backup-elasticache-to-s3.log 2>&1
...
EOF

This script basically has a single redis-cli command inside that looks like follows

...
RESULTS=$(redis-cli -h "${REDIS_HOST}" -p "${REDIS_PORT}" --tls info all)
readonly RESULTS
...

The script works fine when I execute it manually inside my EC2 instance by calling

/home/ec2-user/utils/src/elasticache/backup-elasticache-to-s3.sh

but fails inside the crontab every single time with the following error

/home/ec2-user/utils/src/elasticache/backup-elasticache-to-s3.sh: line 99: redis-cli: command not found

Anyone knows how to get it working? Should I install inside /usr/bin instead of /usr/local/bin? Should I reference the full path inside my script?

Super appreciate if someone can help me identify the cause of failure. I verified that the crontab was running the script as ec2-user by running a simple "echo $(whoami)"

0

There are 0 best solutions below