HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials

HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials (http://www.howtoforge.com/forums/index.php)
-   Programming/Scripts (http://www.howtoforge.com/forums/forumdisplay.php?f=7)
-   -   How to disconnect modem when postfix queue empty?? (http://www.howtoforge.com/forums/showthread.php?t=54823)

nerdhacker 7th November 2011 18: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.

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.

nbhadauria 9th November 2011 12:06

use mailq cammand like this..

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

Mark_NL 9th November 2011 13: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

so:

Code:

#!/bin/bash
/usr/bin/fetchmail -v -f /etc/fetchmailrc -L /var/log/fetchmail.log
/script/location/checkqueue.sh &

checkqueue.sh:
Code:

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

exit 0;

checkqueue.sh 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 00: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
Code:

#!/bin/sh

echo "Checking if exist internet connection"

ping -c 3 www.google.com

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

  done

  echo "Terminating the connection"

  killall wvdial

fi

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 13:51

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


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

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