2

I am automating the ec2 start and shutdown, I have written a bash script, which works when I run it manually, but when I add the bash to run by crontab its not executing. I have assigned proper permissions to the script. Here is my script

#! /bin/bash
/usr/local/aws/bin/ec2-start-instances --region us-east-1  i-abc12345

Here is my crontab

35 13 * * * /bin/bash /home/ubuntu/.script/testshutdown.sh

Please help me out !!!

Jeevan Dongre
  • 4,627
  • 13
  • 67
  • 129
  • You may be missing some environmental variables that the ec2 command line tools require in the session that cron uses. You may want to try defining them in your script before you run the ec2 commands. – datasage Mar 21 '13 at 13:44

2 Answers2

2

What output do you get from /var/log/cron with regard to any errors? Further, if you put your a she-bang of #!/bin/bash at the start of the script, you don't need to tell cron how to execute the script. Make sure it's executable (chmod +x testshutdown.sh).

Are you editing /etc/crontab, running crontab -e, or otherwise? There's more than just one crontab on a system and they are edited differently in terms of number of values needed in some cases.

Mark Stanislav
  • 979
  • 4
  • 11
1

My answer here might be the help you are looking for: https://stackoverflow.com/a/21397517/2083509 Snippet:

AWS_ACCESS_KEY="blah-blah-dingle-smith" # changeme
AWS_SECRET_KEY="yankee-doodle-shit-no-stank" # changeme
JAVA_HOME="/usr/lib/jvm/java"
EC2_HOME="/opt/aws/apitools/ec2"
EC2_URL="https://us-west-2.ec2.amazonaws.com/" # changeme
PATH="$PATH:/opt/aws/bin"  # is dir contains a symlinks of tool binaries
export AWS_ACCESS_KEY AWS_SECRET_KEY JAVA_HOME EC2_HOME EC2_URL PATH

Add the above directly into your testshutdown.sh script. Or give it its own script and load it before yours on the same crontab job. ex: * * * * * /home/ubuntu/.script/aws-env.sh; /home/ubuntu/.script/testshutdown.sh or what i suggest todo from the link (source a *.conf) Using full access admin keys for specific tasks is never advisable. ;)

Might be wise to set your SHELL in cron itself. Replace or Add SHELL=/bin/bash to your cron. Even if you have proper shebang, its still a good decision. You might drive yourself nuts trying to find out why commands you know work, don't.

Community
  • 1
  • 1
jonretting
  • 412
  • 2
  • 6