1.free -h
[root@gyh nginx]# free -h
total used free shared buff/cache available
Mem: 1.7G 977M 150M 680K 606M 579M
Swap: 0B 0B 0B
[root@gyh nginx]#
2.参数详解
total: 内存总数
used: 已经使用内存数
free: 完全空闲内存
shared: 多个进程共享的内存
buffers: 用于块设备数据缓冲,记录文件系统metadata(目录,权限,属性等)
cached: 用于文件内容的缓冲
available:真正剩余的可被程序应用的内存数
3.编写清除buffers/cached脚本
vim 文件名.sh
编写内容
#!/bin/bash
currtDay=$(date +%F)
echo -------------------------$currtDay缓存清理开始---------------------------
startTime=$(date +%F\ %T)
echo $startTime"开始清除缓存"
# sync;sync;sync #写入硬盘,防止数据丢失
sleep 10 #延迟10秒
echo 1 > /proc/sys/vm/drop_caches
echo 2 > /proc/sys/vm/drop_caches
echo 3 > /proc/sys/vm/drop_caches
endTime=$(date +%F\ %T)
echo $endTime"清除缓存完成"
echo -------------------------$currtDay缓存清理完成---------------------------
4.给脚本文件添加执行权限
chmod +x 文件名
5.创建定时执行
crontab -e
设置脚本执行时间(执行时间要超过当前时间3分钟),并制定输出位置、每天11:59分执行。
59 11 * * * /root/clean_cache.sh >> /work/clean_cache.log
6.执行命令让其生效
systemctl restart crond.service
7.crond常用命令
systemctl start crond 启动服务
systemctl stop crond 停止服务
systemctl restart crond 重启服务
systemctl reload crond 重载配置文件
systemctl status crond 查看状态
---------------------------------------------
crontab -u 设定某个用户的cron服务
crontab -l 显示crontab文件(显示已设置的定时任务)
crontab -e 编辑crontab文件(编辑定时任务)
crontab -r 删除crontab文件(删除定时任务)
crontab -i 删除crontab文件提醒用户(删除定时任务)
8.crontab 详解
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# For details see man 4 crontabs
# Example of job definition:
# .---------------- minute (0 - 59)
# | .------------- hour (0 - 23)
# | | .---------- day of month (1 - 31)
# | | | .------- month (1 - 12) OR jan,feb,mar,apr ...
# | | | | .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# | | | | |
# * * * * * user-name command to be executed
用户的定时任务分6段,分别是:分,时,日,月,周,命令
第1列表示分钟1~59 每分钟用*或者 */1表示
第2列表示小时1~23(0表示0点)
第3列表示日期1~31
第4列表示月份1~12
第5列标识号星期0~6(0表示星期天)
第6列要运行的命令
*:表示任意时间都,实际上就是“每”的意思。可以代表00-23小时或者00-12每月或者00-59分
-:表示区间,是一个范围,00 17-19 * * * cmd,就是每天17,18,19点的整点执行命令
,:是分割时段,30 3,19,21 * * * cmd,就是每天凌晨3和晚上19,21点的半点时刻执行命令
/n:表示分割,可以看成除法,*/5 * * * * cmd,每隔五分钟执行一次