1

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.

nerdhacker
  • 61
  • 2
  • 5

3 Answers3

5
#!/bin/bash
while ! postqueue -p | grep -q empty; do
    sleep 1
done
killall wvdial
ata
  • 228
  • 1
  • 4
3

Is this what you're looking for?

while postqueue -p | grep -q empty; do
    killall wvdial
    sleep 1
done
Kevin
  • 338
  • 2
  • 13
  • 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 09 '11 at 20:15
2

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.

mailq
  • 17,023
  • 2
  • 37
  • 69
  • Yes, but you probably need more privileges to look in this folders (deferred,active,maildrop). But to speed the find up, you may use `-print -quit` to get only the first match `while [ \`find /var/spool/postfix/{deferred,active,maildrop}/ -type f -print -quit\` != "" ]` – Martin T. Nov 05 '20 at 09:02