python - how to get the time after two minutes -
i have execute command automatically after 2 minutes current date using shell scripting. lets current time in below format. script should read current time ,add 2 minutes existing time 2014-09-10t09-23-34 , execute remaining script.
date +"%y"-"%m"-"%d"t"%h"-"%m"-"%s"
2014-09-10t09-21-34
to date-string in bash:
echo "current: " $(date +"%y"-"%m"-"%d"t"%h"-"%m"-"%s") echo "+2 min : " $(date --date="@$(($(date +%s)+120))" +"%y"-"%m"-"%d"t"%h"-"%m"-"%s")
prints
current: 2014-09-10t15-58-15 +2 min : 2014-09-10t16-00-15
read time string , print +2min string
str="2014-09-10t15-58-15" new=$(date --date="@$(($( ifs="-t" read y m d h m s <<< "$str";date --date="$y-$m-${d}t$h:$m:$s" +%s )+120))" +"%y"-"%m"-"%d"t"%h"-"%m"-"%s") echo "from string: $str" echo "string +2m : $new"
prints
from string: 2014-09-10t15-58-15 string +2m : 2014-09-10t16-00-15
execute command "current time", others says, use:
sleep 120 ; commands...
execute command 2 minute later specified in string:
sec2date() { date --date="@$1"; } countdown() { s="$1";while (($s)) ; printf "%04d\r" $s; sleep 1; let s--; done; } str="2014-09-10t16-55-10" current=$(date +%s) stringsec=$(ifs="-t" read y m d h m s <<< "$str";date --date="$y-$m-${d}t$h:$m:$s" +%s) wanted=$(date --date="@$(($stringsec + 120))" +%s) diffsec=$(($wanted - $current)) (( $diffsec > 0 )) || { echo "can't execute in past" ; exit 1; } echo "time in string :" $(sec2date $stringsec) echo "+2min = execute @ :" $(sec2date $wanted) echo "current time :" $(date) echo "need wait :" $diffsec countdown $diffsec echo "running @ :" $(date)
prints
time in string : st sep 10 16:55:10 cest 2014 +2min = execute @ : st sep 10 16:57:10 cest 2014 current time : st sep 10 16:56:52 cest 2014 need wait : 18 running @ : st sep 10 16:57:10 cest 2014
or, use at
command. :) :)
Comments
Post a Comment