2
while ! postqueue -p | grep -q empty; do
  sleep 1
done
killall wvdial

this script checks if my mail queue is empty, then disconnects my modem. now i would like to add also a checking to fetchmail if is running. i am not sure how to develop it but i know that it can be something like the down example.

while ! postqueue -p | grep -q empty && "fetchmail process is not running"; do 

some help please?

  • 1
    Nerdhacker's postqueue week on SF. – ДМИТРИЙ МАЛИКОВ Nov 11 '11 at 20:52
  • And the next question is already in the queue ;-) – mailq Nov 11 '11 at 21:01
2
echo "Checking mail queue and fetchmail process"
while ! postqueue -p | grep -q empty && ps -C fetchmail > /dev/null; do
  echo "There is still mail in queue or fetchmail is still working"
  sleep 1
done
echo "Terminating the connection"
killall wvdial
  • Yes, and i have more questions still in queue ... ;) – nerdhacker Nov 11 '11 at 22:23
1
while [ "`find /var/spool/postfix/{deferred,active,maildrop}/ -type f | wc -l`" -gt 0 ] ||
      [ "`ps -C fetchmail -o pid= | wc -l`" -gt 0 ]; do
    sleep 5
done
killall wvdial

or even with fewer spawned processes:

while [ -n $("find /var/spool/postfix/{deferred,active,maildrop}/ -type f") ] ||
      [ -n $("ps -C fetchmail -o pid=") ]; do
    sleep 5
done
killall wvdial
  • 1
    [ "`somecommand | wc -l`" -gt 0 ] is an awfully indirect way of checking for empty output. [ -n "$(somecommand)" ] is the direct way. You can do better though, see nerdhacker's answer. Also, you should be testing postfix and fetchmail at the same time rather than never test postfix once you've started testing for fetchmail (there's still a race condition, but the window is a lot smaller). Oh, and always put double quotes around substitutions. What do you think would happen if the output contained whitespace (like, several lines)? – Gilles 'SO- stop being evil' Nov 11 '11 at 22:17
  • @mailq I've corrected what was objectively wrong. I'll leave the correct-but-suboptimal stuff up to you. – Gilles 'SO- stop being evil' Nov 11 '11 at 22:45

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy