Possible Duplicate:
Disconnect modem when postfix queue empty

This script code checks to see if the postfix mail queue empty and if true it disconnects my modem.

#!/bin/sh
postqueue -p|grep empty
if [ $? -eq 0 ]; then
  killall wvdial
fi

I need to run it many times as needed until the mail queue gets empty.

How can I loop it?

Maybe placing an else that returns to execute the script from the line of postqueue -p|grep empty ?

I know it can be simple but am not too much expert in bash scripting so I need help to complete this code.

link|improve this question

feedback

closed as exact duplicate by mailq, Chris S, Bart De Vos, splattne Nov 10 '11 at 19:36

This question covers exactly the same ground as earlier questions on this topic; its answers may be merged with another identical question. See the FAQ for guidance on how to improve it.

3 Answers

up vote 4 down vote accepted
#!/bin/bash
while ! postqueue -p | grep -q empty; do
    sleep 1
done
killall wvdial
link|improve this answer
wow!! just what i needed! it works like a charm! – nerdhacker Nov 9 '11 at 20:13
feedback

Is this what you're looking for?

while postqueue -p | grep -q empty; do
    killall wvdial
    sleep 1
done
link|improve this answer
Yes Kevin, this seems to be very similar to the answer of Juaco, both of your codes fix my script, now it works perfectly as i want. thanks! – nerdhacker Nov 9 '11 at 20:15
feedback

This code works even better (as it is faster on large queues):

while [ `find /var/spool/postfix/{deferred,active,maildrop}/ -type f | wc -l` -gt 0 ]; do
    sleep 5
done
killall wvdial

And note that this question is a duplicate of your own question.

link|improve this answer
feedback

Not the answer you're looking for? Browse other questions tagged or ask your own question.