Linux定时任务之我见
执行:crontab -e
或者是编辑:/var/spool/cron/root 文件(root为当前登录用户对应的文件)
*/30 * * * * /home/monitor.sh ~ ~
为30分钟执行一次,monitor.sh内容为:
date >>/home/monitor.log free >>/home/monitor.log uptime >>/home/monitor.log iostat >>/home/monitor.log
结果在monitor.log中:
Thu Jan 8 09:00:01 CST 2015 total used free shared buffers cached Mem: 32831320 2839336 29991984 0 1121596 548744 -/+ buffers/cache: 1168996 31662324 Swap: 16383992 0 16383992 09:00:01 up 16:29, 2 users, load average: 0.49, 0.41, 0.28 Linux 2.6.32-279.el6.x86_64 (master) 01/08/15 _x86_64_ (8 CPU) avg-cpu: %user %nice %system %iowait %steal %idle 0.30 0.00 0.39 1.07 0.00 98.25 Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn sda 1.22 26.48 9.89 1572226 587244 sdg 0.04 2.97 0.29 176522 17048 sdi 0.03 2.95 0.28 175010 16896 sdj 0.03 2.98 0.29 177098 17000 sdh 0.03 2.95 0.29 175082 16992 sdb 0.04 2.64 0.20 156634 12120 sdd 0.03 2.93 0.29 174266 17024 sde 0.03 2.98 0.29 176730 16984 sdf 0.03 2.98 0.29 177250 17024 sdc 0.04 3.02 0.30 179570 17976 Thu Jan 8 09:30:01 CST 2015 total used free shared buffers cached Mem: 32831320 2847472 29983848 0 1123416 548828 -/+ buffers/cache: 1175228 31656092 Swap: 16383992 0 16383992 09:30:01 up 16:59, 4 users, load average: 0.07, 0.08, 0.09 Linux 2.6.32-279.el6.x86_64 (master) 01/08/15 _x86_64_ (8 CPU) avg-cpu: %user %nice %system %iowait %steal %idle 0.29 0.00 0.38 1.08 0.00 98.25 Device: tps Blk_read/s Blk_wrtn/s Blk_read Blk_wrtn sda 1.20 25.70 9.78 1572226 598172 sdg 0.03 2.89 0.28 176522 17048 sdi 0.03 2.86 0.28 175010 16896 sdj 0.03 2.89 0.28 177098 17000 sdh 0.03 2.86 0.28 175082 16992 sdb 0.04 2.56 0.20 156634 12120 sdd 0.03 2.85 0.28 174266 17024 sde 0.03 2.89 0.28 176730 16984 sdf 0.03 2.90 0.28 177250 17024 sdc 0.03 2.93 0.29 179570 17976
结果生成的文件名字上有个?号的解决办法:
经过排查,知道因为我系用记事本写sh脚本,换行时和linux的换行符唔同,导致linux编译唔到笔记本换行符
用UE这个编辑器就可以编辑linux命令。选择文件,转换,在里面选doc-unix就行了!
修正版的 monitor-updated.sh:
#! /bin/bash #Purpose: Save System status DATE=$(date +%Y%m%d) SYSTEM_TIME=$(date '+%Y-%m-%d %T') FILE_NAME="Monitor""$DATE"".log" date >>/home/$FILE_NAME free >>/home/$FILE_NAME uptime >>/home/$FILE_NAME iostat >>/home/$FILE_NAME echo "==================EXECUTE Clear..."$SYSTEM_TIME"==================" >>/home/$FILE_NAME echo 1 > /proc/sys/vm/drop_caches echo "" >>/home/$FILE_NAME FILE_NAME="Monitor""$DATE"".log" date >>/home/$FILE_NAME free >>/home/$FILE_NAME uptime >>/home/$FILE_NAME iostat >>/home/$FILE_NAME echo "==================Done Clear. "$SYSTEM_TIME"======================" >>/home/$FILE_NAME echo "" >>/home/$FILE_NAME
重新载入配置 :
/sbin/service crond reload
重启服务:
/sbin/service crond restart
相关推荐
farwang 2020-11-25
星愿心愿 2020-11-24
tianhuak 2020-11-24
zhjn0 2020-11-24
昭君出塞 2020-11-23
bluecarrot 2020-11-23
linuxwcj 2020-10-21
以梦为马不负韶华 2020-10-20
彼岸随笔 2020-10-20
yutou0 2020-10-17
applecarelte 2020-10-16
ourtimes 2020-10-16
waterhorse 2020-09-19
MRFENGG 2020-11-11
rainandtear 2020-10-30
kyssfanhui 2020-10-20
liuhangtiant 2020-10-20