Server Fault is a question and answer site for system and network administrators. It's 100% free, no registration required.

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

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.

postqueue -p|grep empty
if [ $? -eq 0 ]; then
  killall wvdial

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.

share|improve this question

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

This question has been asked before and already has an answer. If those answers do not fully address your question, please ask a new question.

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

Is this what you're looking for?

while postqueue -p | grep -q empty; do
    killall wvdial
    sleep 1
share|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

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
killall wvdial

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

share|improve this answer

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