HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials

HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials (
-   Programming/Scripts (
-   -   How to disconnect modem when postfix queue empty?? (

nerdhacker 7th November 2011 19:08

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.


 /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.

nbhadauria 9th November 2011 13:06

use mailq cammand like this..

QU=`mailq |grep 'Total requests'|sed 's/Total requests: //'`
if [ "$QU" -ne "0" ]

Mark_NL 9th November 2011 14:41

When you connect, you run fetchmail and kill, but you want only to kill if the queue is empty .. this will only be checked once .. if the queue at that point is not empty, the connection will stay open indefinitly, not what you want i think. :)

maybe you should fork another script after fetchmail. That script will keep checking the mailqueue and when it's empty, close ppp0



/usr/bin/fetchmail -v -f /etc/fetchmailrc -L /var/log/fetchmail.log
/script/location/ &

while ( true )
 SIZE=`find /var/spool/postfix/{deferred,active,maildrop}/ -type f | wc -l`;
 if [ $SIZE -eq 0 ];
  killall wvdial;
 sleep 10;

exit 0; will check every 10 seconds if there are still mails in the queue left to be send, if none, execute killall, else wait 10seconds and check again.

I used find, since it's much faster then mailq when you have a lot of mail in the queue.

nerdhacker 19th November 2011 01:59

Thanks Mark, very usefull your script, i already fix mine with something similar that you post

the script looks now like this and works just as i want


echo "Checking if exist internet connection"

ping -c 3

if [ $? -eq 0 ]; then

  echo "Starting to send & download email"

  echo "Flushing mail queue"

  /usr/sbin/postqueue -c /etc/postfix -f

  echo "Starting fetchmail"

  /usr/bin/fetchmail -v -f /etc/fetchmailrc -L /var/log/fetchmail.log

  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


  echo "Terminating the connection"

  killall wvdial


echo "Internet connection not found"

now i have another issue. as this script runs automatically when ppp0 comes up and not manually or by a cron job, how can i see the output of this script when it runs? may be logging it to some file under /var/log or there is some other way to see it when it is running? how this can be done?

Mark_NL 19th November 2011 14:51

in your cron entry put: "> /var/log/myscript.log" behind it

All times are GMT +2. The time now is 22:28.

Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2014, vBulletin Solutions, Inc.