Go Back   HowtoForge Forums | HowtoForge - Linux Howtos and Tutorials > Linux Forums > Programming/Scripts

Do you like HowtoForge? Please consider supporting us by becoming a subscriber.
Reply
 
Thread Tools Display Modes
  #1  
Old 7th November 2011, 18:08
nerdhacker nerdhacker is offline
Junior Member
 
Join Date: Nov 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
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.
Reply With Quote
Sponsored Links
  #2  
Old 9th November 2011, 12:06
nbhadauria nbhadauria is offline
Member
 
Join Date: Aug 2010
Location: New Delhi, India
Posts: 79
Thanks: 1
Thanked 13 Times in 13 Posts
Send a message via Skype™ to nbhadauria
Default

use mailq cammand like this..

QU=`mailq |grep 'Total requests'|sed 's/Total requests: //'`
if [ "$QU" -ne "0" ]
Reply With Quote
  #3  
Old 9th November 2011, 13:41
Mark_NL Mark_NL is offline
Senior Member
 
Join Date: Sep 2008
Location: The Netherlands
Posts: 912
Thanks: 12
Thanked 100 Times in 96 Posts
Default

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.
__________________
Real men don't backup... Real men cry!

http://www.e-rave.nl/
Reply With Quote
  #4  
Old 19th November 2011, 00:59
nerdhacker nerdhacker is offline
Junior Member
 
Join Date: Nov 2011
Posts: 6
Thanks: 0
Thanked 0 Times in 0 Posts
Default

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?
Reply With Quote
  #5  
Old 19th November 2011, 13:51
Mark_NL Mark_NL is offline
Senior Member
 
Join Date: Sep 2008
Location: The Netherlands
Posts: 912
Thanks: 12
Thanked 100 Times in 96 Posts
 
Default

in your cron entry put: "> /var/log/myscript.log" behind it
__________________
Real men don't backup... Real men cry!

http://www.e-rave.nl/
Reply With Quote
Reply

Bookmarks

Tags
bash, mailq, postfix, script, wvdial

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Postfix SMTP Auth to Dovecot Not Working -- HELP! Scratchpad Server Operation 6 12th April 2011 13:29
Unable to Receve Email walner8080 Installation/Configuration 8 30th September 2010 05:58
dovecot: lda: Fatal: Unknown user dpicella Installation/Configuration 1 22nd August 2010 02:59
ERROR: Connection dropped by IMAP server. [Centos 5.4, courier imap,squirrel, etc] darevil HOWTO-Related Questions 7 9th June 2010 14:49
Centos 4.4 32bit Hangs, High Server load 3cwired_com Server Operation 11 16th November 2006 15:47


All times are GMT +2. The time now is 06:43.


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