2

I need to send email using postfix as server once wifi has received IP address (based on dhcpcd). It seems that systemd by default boot postfix before wifi (wlan0) is ready. The consequence associated with this is that postfix failed to run in background and email can not be sent, successfully. However, if I run the following script (namely abc.sh) on boot, email can definitely get through:

sleep 60
systemctl restart postfix
echo 'email content' | mail -s 'titile' mailaccount@gmail.com 

The service file associated with abc.sh is given below:

[Unit]
Description=pptp_setup
Requires=network-online.target
After=network-online.target

[Service]
Type=forking
ExecStart=/home/pi/script/abc.sh

[Install]
WantedBy=multi-user.target 

Note that sleep 60 and systemctl restart postfix is the trick to ensure the email is sent. if only sleep 60 is applied without restarting postfix, the email still can not send through as postfix is not booted properly initially ( as mentioned above, it may get loaded before receiving an IP). Also If only postfix is restarted without sleep for 60 seconds, the postfix reboot still would still fail, even Requires=network-online.target and After=network-online.target have been applied in the service file.

My question is how to set postfix correctly so that it gets booted after wifi has received ip address.

1

It is ever-green problem with systemd, as discussed many times on many places. With "slow network" activation, your best chance is probably IP_FREEBIND. You can't modify that for postfix-only, but system-wide using:

echo "1" > /proc/sys/net/ipv4/ip_nonlocal_bind

under the root account. It is well-described in manual page for ip(7).

  • systemd certainly has its flaws, but this particular issue isn't entirely its fault. What's going wrong here is that the unit file that handles setting up networking returns 'all done!' before it's actually finished. If someone using sysvinit with a networking script that backgrounded the call to dhcpcd you could potentially run into the same problem if it got to the postfix launch before dhcpcd actually finished; it'd just be less likely because of the linear boot sequence. – Shadur May 3 '16 at 8:10
1

I experienced the same issues when deploying Ubuntu 16.10 to a new MiniPC (opposed to Ubuntu 14.x that worked fine).

I have finally found an automated solution for this: enable the NetworkManager-wait-online.service and deploy a custom script that restarts the postfix service +-5 minutes after the machine has booted (assuming that the wireless connection is active by then).

A. Enable this. It is a generic approach that might be beneficial for other service than Postfix as well so I keep it in the script. systemctl enable NetworkManager-wait-online.service; systemctl status NetworkManager-wait-online.service;

B. Add a customer Systemd timer + service @info The timer will start ONCE, {x} minutes after the machine was booted. nano /etc/systemd/system/mjd-restart-postfix-after-wlan-connected.timer [Unit] Description=(timer)mjd-restart-postfix-after-wlan-connected [Timer] OnBootSec=5min [Install] WantedBy=timers.target nano /etc/systemd/system/mjd-restart-postfix-after-wlan-connected.service [Unit] Description=mjd-restart-postfix-after-wlan-connected [Service] Type=oneshot ExecStart=/bin/sh -ec "systemctl restart postfix; systemctl status postfix; uname -a | /usr/bin/mailx -s \"Server (`hostname`): postfix was restarted.\" youremail@gmail.com"

MYUNIT=mjd-restart-postfix-after-wlan-connected MYTIMER=${MYUNIT}.timer systemctl enable ${MYTIMER}; systemctl status ${MYTIMER}; systemctl list-units --all | grep "${MYUNIT}" systemctl status ${MYUNIT}

C. Restart reboot # wait 5 minutes

D. Check MYUNIT=mjd-restart-postfix-after-wlan-connected MYTIMER=${MYUNIT}.timer systemctl status ${MYTIMER}

If the contents of these 2 config files (one of Resolve and one of Postfix) are not the same then the issue is PROBLEMATIC (Postfix started before LAN/WLAN gets connected). cat /etc/resolv.conf cat /var/spool/postfix/etc/resolv.conf

E. Info @doc https://bugs.launchpad.net/ubuntu/+source/postfix/+bug/1519331 @doc https://wiki.archlinux.org/index.php/Systemd/Timers @doc qshape deferred @doc cat /var/log/syslog | egrep "NetworkManager|postfix"

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.