|
At是一个仅执行一次就结束的调度的命令(at的执行必须要启动atd服务)
1.命令格式:
at[参数][时间]
2.命令功能:
在一个指定的时间执行一个指定任务,只能执行一次,且需要开启atd进程(
ps -ef | grep atd查看, 开启用/etc/init.d/atd start or restart; 开机即启动则需要运行 chkconfig --level 2345 atd on)。
3.命令参数:
-m 当指定的任务被完成之后,将给用户发送邮件,即使没有标准输出
-I atq的别名
-d atrm的别名
-v 显示任务将被执行的时间
-c 打印任务的内容到标准输出
-V 显示版本信息
-q<列队> 使用指定的列队
-f<文件> 从指定文件读入任务而不是从标准输入读入
-t<时间参数> 以时间参数的形式提交要运行的任务
[root@lystest ~]# /etc/init.d/atd restart //重启atd进程
Stopping atd: [ OK ]
Starting atd: [ OK ]
/etc/at.allow 写入这个文件的用户能使用
/etc/at.deny 写入这个文件的用户则不能使用
当这两个文件都不存在时,只有root才能使用at
实例:
1.两天后的上午6点重启服务器:
[root@lys ~]# at 6am +2days
at> reboot
at> <EOT>
job 6 at 2015-09-09 06:00
2.明天下午18:30 把时间重定向到/etc/date
[root@lys ~]# at 18:30 tomorrow
at> date >/etc/date
at> <EOT>
job 7 at 2015-09-08 18:30
3.查询定时任务
[root@lys ~]# atq
4 2015-09-10 17:00 a root
6 2015-09-09 06:00 a root
5 2015-09-10 17:00 a root
[root@lystest ~]# atrm 删除任务
crontab 这个命令所设置的工作将会一直进行下去,在linux平台上如果需要实现任务调度功能可以编写cron脚本来实现。
[root@lystest ~]# crontab -e 编辑
[root@lystest ~]# crontab -l 查看
45 19 18 12 * mail 1095696077@qq.com < home/sh01.sh
[root@lystest ~]# 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实例1:每隔两天的上午8点到11点的第3和第15分钟执行
3,15 8-11 */2 * * /etc/init.d/snmpd restart实例2:每小时的第3和第15分钟执行
3,15 * * * * cat /proc/cpuinfo实例3:查找core文件,并在每个月的1,7,14,21,26的3.30将其删除
30 3 1,7,14,21,26 * * /bin/find -name 'core' -exec rm {} \; 习题:
每周一到周六的凌晨3点20分,运行cp命令对/etc/目录进行归档另存,存储位置为/backups/etc-YYYY-MM-DD
- [root@lys ~]# mkdir -p /backups/etc-&(date +%F) //新建一个目录
- [root@lys ~]# crontab -e //编辑定时任务
- 20 3 * * 0-6 cp /etc /backups/etc-&(date +%F)
每周日凌晨2点30分,运行cp命令对/etc/fstab文件进行备份,存储位置为/backup/fstab-YYYY-MM-DD-hh-mm-ss
- [root@lys ~]# mkdir -p /backup/fstab-&(date +%F-%H-%M-%S) //新建一个目录
- [root@lys ~]# crontab -e //编辑定时任务
- 30 2 * * 7 cp /etc/fstab /backup/fstab-&(date +%F-%H-%M-%S)
每天晚上12点,取得/proc/meminfo文件中所有以S或M开头的行,追加至/statistics/meminfo.txt文件中,且每天的消息之前,要加上类似===============分隔线
- mkdir -p /statistics/meminfo.txt
- cat /proc/meminfo | egrep '^S|M'
- 0 0 * * * /usr/bin/egrep (egrep '^(S|M)' /proc/meminfo;echo "=================") >> statistics/meminfo.txt
|
|
|