Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.
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?

share|improve this question

migrated from serverfault.com Nov 11 '11 at 21:28

This question came from our site for professional system and network administrators.

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 Answers 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
share|improve this answer
    
Yes, and i have more questions still in queue ... ;) –  nerdhacker Nov 11 '11 at 22:23
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
share|improve this answer
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 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 Nov 11 '11 at 22:45

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.