#!/bin/sh
#=========================================================================
# Daemon to Relax the Mail processes
#
# Start/Stop some Mail processes depending on Mail client activity:
# 1) external Mail client: imap is running
# 2) webmail (roundcube) : "dovecot-auth -w" remains running
#
# This will allow your Syno to sleep during the quiet hours
#-------------------------------------------------------------------------
# 03may12 EB First version
# 19apr13 EB Changed into standard DSM 3rd party app
# 08jun13 EB POP3FetchControl() changed, check on POP3Fetch_enabled
# 11aug14 EB Aligned with Mail Server 1.3-0215, minor change:
# POP3FetchControl() changed, changed POP3Fetch_enabled
# 08nov14 EB Alignment with Mail Station 20141031-0213:
# - WebMailConf: changed main.inc.php into config.inc.php
# - POP3FetchControl(): POP3Fetch_enabled and popusers
# main changed: try max 3 times to start or stop processes
# main changed: check on auth instead of dovecot-auth
# No changes needed for Mail Server 1.4-0281
# 16dec16 OH Aligned with DSM6, Mail Server 1.6.1-0484
# - Basic path infos changed
# - Sed syntax changed to match current version
#=========================================================================
LOGINFO="/usr/bin/logger -sp user.warn -t relax INFO: "
LOGERROR="/usr/bin/logger -sp user.error -t relax ERROR: "
#=========================================================================
# Start/Stop Fetchmail
# Based on: /var/packages/MailStation/scripts/start-stop-status
#=========================================================================
WebMailDir="/var/packages/MailStation/target"
WebMailExt=${WebMailDir}/mail/ext
WebMailConf=${WebMailDir}/mail/config/config.inc.php
FetchBin=${WebMailDir}/bin/synofetch
POP3FetchControl() # argument: -1=Start|-2=Stop
{
if [ "$1" = "-1" ]; then
POP3Fetch_enabled=`grep -E "config.*extmailallow" ${WebMailConf} | tr -d ' ' | cut -d '=' -f2`
if [ "$POP3Fetch_enabled" != "true;" ]; then
# Fetching with POP3 is not enabled
return
fi
popusers=`/usr/bin/find ${WebMailExt} -name "*_fetch" | /usr/bin/cut -d'/' -f8 | sed 's/.\{6\}$//'`
# To check if sed's syntax matches the sed version on your system, you may activate
# $LOGINFO "PopUser is \"$popusers\""
# The log entry in /var/log/messages should contain those users fetching mail via POP3
for popusr in $popusers
do
if [ -e ${WebMailExt}/${popusr}_fetch ]; then
${FetchBin} ${popusr} $1
#else No fetch configured, skip
fi
done
else
# Since Mail Station 20130129-0058 this command is used to stop
# (previously, the for loop above was used for stop as well)
killall fetchmail
fi
}
#=========================================================================
# Start/Stop Postfix
# Based on: /var/packages/MailServer/scripts/start-stop-status
# ...and: /var/packages/MailServer/target/scripts/PostfixDaemon.sh
# Note that only the daemon is started/stopped here.
#=========================================================================
POSTFIX=/var/packages/MailServer/target/sbin/postfix
MAILCONF="/var/packages/MailServer/etc/mailserver.conf"
PostfixControl() # argument: start|stop
{
SmtpEnable=`/bin/get_key_value $MAILCONF smtp_enabled`
if [ "$SmtpEnable" = "yes" ]; then
$POSTFIX $1
fi
}
#*************************************************************************
# Main
#*************************************************************************
STOP_FLAG=$0_stop_flag
case "$1" in
boot_start)
rm -f $STOP_FLAG
$LOGINFO "System boot process requested to start daemon=\"$0\", first sleep to ensure proper boot completion"
sleep 60
$LOGINFO "The daemon=\"$0\" is awake now"
;;
manual_start)
rm -f $STOP_FLAG
$LOGINFO "Manual request to start daemon=\"$0\""
;;
stop)
touch $STOP_FLAG
$LOGINFO "Requested daemon=\"$0\" to stop gracefully"
exit 0
;;
*)
$LOGERROR "Invalid argument=\"$1\" to daemon=\"$0\""
exit 1
;;
esac
IsActiveMailClient=0
IsRunningFetchMail=0
InactiveCounter=0
RetryCounter=0
while [ ! -f $STOP_FLAG ]
do
#------------------------------------------------------------------
# Check if fetchmail is running
#------------------------------------------------------------------
if [ -n "`pidof fetchmail`" ]
then IsRunningFetchMail=1
else IsRunningFetchMail=0
fi
#------------------------------------------------------------------
# Check if mail clients are active
#------------------------------------------------------------------
if [ -n "`pidof imap`" ]; then
IsActiveMailClient=1 # external Mail client active, e.g. Outlook
else
IsActiveMailClient=0
# Look if dovecot authentication worker is active, that indicates webmail active
# With `ps` this process shows up as "dovecot/auth -w"
# (however, do not use `ps` since it wakes up Syno by reading nsswitch.conf)
for process_id in `pidof auth`
do
# Check if this is a worker process, by reading cmdline switch in /proc
grep -s -q /proc/$process_id/cmdline -i -e "-w$"
if [ $? -eq 0 ]; then
IsActiveMailClient=1 # Webmail active
fi
done
fi
if [ $IsActiveMailClient -ne $IsRunningFetchMail ]; then
if [ $IsRunningFetchMail -ne 0 ]; then
#------------------------------------------------------------------
# Fetchmail is still running while no mail clients active anymore
#------------------------------------------------------------------
if [ $InactiveCounter -gt 20 ]; then
if [ $RetryCounter -le 3 ]; then
$LOGINFO "Mail clients inactive, stopping mail processes"
POP3FetchControl -2
PostfixControl stop
let RetryCounter++
InactiveCounter=0
else
$LOGERROR "Giving up: cannot stop mail processes."
touch $STOP_FLAG
fi
else
# Check a few times first
let InactiveCounter++
fi
else
#------------------------------------------------------------------
# Mail clients are active, while mail processes are not running yet
#------------------------------------------------------------------
if [ $RetryCounter -le 3 ]; then
$LOGINFO "Mail clients active, restarting mail processes"
POP3FetchControl -1
PostfixControl start
let RetryCounter++
InactiveCounter=0
else
$LOGERROR "Giving up: cannot start mail processes."
touch $STOP_FLAG
fi
fi
else
# Mail processes in sync with Mail client demands
InactiveCounter=0
RetryCounter=0
fi
sleep 3
done
#------------------------------------------------
# Daemon is stopping gracefully
# Make sure mail processes are running again
#------------------------------------------------
if [ $IsRunningFetchMail -eq 0 ]; then
$LOGINFO "Daemon=\"$0\" is restarting the mail processes..."
POP3FetchControl -1
PostfixControl start
fi
$LOGINFO "Daemon=\"$0\" will exit now"
Wenn du das Forum hilfreich findest oder uns unterstützen möchtest, dann gib uns doch einfach einen Kaffee aus.
Als Dankeschön schalten wir deinen Account werbefrei.