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.
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 usingsysvinit
with a networking script that backgrounded the call todhcpcd
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-don't-feed-the-AI May 03 '16 at 08:10