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.

0

3 Answers 3

5
#!/bin/bash
while ! postqueue -p | grep -q empty; do
    sleep 1
done
killall wvdial
1
  • wow!! just what i needed! it works like a charm!
    – nerdhacker
    Nov 9, 2011 at 20:13
3

Is this what you're looking for?

while postqueue -p | grep -q empty; do
    killall wvdial
    sleep 1
done
1
  • 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, 2011 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.

1
  • 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 5, 2020 at 9:02

Not the answer you're looking for? Browse other questions tagged .