返回

从根源上理解sendmail被持续唤醒的原因

见解分享

crond 不断唤醒 sendmail 导致资源耗尽的排查

在服务器管理中,我们经常会遇到一些棘手的性能问题,需要进行深入的调查和排查。本文将分享一个实际案例,讲述了如何排查和解决因 crond 不断唤醒 sendmail 而导致的资源耗尽问题。

问题

在春节值班期间,收到了反馈,两台服务器 c3-im-msg-tun001.bj 和 c3-im-msg-tun002.bj 的资源使用异常,主要是内存和磁盘占用过高。监控图表显示,资源占用突然下降,这是手动清理进程和文件的结果,但这种方法治标不治本。

初步排查

首先想到的是 crond 任务,因为它是计划任务的守护进程。于是,使用 ps 命令查看正在运行的 crond 任务。

[root@c3-im-msg-tun002 ~]# ps -ef | grep -v grep | grep cron
root       26152     1  0 05:00 ?        00:00:00 /usr/sbin/crond -n
root       26153 26152  0 05:00 ?        00:00:00 /usr/sbin/sendmail -t -i
root       26154 26152  0 05:00 ?        00:00:00 (sd-pam)
root       26156 26152  0 05:00 ?        00:00:00 /usr/sbin/sendmail -t -i
root       26157 26152  0 05:00 ?        00:00:00 (sd-pam)
root       26158 26152  0 05:00 ?        00:00:00 /usr/sbin/sendmail -t -i
root       26159 26152  0 05:00 ?        00:00:00 (sd-pam)

发现第一个 crond 任务正在运行 sendmail,这显然是不正常的。于是,果断地将 crontab 中的这个任务注释掉。

0 5 * * * /usr/sbin/sendmail -t -i

注释掉任务后,再次查看 sendmail 进程,发现仍然有许多。

[root@c3-im-msg-tun002 ~]# ps -ef | grep sendmail | grep -v grep
root      22413 22410  0 05:32 ?        00:00:00 /usr/sbin/sendmail -t -i
postfix   23770 22410  0 05:32 ?        00:00:00 pickup -l -t unix -u
postfix   23772 22410  0 05:32 ?        00:00:00 qmgr -l -t unix
root      23773 22410  0 05:32 ?        00:00:00 sendmail: child
postfix   23774 23770  0 05:32 ?        00:00:00 qmgr -l -t unix
postfix   23776 22410  0 05:32 ?        00:00:00 sendmail: child
root      23777 22410  0 05:32 ?        00:00:00 sendmail: child
root      23779 22410  0 05:32 ?        00:00:00 sendmail: child
postfix   23780 23770  0 05:32 ?        00:00:00 qmgr -l -t unix
root      23781 22410  0 05:32 ?        00:00:00 sendmail: child
root      23782 22410  0 05:32 ?        00:00:00 sendmail: child
postfix   23783 23770  0 05:32 ?        00:00:00 qmgr -l -t unix
postfix   23784 23770  0 05:32 ?        00:00:00 qmgr -l -t unix
root      23785 22410  0 05:32 ?        00:00:00 sendmail: child
root      23786 22410  0 05:32 ?        00:00:00 sendmail: child
postfix   23787 23770  0 05:32 ?        00:00:00 qmgr -l -t unix
postfix   23788 23770  0 05:32 ?        00:00:00 qmgr -l -t unix
postfix   23789 23770  0 05:32 ?        00:00:00 qmgr -l -t unix
postfix   23790 23770  0 05:32 ?        00:00:00 qmgr -l -t unix
postfix   23791 23770  0 05:32 ?        00:00:00 qmgr -l -t unix

咦,这是什么情况?怎么会有这么多 sendmail 进程?带着这个疑问,去网上查了一下。原来,sendmail 是 postfix 的一个插件,用来发送电子邮件,当 postfix 启动时,它会自动加载 sendmail 插件。因此,当 postfix 运行时,sendmail 也就会运行。

现在问题就清楚了,只要把 postfix 停止,sendmail 就会停止。

[root@c3-im-msg-tun002 ~]# service postfix stop
Redirecting to /bin/systemctl stop postfix.service

再次查看 sendmail 进程,果然没有了。

[root@c3-im-msg-tun002 ~]# ps -ef | grep sendmail | grep -v grep

这样问题就解决了,但是我们还要找到问题的根源,为什么 postfix 会自动启动?

去网上查了一下,原来 postfix 是在系统启动时自动启动的,这是由 systemctl 决定的。

[root@c3-im-msg-tun002 ~]# systemctl list-unit-files | grep postfix
postfix.service                               enabled

现在我们只要把 postfix 禁用,问题就彻底解决了。

[root@c3-im-msg-tun002 ~]# systemctl disable postfix

再次查看 postfix 的状态,果然是禁用了。

[root@c3-im-msg-tun002 ~]# systemctl status postfix
 postfix.service - LSB: Postfix Mail Transport Agent
   Loaded: disabled (/etc/rc.d/init.d/postfix)
   Active: inactive (dead)

至此,问题彻底解决。

总结

通过对日志的分析和进一步的调查,我们发现 crond 不断唤醒 sendmail 是由于 postfix 的自动启动造成的。通过停止 postfix 并将其禁用,我们解决了资源耗尽的问题。

常见问题解答

  1. 为什么 crond 会唤醒 sendmail?
    crond 是计划任务的守护进程,它根据 crontab 文件中配置的任务计划运行命令或脚本。在本例中,有人在 crontab 中配置了运行 sendmail 的任务,导致 sendmail 被不断唤醒。
  2. sendmail 和 postfix 之间有什么关系?
    sendmail 是一个邮件传输代理 (MTA),负责发送电子邮件。postfix 也是一个 MTA,但它是更现代的替代品。sendmail 是 postfix 的一个插件,当 postfix 启动时,它会自动加载 sendmail 插件。
  3. 为什么 postfix 会在系统启动时自动启动?
    systemctl 负责在系统启动时启动和停止服务。postfix.service 单元文件被配置为在系统启动时启动 postfix 服务,因此它会在系统启动时自动启动。
  4. 如何停止 postfix 服务?
    可以通过多种方式停止 postfix 服务,包括使用 service 命令、systemctl 命令或 init.d 脚本。在本例中,使用了 service postfix stop 命令来停止服务。
  5. 如何禁用 postfix 服务?
    可以使用 systemctl disable postfix 命令来禁用 postfix 服务。这将防止服务在系统启动时自动启动