1. 查看当前有哪些cron job
crontab -l
2. 写一个shell脚本
下面这个脚本例子
#!/bin/sh
myFile="/opt/mem_report.txt"
## 如果文件不存在就创建一个
if [ ! -f "$myFile" ]; then
touch "$myFile"
fi
##获取该文件的第一行数据
first=$(cat /opt/mem_report.txt| awk '{print $1}'|sed -n '1,1p')
##获取前进程的memory的相关信息
values=$(free -k | grep ^M | awk -F' ' '{print $1"\t"$2"\t"$3"\t"$4 }' && ps aux | egrep 'PID|apiService' | grep -v -e grep -e CPU | awk -F' ' '{print $4"\t"$5"\t"$6"\t"strftime("%m/%d/%Y %H:%M:%S", systime())}')
##将内存的信息存到文件中
if [ $first == "memory" ]
then
echo $values >> /opt/mem_report.txt
else
free -k | grep total | awk -F' ' '{print "memory \t" $1"\t"$2"\t"$3"\t %MEM \t VSZ \t RSS \t timestamp"}' >> /opt/mem_report.txt
echo $values >> /opt/mem_report.txt
fi
3. 用cron job定时去执行这个文件
crontab -e (进入cron job 编辑模式)
0,15,30,45 * * * * /usr/local/bin/memory.sh >/dev/null 2>&1
4. cron 表达式
* * * * * command to be executed - - - - - | | | | | | | | | ----- Day of week (0 - 7) (Sunday=0 or 7) | | | ------- Month (1 - 12) | | --------- Day of month (1 - 31) | ----------- Hour (0 - 23) ------------- Minute (0 - 59)