You are not logged in.

#1 2016-02-20 12:18:22

Est
Member
Registered: 2015-08-16
Posts: 28

[SOLVED]Re-execute a command by a given time interval in bash

Hello,

Is there a way to Re-execute a command by a given time interval in bash?

I want Weather to be re-executed every 30 min.

here is example of my script:

#!/bin/bash

#Show time, date and clickable calendar
clock() {
    curtime=`date '+%I:%M'` 
    curdate=`date '+%d-%m-%Y'`
    echo "%{A:/usr/bin/gsimplecal:}%{T2} %{T1}$curdate%{A} %{T2} %{T1}$curtime"
}


volume1() {
    amixer get Master | sed -n 'N;s/^.*\[\([0-9]\+%\).*$/\1/p'
 }

#weather
weather(){
  curl -s "$(curl -s 'http://www.wunderground.com/' | sed -n '/Full Forecast/{s#[^"]*"\([^"]*\)".*#http://www.wunderground.com\1#p}')" | sed -n '/"curCond"/{ s#.*wx-value[^>]*>\([^<]*\)<.*#\1#; h }; /"curTemp"/{ N; N; N; s#.*wx-value[^>]*>\([^<]*\)<.*wx-unit[^>]*>&deg;\(.\).*#\1°\2 #; G; s#\n##; p }'
    echo $weather
    }

while :; do

echo "%{B#DDecf0f1}%{F#2c2c2c}%{T2}%{l}    $(groups)    %{T1}$(curwin) %{r}%{F#2C2C2C}  $(weather) $(space)|$(space) VOL: $(volume1) $(space)|$(space) $(uptime)  $(space)|$(space)   $(clock) %{A2}$(curwin)"

sleep 1 #sleep of mainloop
    
done

Last edited by Est (2016-02-20 16:24:46)

Offline

#2 2016-02-20 12:48:02

graysky
Wiki Maintainer
From: :wq
Registered: 2008-12-01
Posts: 10,600
Website

Re: [SOLVED]Re-execute a command by a given time interval in bash

What's the name of your script?  Let's say it's ~/bin/foo ... just use cron:

crontab -e

#     *     *   *    *        command to be executed
#-     -     -   -    -
#|     |     |   |    |
#|     |     |   |    +----- day of week (0 - 6) (Sunday=0)
#|     |     |   +------- month (1 - 12)
#|     |     +--------- day of month (1 - 31)
#|     +----------- hour (0 - 23)
#+------------- min (0 - 59)

*/30 * * * *  ~/bin/foo

Last edited by graysky (2016-02-20 12:48:24)


CPU-optimized Linux-ck packages @ Repo-ck  • AUR packagesZsh and other configs

Offline

#3 2016-02-20 13:15:46

Est
Member
Registered: 2015-08-16
Posts: 28

Re: [SOLVED]Re-execute a command by a given time interval in bash

graysky wrote:

What's the name of your script?  Let's say it's ~/bin/foo ... just use cron:

crontab -e

#     *     *   *    *        command to be executed
#-     -     -   -    -
#|     |     |   |    |
#|     |     |   |    +----- day of week (0 - 6) (Sunday=0)
#|     |     |   +------- month (1 - 12)
#|     |     +--------- day of month (1 - 31)
#|     +----------- hour (0 - 23)
#+------------- min (0 - 59)

*/30 * * * *  ~/bin/foo

panel.sh I'm using it for lemonbar.

cron will re-execute whole script? I want to re-execute only "weather" in my script. smile

Last edited by Est (2016-02-20 13:29:29)

Offline

#4 2016-02-20 13:28:20

Raynman
Member
Registered: 2011-10-22
Posts: 1,539

Re: [SOLVED]Re-execute a command by a given time interval in bash

Just use a counter variable. Increase/decrease the counter in weather(), counting up to/down from 30; then you reset the counter and run curl to refresh the weather data. In the other 29 executions you simply reprint the data you extracted during the last curl run.

Pseudo code:

weather_interval = 30
counter = weather_interval - 1  // to get data the first time weather() is executed
weather() {
    increment counter
    if counter == weather_interval {
        get new weather data
        counter = 0
    }
    print weather data
}

Offline

#5 2016-02-20 16:25:18

Est
Member
Registered: 2015-08-16
Posts: 28

Re: [SOLVED]Re-execute a command by a given time interval in bash

@Raynman thanks a lot.

Offline

Board footer

Powered by FluxBB