Reputation: 83
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
Upvotes: 1
Views: 326
Reputation: 3474
I suggest you to use cron to run your script periodically.
If you only want to run a specific function from your script ( e.g. weather()
), add a switch statement to your script in which you'll perform a check if a certain option was provided, for example -w
for executing the weather()
function.
When you edit crontab to run every 30 minutes ( i.e. 0,30 * * * * my_script
), provide -w
to your script in order to execute weather()
only - 0,30 * * * * my_script -w
Upvotes: 1