Register
It is currently Tue Sep 23, 2014 4:20 am

[SOLVED]circular timer function in a bash script


All times are UTC - 6 hours


Post new topic Reply to topic  [ 8 posts ] 
Author Message
 PostPosted: Thu Jul 18, 2013 9:51 am   
User avatar

Joined: Fri Jul 22, 2011 5:39 pm
Posts: 47
Hello:

I am trying to delay two destructive instructions (via functions) in the script
IF $PENDING = "pending" using sleep_timer()
Here's the original script:
Code:
#!/bin/bash
# dw_copy_dataworks.sh
# 07/17/2013 02:29:24 PM EDT
export TERM=linux
export EC2_HOME=/home/c9admin/ec2/ec2-api-tools/current
export PATH=$PATH:$EC2_HOME/bin
export JAVA_HOME=/usr
export AWS_ACCESS_KEY=
export AWS_SECRET_KEY=
export EC2_URL=https://ec2.us-west-2.amazonaws.com
AMIDATE=$(date '+%m%d%Y')
DATAWORKS_AMI_SOURCE=$(sed -n '2p' /home/c9admin/dwd/dw_ebs_backup.lst | awk '{print $2}')
ec2-copy-image -r us-east-1 --region us-west-2 -s "$DATAWORKS_AMI_SOURCE" -n "$AMIDATE"_"dataworks" -d "Daily dataworks backup" -c 550e8400-e29b-41d4-a716-$(< /dev/urandom tr -dc 0-9 | head -c${1:-12};echo;)
OLD_IMAGE=$(ec2-describe-images | grep "ami-" | grep $(echo $(date +"%m%d%Y" --date="1 days ago")_dataworks) | awk '{print $2}')
OLD_SNAPS=$(ec2-describe-images `echo "$OLD_IMAGE"` | grep snap | awk '{print $4}')

DE_REGISTER()
{
for i in $(echo "$OLD_IMAGE")
    do ec2-deregister "$i"
done
}

DEL_SNAPSHOTS()
{
for i in $(echo "$OLD_SNAPS")
   do ec2-delete-snapshot "$i"
done
}

if [[ -n "$OLD_IMAGE" ]]; then
   #sleep 1h
   DE_REGISTER
   DEL_SNAPSHOTS
else
   exit
fi
exit 0
#EOF

These functions work great.
What I am trying to do is have the script wait, then loop to check for pending again else sleep 1h and then check for pending again.
If not true, then proceed with the(se) two destructive functions.

The main script should NOT process
DE_REGISTER
DEL_SNAPSHOTS

IF
Code:
[[ $PENDING = "pending" ]]
is true.

Here's what I have managed so far,
Code:
#!/bin/bash
# dw_copy_dataworks.sh
# 07/18/2013 11:40:27 AM EDT
export TERM=linux
export EC2_HOME=/home/c9admin/ec2/ec2-api-tools/current
export PATH=$PATH:$EC2_HOME/bin
export JAVA_HOME=/usr
export AWS_ACCESS_KEY=
export AWS_SECRET_KEY=
export EC2_URL=https://ec2.us-west-2.amazonaws.com
AMIDATE=$(date '+%m%d%Y')
DATAWORKS_AMI_SOURCE=$(sed -n '2p' /home/c9admin/dwd/dw_ebs_backup.lst | awk '{print $2}')
ec2-copy-image -r us-east-1 --region us-west-2 -s "$DATAWORKS_AMI_SOURCE" -n "$AMIDATE"_"dataworks" -d "Daily dataworks backup" -c 550e8400-e29b-41d4-a716-$(< /dev/urandom tr -dc 0-9 | head -c${1:-12};echo;)
OLD_IMAGE=$(ec2-describe-images | grep "ami-" | grep $(echo $(date +"%m%d%Y" --date="1 days ago")_dataworks) | awk '{print $2}')
OLD_SNAPS=$(ec2-describe-images `echo "$OLD_IMAGE"` | grep snap | awk '{print $4}')

# Functions
DE_REGISTER()
{
for i in $(echo "$OLD_IMAGE")
    do ec2-deregister "$i"
done
}

DEL_SNAPSHOTS()
{
for i in $(echo "$OLD_SNAPS")
   do ec2-delete-snapshot "$i"
done
}

sleep_timer()
{
sleep 1h
pending_status_check
}

pending_status_check ()
{
PENDING=$(ec2-describe-images | grep -i Pending | awk '{print $5}') # returns lowercase 'pending'
if [[ $PENDING = "pending" ]] ; then
   sleep_timer
else
   DE_REGISTER
   DEL_SNAPSHOTS
fi
}

pending_status_check


exit 0


I value and encourage your responses,
Thank you,


Last edited by Habitual on Tue Aug 13, 2013 3:00 pm, edited 1 time in total.

Top
 Profile WWW  
 PostPosted: Tue Aug 13, 2013 3:00 pm   
User avatar

Joined: Fri Jul 22, 2011 5:39 pm
Posts: 47
Code:
#!/bin/bash
# 08/12/2013 08:31:48 PM EDT
# http://ubuntuforums.org/showthread.php?t=2164483&page=3&p=12755319#post12755319
export TERM=linux
export EC2_HOME=/home/c9admin/ec2/ec2-api-tools/current
export PATH=$PATH:$EC2_HOME/bin
export JAVA_HOME=/usr
export AWS_ACCESS_KEY=
export AWS_SECRET_KEY=
export EC2_URL=https://ec2.us-west-2.amazonaws.com

# Variables
suffix=sftpserver
lst=/home/c9admin/dwd/${suffix}.lst
sleep=1m

# Constants
td=$( date +%m%d%Y )_${suffix}
yd=$( date +%m%d%Y -d yesterday )_${suffix}

# Copy to us-west-2
ec2-copy-image -r us-east-1 --region us-west-2 -s $( awk 'NR==1 {print $2}' "$lst" ) -n "$td" -d "Daily ${suffix^} backup"

# Yesterday's IMAGES + SNAPSHOTS
OLD_IMAGE=( $( ec2-describe-images | awk -vd="$yd" '$2~/ami-/ && $3~d { print $2; }' ) )
OLD_SNAPS=( $( ec2-describe-images "${OLD_IMAGE[@]}" | awk '$4~/snap/ { print $4 }' ) ) 

# Wait for "Available"
while :
do
  ec2-describe-images --filter state=available | grep -q "$td" && break
  sleep $sleep
done

# Delete Yesterday's IMAGE + SNAPSHOTS
for i in "${OLD_IMAGE[@]}" ; do ec2-deregister "$i"; done
for i in "${OLD_SNAPS[@]}" ; do ec2-delete-snapshot "$i"; done
#EOF


enjoy the goodness.


Top
 Profile WWW  
 PostPosted: Tue Aug 13, 2013 9:21 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 548
hi,

line 22: awk is not useful. you only read the first line of a file to get the second field, right ?
then
Code:
read -a ar <"$lst"
ec2-copy-image -r us-east-1 --region us-west-2 -s "${ar[1]}" -n "$td" -d "Daily ${suffix^} backup"
or
Code:
read nm field nm < "$lst"
ec2-copy-image -r us-east-1 --region us-west-2 -s "$field" -n "$td" -d "Daily ${suffix^} backup"


Top
 Profile  
 PostPosted: Thu Aug 15, 2013 3:45 pm   
User avatar

Joined: Fri Jul 22, 2011 5:39 pm
Posts: 47
Watael:

It's a multiline file|output, does your advice still apply?
and I dropped the "^" as I I don't need UPPERCASE
and bash on the cron host complained about 'bad substitution'

Thanks.


Top
 Profile WWW  
 PostPosted: Thu Aug 15, 2013 10:30 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 548
«It's a multiline file|output», but you only want the first line, right ?
if so, then my advice do still apply.

add a ``set -x'' at the beginning of the script, and redirect stderr to a file so we can see where 'bad substitution' applies.


Top
 Profile  
 PostPosted: Fri Aug 16, 2013 12:13 pm   
User avatar

Joined: Fri Jul 22, 2011 5:39 pm
Posts: 47
Code:
suffix=sftpserver
lst=/home/c9admin/dwd/${suffix}.lst
echo "Daily ${suffix^^} backup"

-bash: Daily ${suffix^^} backup: bad substitution

bash version GNU bash, version 3.2.25 / CentOS release 5.9 (Final)


where another (local) doesn't mind.
echo "Daily ${suffix^^} backup"
Daily SFTPSERVER backup

$ bash --version
GNU bash, version 4.2.37(2)-release (x86_64-slackware-linux-gnu)

so I don't have uppercase, I didn't specify it to the person that crafted this solution.

Thanks!


Top
 Profile WWW  
 PostPosted: Fri Aug 16, 2013 11:52 pm   

Joined: Mon Mar 02, 2009 3:03 am
Posts: 548
bash 3.2 !? (:~
it seems so old now bash-4.3-beta is available. <:-P


Top
 Profile  
 PostPosted: Sat Aug 17, 2013 9:39 am   
User avatar

Joined: Fri Jul 22, 2011 5:39 pm
Posts: 47
It's a VM "catalog" appliance.
Next version of AppLogic should have something more "current".


Top
 Profile WWW  
Display posts from previous:  Sort by  
Post new topic Reply to topic  [ 8 posts ] 

All times are UTC - 6 hours


Who is online

Users browsing this forum: Bing [Bot] and 4 guests


You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot post attachments in this forum

Jump to:  


BashScripts | Promote Your Page Too
Powered by phpBB © 2011 phpBB Group
© 2003 - 2011 USA LINUX USERS GROUP