You are not logged in.
Pages: 1
Hi,
I'd like to know how to solve my doubt.
I wrote a script for dzen2 like this:
#!/bin/sh
while true; do
func1() {......}
func2() {......}
mail_check() {?????}
echo `func1 && func2 && mail_check`
sleep 5
done | dzen2
All functions are called every 5 seconds, but I'd like to run mail_check() every 60 seconds (for example).
How can I do it?
Thank you.
Last edited by monotiz (2012-05-06 08:32:58)
Offline
You could use something like this:
#!/bin/bash
sle () {
tick=1
while true; do
sleep 10
tjek="Sleep 10 secs has run $tick times"
tick=$(($tick+1))
done
}
sle &
jpid=$!
trap 'kill -9 $jpid; exit' INT TERM EXIT
while true; do
sleep 2
echo $tjek
done
Offline
Put it in another backgrounded shell:
#!/bin/sh
while true; do
{
echo $(func1 && func2)
sleep 5
} &
{
echo $(mail_check)
sleep 60
} &
done | dzen2
Offline
Put it in another backgrounded shell:
#!/bin/sh while true; do { echo $(func1 && func2) sleep 5 } & { echo $(mail_check) sleep 60 } & done | dzen2
It doesn't work...
Offline
The problem with these examples is they each run a loop in a subshell that sets a variable but then tries to access that variable in the parent shell. AFAIK this is not possible in bash ... which is why they fail or produce no ouput - they are "echoing" an empty variable.
One work around is to have the individual while loops echo their data to a temp file rather than saving it to a variable, then have the main loop cat those files.
One possible example:
# define functions here ...
while :; do func1 > /tmp/battery; sleep 1; done
while :; do func2 > /tmp/time; sleep 30; done
while :; do func3 > /tmp/mail; sleep 60; done
while :; do echo $(cat /tmp/battery /tmp/time /tmp/mail); sleep 1; done
Alternately this is very easy to do in C, which is how I rewrote my dwm status line and saved a LOT on resources while still having a rapid update rate.
Edit: another, potentially cleaner, method would be to use a condtional inside a single loop such as
last_email_check=0
while :; do
if [ last_email_check + 60 -lt $(date +%s) ]; then
email_string=$(func3)
last_email_check = $(date +%s)
fi
# more here to set other variables from func1 and func2
echo $email_string $func1_string $func2_string
sleep 1
done
Last edited by Trilby (2012-05-06 11:56:25)
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
I can see my first example does not work.
This OTOH does:
#!/bin/bash
sle () {
tick=1
while true; do
sleep 10
echo "Sleep 10 secs has run $tick times"
tick=$(($tick+1))
export tjek=$1
done
}
sle &
jpid=$!
trap 'kill -9 $jpid; exit' INT TERM EXIT
while true; do
sleep 2
echo $tjek
done
Last edited by Ashren (2012-05-06 12:30:08)
Offline
Ashren, the main loop still does not have access to the tjek variable. It echos a blank line every two seconds as tjek is not set within the scripts scope.
This does echo the "sleep 10 sec ..." part properly, but that is only from within the sle function. This would not be useful for giving output to dzen as the output to be updated every 60 seconds would only be displayed for 1 second every 60 seconds.
I'm pretty sure the goal is to have it updated every 60s but still displayed all the time.
"UNIX is simple and coherent" - Dennis Ritchie; "GNU's Not Unix" - Richard Stallman
Offline
Hi,
I'd like to know how to solve my doubt.
I wrote a script for dzen2 like this:#!/bin/sh while true; do func1() {......} func2() {......} mail_check() {?????} echo `func1 && func2 && mail_check` sleep 5 done | dzen2
All functions are called every 5 seconds, but I'd like to run mail_check() every 60 seconds (for example).
How can I do it?Thank you.
With a bit of bash experience this can be simple. Here's an example:
#!/bin/bash
# seperate timings
int=0
# print 3 variables until sixteen seconds
# change two variables every second
# change the third every five seconds
while [ $int -lt 16 ]; do
answer1=$(printf "answer1 $int") # replace printf with function
answer2=$(printf "answer2 $int") # replace printf with function
[ $(($int%5)) == 0 ] && mail_check=$(printf "mail_check $int") # replace printf with function
printf "$answer1 $answer2 $mail_check"
sleep 1
let 'int=int+1'
done | dzen2
exit 0
HTH
You're just jealous because the voices only talk to me.
Offline
Pages: 1