You are not logged in.
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[^>]*>°\(.\).*#\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
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 packages • Zsh and other configs
Offline
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.
Last edited by Est (2016-02-20 13:29:29)
Offline
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
@Raynman thanks a lot.
Offline