Linux每天自动重启Web服务器

在Aliyun最小配置VPS上跑了一个Wordpress,最近Mysql总是dead,推测原因是内存不足。因为用的是Nginx+Mysql+PHP,自然要用php-fpm服务,而php-fpm很是吃内存,所以mysql经常会死。

一、Mysql错误日志

141126 20:07:04 mysqld_safe Number of processes running now: 0141126 20:07:04 mysqld_safe mysqld restarted141126 20:07:06 [Note] Plugin 'FEDERATED' is disabled.141126 20:07:06 InnoDB: The InnoDB memory heap is disabled141126 20:07:06 InnoDB: Mutexes and rw_locks use GCC atomic builtins141126 20:07:06 InnoDB: Compressed tables use zlib 1.2.3141126 20:07:06 InnoDB: Using Linux native AIO141126 20:07:06 InnoDB: Initializing buffer pool, size = 128.0MInnoDB: mmap(137363456 bytes) failed; errno 12141126 20:07:06 InnoDB: Completed initialization of buffer pool141126 20:07:06 InnoDB: Fatal error: cannot allocate memory for the buffer pool141126 20:07:06 [ERROR] Plugin 'InnoDB' init function returned error.141126 20:07:06 [ERROR] Plugin 'InnoDB' registration as a STORAGE ENGINE failed.141126 20:07:06 [ERROR] Unknown/unsupported storage engine: InnoDB141126 20:07:06 [ERROR] Aborting141126 20:07:06 [Note] /*/*/*: Shutdown complete

这段日志说明Mysql启动时内存不够用了,推测原因是php-fpm把内存吃没了。想了很久,也Google了很久,有大神建议把mysql的innodb_buffer_pool_size设的小一些,遂把innodb_buffer_pool_size设成了64M,默认是128M。但这始终不是好的办法,因为只要机器开着,内存久会越来越小。最终决定定期重启服务器(Nginx、Mysql、php-fpm)。

二、Crontab

Crontab是Unix和Unix-like系统下执行周期性任务的命令。

1、安装Ubuntu

sudo apt-get install cronie

CentOS

sudo yum install cronie

2、查看已经存在的周期性任务

crontab -l

3、编辑或新增周期性任务

crontab -e

这个命令会打开系统的默认编辑器(vi or pico),在这里你可以修改或添加你想要执行的周期性任务。一个任务写一行,格式如下:

* * * * *  username run-parts command >> output.log

*号代表时间,依次是 分、时、日、月、周。比如我要在每天的2点重启mysql服务可以这样写

00 2 * * * root run-parts service nginx restart >> /var/log/crontab/restart.log

参考:http://kvz.io/blog/2007/07/29/schedule-tasks-on-linux-using-crontab/

本文地址:http://www.sundabao.com/linux%E6%AF%8F%E5%A4%A9%E8%87%AA%E5%8A%A8%E9%87%8D%E5%90%AFweb%E6%9C%8D%E5%8A%A1%E5%99%A8/

Linux每天自动重启Web服务器

相关文章:

你感兴趣的文章:

标签云: