Find the answer to your Linux question:
Results 1 to 8 of 8
Enjoy an ad free experience by logging in. Not a member yet? Register.
  1. #1

    Lightbulb How to disconnect modem when postfix queue empty??


    in /etc/ppp/ip-up.d/script I have the following that runs fetchmail when the ppp0 interface is up (connected) and when it finish the modem ends internet conection automatically.

    Code:
     #!/bin/sh
     /usr/bin/fetchmail -v -f /etc/fetchmailrc -L /var/log/fetchmail.log
     killall wvdial
    this works perfectly. now i need to add to the script below the fetchmail command execution something that checks if the mail queue of postfix is completely empty and if is true then execute the command killall wvdial to hangup the modem.

    In theory i know i could do something using if, else, do, while, until, etc. but in practice i do not know how to develop it. I would like you guys to help me to program and complete this script to work properly. I appreciate the comments.

  2. #2
    I've not mucked with postfix in forever, but basically you need to figure out how to query the status of the queue (maybe by looking in /var/spool/clientmqueue or by running 'postqueue -p' and evaluating the output).

    Assuming 'postqueue -p' reports "0 Requests" if empty (I have no idea, just made that up), then you could do something like:
    Code:
    #!/bin/sh
    /usr/bin/fetchmail -v -f /etc/fetchmailrc -L /var/log/fetchmail.log
    
    # run some command to look for more jobs in queue (this is made up!) 
    postqueue -p|grep 0\ Requests 
    
    # if the above command returns 'true' (queue is empty) 
    if [ $? -eq 0 ]; then 
     
      killall wvdial
      exit 0
    
    # if the above command returned 'false' (queue is not empty) 
    else 
     
      echo Queue is not empty - not killing wvdial
     
    fi
    It may be that you want to run the queue checking code in a loop and run fetchmail until it is empty - up to you. If you do, just insert this:
    Code:
    while; do
      (above code)
      sleep 1
    done
    Again, this is just skeleton framework - you need to figure out how to check on the queue. Maybe something in that fetchmail.log has something?

  3. #3

    Thumbs up

    Ok! following you skeleton framework i get it to work perfectly in this way.

    Code:
    #!/bin/sh
    postqueue -p|grep empty
    if [ $? -eq 0 ]; then
      killall wvdial
    fi
    but when i add the while for run the queue checking code in a loop in the way that you mention it gives me unespected syntax error (my system is in spanish)
    línea 2: error sintáctico cerca del elemento inesperado `;'
    línea 2: `while; do'

    here is the code:
    Code:
    #!/bin/sh
    while; do                   #the error is on this line, is here something missed? 
    postqueue -p|grep empty
    if [ $? -eq 0 ]; then
      killall wvdial
    fi
    sleep 1
    done

  4. $spacer_open
    $spacer_close
  5. #4
    not this

    Code:
    while; do
    but this
    Code:
    while :; do
    and i reiterate - that postqueue command is surely not what you want. you'll need to figure it out.

    EDIT: did not see the top of your post about postqueue - so that works, eh? great!

    EDIT2: Krikey! just realized you were using MY original typo - sorry!
    Last edited by atreyu; 11-09-2011 at 07:23 PM. Reason: oops!

  6. #5
    Sure! the postqueue command perfectly works, i just haved to change 0\ Requests by empty cause it is part of the text that gives me the shell when is fully empty.
    if the queue is empty and i type postqueue -p it returns to me "Mail queue is empty"
    so if i place postqueue -p|grep empty the value of $? is going to be 0
    and if the queue is not empty the value of $? is 1
    after testing and knowing that i can perfectly go to compare if [ $? -eq 0 ] to then do the killall wvdial

    now i am going to fix the script adding the while how you tell me to make it loop. then i will post the results

  7. #6

    Talking I got it!

    i finally solve it! this is the final solution, it works like a charm!
    Code:
    while ! postqueue -p | grep -q empty; do
      sleep 1
    done
    killall wvdial
    so now my final script under /etc/ppp/ip-up.d is like this:

    Code:
    #!/bin/sh
    /usr/sbin/postqueue -c /etc/postfix -f
    /usr/bin/fetchmail -v -f /etc/fetchmailrc -L /var/log/fetchmail.log
    while ! postqueue -p | grep -q empty; do
      sleep 1
    done
    killall wvdial
    fetchmail could finish his job but the modem wont disconect until also the mail queue is empty!

    thanks a lot by your help @atreyu, in fact is atreyu by the hardcore band? if is it i where a big fan of them in their times!

  8. #7
    thanks a lot by your help @atreyu, in fact is atreyu by the hardcore band? if is it i where a big fan of them in their times!
    ˇNo problemo!

    ~atreyu

  9. #8
    Ohhh by the film... jajaj great film also

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •