tags:

views:

135

answers:

2

Dear all,

I wish to let cron executes delete_snapshot.bash, but when I try to create cron as below:
*/1 * * * * /var/www/mango_gis/delete_snapshot.bash >/dev/null
It didn't execute my script at all, because when I didn't see it delete the snapshot in the amazon cloud, and my script is already tested with bash, it work fine.

Here is my script as below:

#!/bin/bash

get()
{
    local pos=$1
    shift
    eval 'echo ${'$pos'}';
}

length(){ echo $#; }

find_snapshots()
{
    echo $(ec2-describe-snapshots | xargs -n1 basename);
}

snapshots=$(find_snapshots)
len=$(length $snapshots)
row_count=$(($len/6))

if(($row_count > 6)); then
    delete_count=$(($row_count-6))
    for (( i=1; i<=$delete_count; i++ )); do
            ec2-delete-snapshot $(echo $(get $((2+$((6*$(($i-1)))))) $snapshots)) > /dev/null
    done
fi


In above, I have found the problem is that I call one command of EC2 command.
I have tested to create one cron job to call this command is ec2-describe-snapshots,
but it doesn't work.

Please advise...

Leakhina

A: 

you should add the program with which you want to process this file; something like:

*/1 * * * * /bin/bash  /var/www/mango_gis/delete_snapshot.bash > /dev/null

using the full path is important, because you do not have the environment variables loaded when cron starts the process

Thariama
I have changed it to like this */1 * * * * /usr/bin/bash /var/www/mango_gis/delete_snapshot.bash >/dev/nullbut it didn't work.
leejava
you may use "which bash" on commandline to find out the location of your bash program. then put this absolute path into your cron job. (location of programs differ from system to system, for example i got the bash program under /bin/bash (from your edit it looks like your absolute path to bash is the same))
Thariama
yesh, I have do like that also, but it still not working
leejava
please copy/paste your command (everything afer the wildcards) to your commandline and tell us what happens (does it work propperly?)
Thariama
I copy /bin/bash /var/www/mango_gis/delete_snapshot.bash > /dev/null and past it to my command, it worked fine.
leejava
in that case your program is fine - check cron using the cronlog. Does any other cronjob get executed? you can for example place a very simple one there
Thariama
now I know the problem, the problem is that in delete_snapshot.bash, I have called ec2 command is ec2-describe-snapshots,and it doesn't work when I create one cron job to execute this ec2-describe-snapshots command and redirect the result to log file, but it doesn't work. have you ever created cron job to execute ec2-describe-snapshots of amazon cloud?
leejava
never used ec2-describe-snapshots at all, but it is running from commandline so i guess it is possible to make it work (maybe you miss some environment variables when executed via cron)
Thariama
for changing the cron environment there are some helpfull hints to be found here: http://stackoverflow.com/questions/2135478/how-to-simulate-the-environment-cron-executes-a-script-with
Thariama
A: 

thanks for your help so far. Now I can catch it.

the problem is that cron not load the env variable so we just need to specify what we use to env variable while cron executing. here is my test code for ec2 command as below:

export EC2_PRIVATE_KEY=/root/keys/pk-Q4NR5FGDTSWWK65EDSIDCBTEQWJ6G24V.pem export EC2_CERT=/root/keys/cert-Q4NR5FGDTSWWK65EDSIDCBTEQWJ6G24V.pem export JAVA_HOME=/usr/lib/jvm/java-6-sun/ export EC2_HOME_BIN=/usr/bin

echo "start ec2-describe-snapshots" >> /var/log/ec2-delete-snapshot.log $EC2_HOME_BIN/ec2-describe-snapshots >> /var/log/ec2-delete-snapshot.log echo "end it" >> /var/log/ec2-delete-snapshot.log

leejava
that is exactly what i was talking about yesterday in the other comments to my answer (you need to push "show x more comments" to see them)
Thariama