0
  • 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)"

10
  • 1
    Cron jobs don't have the same PATH. In your case it does not contain the directory of the redis-cli command. Try a cronjob with just printenv PATH > /tmp/foo and check. Commented Sep 29, 2023 at 10:43
  • 1
    You can set the PATH in your script (export PATH=.....). This makes sense anyway, because your script is then independent from the PATH setting of the parent process. This is also an advantage, when the script is used interactively. Commented Sep 29, 2023 at 10:46
  • 1
    And you can set in in your crontab file: PATH=...:/usr/local/bin:.... Do not use PATH=$PATH:/usr/local/bin because some implementations do not expand $PATH. Use absolute paths of all directories that are needed for your cron jobs. Commented Sep 29, 2023 at 10:55
  • 1
    @PirateApp: I was not questioning that the PATH is correct when you run it manually; I was talking about the PATH at the time cron runs your job. Commented Sep 29, 2023 at 11:04
  • 1
    Just two question below your (with cron tag): stackoverflow.com/a/77197682/2908599 Commented Sep 29, 2023 at 17:27

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.