You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Ubuntu 16.04 LTS开机及关机发送邮件的实现方案求助

Troubleshooting Boot/Shutdown Email Alerts on Ubuntu Desktop

Since you’ve already tried crontab @reboot, init.d scripts, and rc.local without luck, let’s dive into systemd-specific fixes and desktop-environment quirks that might be blocking your email alerts. Here are actionable troubleshooting steps:

1. Switch to Systemd Services (The Modern Ubuntu Way)

Ubuntu desktop uses systemd by default, so sysvinit-style init.d scripts can run into execution order or environment issues. Create separate services for boot and shutdown:

Boot Email Service

Create a file at /etc/systemd/system/boot-mail.service with this content:

[Unit]
Description=Send email on system boot
After=network.target network-online.target
Wants=network-online.target

[Service]
Type=oneshot
ExecStart=/path/to/your/boot-script.sh
User=root
StandardOutput=journal+console

[Install]
WantedBy=multi-user.target
  • After=network.target ensures the script runs only after the network is up (critical for sending emails)
  • Reload systemd and enable the service:
    sudo systemctl daemon-reload
    sudo systemctl enable boot-mail.service
    

Shutdown Email Service

Shutdown requires special handling because systemd terminates processes quickly. Create /etc/systemd/system/shutdown-mail.service:

[Unit]
Description=Send email on system shutdown
DefaultDependencies=no
Before=shutdown.target reboot.target halt.target

[Service]
Type=oneshot
ExecStart=/bin/true  # Dummy start command to satisfy systemd
ExecStop=/path/to/your/shutdown-script.sh
TimeoutStopSec=30  # Give enough time to send the email
User=root

[Install]
WantedBy=shutdown.target reboot.target halt.target

Enable it with the same daemon-reload and enable commands above.

Check service status for errors with:

sudo systemctl status boot-mail.service
sudo systemctl status shutdown-mail.service

2. Validate Script Execution Environment

Boot/shutdown runs in a minimal, non-interactive environment—your script might fail due to missing paths or variables:

  • Use absolute paths for all commands in your script (e.g., /usr/bin/mail instead of just mail)
  • Test the script as root in a non-interactive shell to replicate boot conditions:
    sudo -i bash -c '/path/to/your/script.sh'
    
  • Add logging to your script to capture errors:
    echo "Boot attempt at $(date):" >> /var/log/boot-shutdown-mail.log 2>&1
    /usr/bin/mail -s "System Booted" your@email.com < /dev/null >> /var/log/boot-shutdown-mail.log 2>&1
    

Check the log with tail /var/log/boot-shutdown-mail.log to spot failures.

3. Account for Ubuntu Desktop Network Delays

Desktop editions often take longer to establish a network connection than server editions. If your boot script runs before the network is ready:

  • Add a delay to your crontab @reboot entry (if you still want to use it):
    @reboot sleep 60 && /path/to/your/boot-script.sh
    
  • Or rely on systemd’s network-online.target (already included in the boot service above) to wait for a fully connected network.

4. Verify Email Configuration

Double-check that your mail setup works outside of boot/shutdown:

  • Test sending a manual email as root:
    echo "Test message" | /usr/bin/mail -s "Test Email" your@email.com
    
  • Check mail logs for errors:
    tail -f /var/log/mail.log
    

Look for entries like authentication failures or SMTP connection timeouts—these indicate a problem with your mailutils/sendmail config, not the boot/shutdown trigger.

5. Ensure rc.local is Enabled (If You Prefer It)

Modern Ubuntu disables rc.local by default. To use it:

  • Check if the service is active:
    systemctl status rc-local.service
    
  • If inactive, enable it:
    sudo systemctl enable rc-local.service
    
  • Make sure /etc/rc.local has the correct shebang (#!/bin/bash) at the top, execution permissions (chmod +x /etc/rc.local), and ends with exit 0.

内容的提问来源于stack exchange,提问作者Alex R.

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.19 03:40:23