我有一个用于我的 Web 应用程序的 linux 服务器。每隔一段时间,一个进程(可能是一个长时间运行的脚本)可能会失控,可能会消耗太多内存/cpu 周期并阻塞所有其他进程。
在这种情况下,我无法通过 ssh 进入服务器,我需要通过管理面板重新启动服务器。我宁愿登录到机器并只处理有问题的过程。
是否可以在 linux 机器中安排资源,以便进程消耗资源,但始终有足够的资源可用于 ssh 连接?
您可以使用“nice”来确定某些软件的优先级。
您还可以考虑安装 monit,如果达到某个阈值,您可以指示重新启动某个包。
沿着这些行的 monit 配置将自动重新启动 Apache:
check process apache
with pidfile "/usr/local/apache/logs/httpd.pid"
start program = "/etc/init.d/httpd start" with timeout 60 seconds
stop program = "/etc/init.d/httpd stop"
if 2 restarts within 3 cycles then timeout
if totalmem > 100 Mb then alert
if children > 255 for 5 cycles then stop
if cpu usage > 95% for 3 cycles then restart
if failed port 80 protocol http then restart
group server
depends on httpd.conf, httpd.bin
Run Code Online (Sandbox Code Playgroud)