关机和系统服务

关机和系统服务

凌晨 5 点,cron 运行关机,systemd 执行 /home/myuser/bin/mailme-shutdown.sh 并关闭我的电脑。当我启动电脑时,一封电子邮件发送给我。该电子邮件是我启动电脑之前执行的脚本 (mailme-shutodown.sh)。有什么方法可以在电脑关机前发送电子邮件?我使用本地 postfix 和外部 smtp 服务。

cat /etc/crontab
00 05   * * *   root    /sbin/shutdown -h now


[Unit]
Description=Run command at shutdown
Requires=network.target
Before=shutdown.target reboot.target halt.target

[Service]
Type=oneshot
RemainAfterExit=true
ExecStart=/home/myuser/bin/mailme-startup.sh
ExecStop=/home/myuser/bin/mailme-shutdown.sh
User=myuser

[Install]
#WantedBy=multi-user.target
WantedBy=halt.target reboot.target shutdown.target

cat mailme-shutdown.sh

[...]
[email protected]
echo "body" | mail -s "test" "$email"
[...]

答案1

也许您应该检查电子邮件是否已发送,如果当前邮件队列为空则关闭。 如何验证电子邮件是否已通过 postfix 成功发送?

相关内容