20170405 21:58-
cron定时任务
[[email protected] ~]# chkconfig|egrep "sshd|network|sysstat|crond|rsyslog" #前面提到的crond的地方
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off
network 0:off 1:off 2:on 3:on 4:on 5:on 6:off
rsyslog 0:off 1:off 2:on 3:on 4:on 5:on 6:off
sshd 0:off 1:off 2:on 3:on 4:on 5:on 6:off
sysstat 0:off 1:on 2:on 3:on 4:on 5:on 6:off
程序文件:程序代码组成,但是没有在计算机内执行。当前没有执行。
进程:所谓进程就是计算机中正在执行的程序。
守护进程或守护程序:守护进程就是一直运行的程序。
vi /etc/logrotate.conf 设置日志切割 大小 日期等
vim /etc/logrotate.d/syslog 设置需要记录的日志文件
4、上千服务器可以开发分布式定时任务项目方案。(好一些的企业 可能会使用这种方式 )
crontab -l list列表 -u 用户 -e edit 编辑 -r remove 删除 -i 确认 -s 和selinux相关
[[email protected] ~]# cat /etc/crontab
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
HOME=/
# 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
分时 日月周 用户 命令
实例:
我自己的错误示范:
crontab -e
i
# 00 */2 * * * cp -a /etc/service /tmp/{date +%F-%H-%M-%S}/
:wq
# 00 24 * * * cp -a /etc/service /tmp/{date +%F-%H-%M-%S}/
tar zcvf 目标文件 源文件 #打包压缩 v显示输出过程
1、命令行搞定:
[[email protected] etc]# tar zcvf /tmp/service_$(date +%F-%H).tar.gz ./service
./service
[[email protected] etc]# ll /tmp/service_2017-04-06-22.tar.gz
-rw-r--r-- 1 root root 114 Apr 6 22:06 /tmp/service_2017-04-06-22.tar.gz
2、定时任务最好用脚本
3、取消默认输出 tar zcf 不要v
4、使用/bin/sh执行脚本
5、定时任务 重定向到空 >/dev/null 2>&1
[[email protected] scripts]# cat tar.sh
cd /etc &&\ # &&\ 这个叫做逻辑符 意思是本行成功了就执行下面的打包命令
tar zcf /tmp/service_$(date +%F-%H).tar.gz ./service
[[email protected] scripts]# /bin/sh /service/scripts/tar.sh #养成这个好习惯 用/bin/sh 即使文件没有执行权限也能运行
[[email protected] scripts]# ll tar.sh
-rw-r--r-- 1 root root 65 Apr 6 22:19 tar.sh
[[email protected] scripts]# crontab -e
#tar by 20has at 20170406
00 */2 * * * /bin/sh /service/scripts/tar.sh >/dev/null 2>&1
"/tmp/crontab.uipXEM" 2L, 87C written
crontab: installing new crontab
[[email protected] scripts]# crontab -l
#tar by 20has at 20170406
00 */2 * * * /bin/sh /service/scripts/tar.sh >/dev/null 2>&1
[[email protected] scripts]# tailf /var/log/cron #检查cron日志 看cron是否执行成功
Apr 6 22:13:49 centos crontab[2443]: (root) LIST (root)
Apr 6 22:13:57 centos crontab[2445]: (root) BEGIN EDIT (root)
Apr 6 22:14:07 centos crontab[2445]: (root) END EDIT (root)
Apr 6 22:20:01 centos CROND[2475]: (root) CMD (/usr/lib64/sa/sa1 1 1)
Apr 6 22:20:01 centos crontab[2478]: (root) LIST (root)
Apr 6 22:22:26 centos crontab[2485]: (root) BEGIN EDIT (root)
Apr 6 22:25:16 centos crontab[2485]: (root) REPLACE (root)
Apr 6 22:25:16 centos crontab[2485]: (root) END EDIT (root)
Apr 6 22:25:24 centos crontab[2488]: (root) LIST (root)
Apr 6 22:30:01 centos CROND[2492]: (root) CMD (/usr/lib64/sa/sa1 1 1)
[[email protected] scripts]# mkdir /backup
[[email protected] scripts]# tar zcvf /backup/rc.local_date$(date +%F-%H).tar.gz /etc/rc.local
tar: Removing leading `/‘ from member names
/etc/rc.local
[[email protected] scripts]# vim back.sh
#tar /backup by 20has at 20170406
tar zcvf /backup/rc.local_date$(date +%F-%H).tar.gz /etc/rc.local >/dev/null 2>&1
tar zcvf /backup/hosts_date$(date +%F-%H).tar.gz /etc/hosts >/dev/null 2>&1
tar zcvf /backup/cron_date$(date +%F-%H).tar.gz /var/spool/cron >/dev/null 2>&1
"back.sh" [New] 4L, 272C written
[[email protected] scripts]# /bin/sh /service/scripts/back.sh
[[email protected] scripts]# crontab -e
#tar by 20has at 20170406
00 */2 * * * /bin/sh /service/scripts/tar.sh >/dev/null 2>&1
00 00 * * * /bin/sh /service/scripts/bake.sh >/dev/null 2>&1
30 00 * * * /bin/sh /service/scripts/back.sh >/dev/null 2>&1
#tar by 20has at 20170406
00 */2 * * * /bin/sh /service/scripts/tar.sh >/dev/null 2>&1
00 00 * * * /bin/sh /service/scripts/bake.sh
30 00 * * * /bin/sh /service/scripts/back.sh
[[email protected] scripts]# find /backup/ -type f -name *tar.gz -mtime +3 |xargs rm -f
[[email protected] scripts]# vim back.sh
#tar /backup by 20has at 20170406
tar zcf /backup/rc.local_date$(date +%F-%H).tar.gz /etc/rc.local >/dev/null 2>&1
tar zcf /backup/hosts_date$(date +%F-%H).tar.gz /etc/hosts >/dev/null 2>&1
tar zcf /backup/cron_date$(date +%F-%H).tar.gz /var/spool/cron >/dev/null 2>&1
find /backup/ -type f -name *tar.gz -mtime +3 |xargs rm -f
"back.sh" 5L, 331C written
[[email protected] scripts]# crontab -l
#tar by 20has at 20170406
00 */2 * * * /bin/sh /service/scripts/tar.sh >/dev/null 2>&1
00 00 * * * /bin/sh /service/scripts/bake.sh
30 00 * * * /bin/sh /service/scripts/back.sh
[[email protected] scripts]# tailf /var/log/cron #监视cron是否执行
Apr 6 23:01:00 centos crontab[2583]: (root) REPLACE (root)
Apr 6 23:01:00 centos crontab[2583]: (root) END EDIT (root)
Apr 6 23:01:01 centos crond[2176]: (root) RELOAD (/var/spool/cron/root)
Apr 6 23:01:01 centos CROND[2586]: (root) CMD (run-parts /etc/cron.hourly)
Apr 6 23:01:01 centos run-parts(/etc/cron.hourly)[2586]: starting 0anacron
Apr 6 23:01:01 centos anacron[2598]: Anacron started on 2017-04-06
Apr 6 23:01:01 centos anacron[2598]: Jobs will be executed sequentially
Apr 6 23:01:01 centos anacron[2598]: Normal exit (0 jobs run)
Apr 6 23:01:01 centos run-parts(/etc/cron.hourly)[2600]: finished 0anacron
Apr 6 23:05:53 centos crontab[2607]: (root) LIST (root)
老男孩讲解 以上命令存在问题: /etc/rc.local是链接文件 不能直接tar打包文件
可以用tar zcfh h就是针对链接文件的压缩
学长讲解 tar命令的记忆方法
tar zcvf 目标文件 源文件 压缩成 *.tar.gz文件
一筐苹果: 先选筐(放哪里 目标文件) 再放苹果(源文件、空格间隔可以多个)
解压 tar zxf *.tar.gz 解压文件
date命令应该是仅次于三剑客、find、tar等
vim编辑界面的时候 快捷键: ^光标回到行首 $光标回到行尾
ntpdate ntp1.aliyun.com 更新时间
学生作图: 关于 cron
8、sh -x 调试脚本
20170407 23:38-
你有没有遇到过 “No space left on device”提示空间不足的问题? 有 遇到过(才能显得你有经验)
比如创建的定时任务由于没有把错误信息等输出的垃圾清空(即没有加 >/dev/null 2>&1)导致inode被很快占用完 -----刘海林的回答
不记得vim的快捷按键了 得去重新回顾下视频 同时作图记忆快捷键
课后作业: