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.
use mailq cammand like this.. QU=`mailq |grep 'Total requests'|sed 's/Total requests: //'` if [ "$QU" -ne "0" ]
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.
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?