一、at
单次执行任务。
(1)30分钟后重启
- [root@wc1 Desktop]# at now + 3 minutes
- at> /usr/bin/reboot<EOT>
- job 3 at 2016-11-10 14:22
这里的<EOT>是按crtl+D 出来的。
(2)查询任务、删除任务
- [root@wc1 Desktop]# atq
- 3 2016-11-10 14:22 a root
-
- [root@wc1 Desktop]# atrm 3
(3)2016-11-11日0点关闭服务器
- [root@wc1 Desktop]# at 00:00 2016-11-11
- at> /sbin/shutdown -h now<EOT>
- job 4 at 2016-11-11 00:00
-
- [root@wc1 Desktop]# atq
- 4 2016-11-11 00:00 a root
(4)把用户名加入文件/etc/at.deny中,禁止用户mysql使用这个功能。
- [root@wc1 Desktop]# cat /etc/at.deny
-
- [root@wc1 Desktop]# vi /etc/at.deny
- [root@wc1 Desktop]# cat /etc/at.deny
- mysql
mysql用户使用at时直接报:没有权限使用at。
- [mysql@wc1 ~]$ at
- You do not have permission to use at.
- [mysql@wc1 ~]$
二、cron
周期性执行任务
(1)启动cron进程
- [root@ggg2 ~]# service crond start
-
- [root@ggg2 ~]# service crond status
- crond (pid 2352) is running...
(2)crontab基本格式:
* ****command
设置crontab的语法:
第1个:分钟,1~59,每分钟可以用*或者*/1.
第2个:小时,0~23。
第3个:日期,1~31。
第4个:月份,1~12.
第5个:星期几,0~6,0是星期日。
举例:
- #每分钟重启一次
- * * * * * service httpd restart
- */1 * * * * service httpd restart
-
- #每小时重启
- * */1 * * * service httpd restart
-
- #从23点开始到3点,每小时重启一次
- * 23-3/1 * * * service httpd restart
-
- #每天晚上23点30分,重启
- 30 23 * * * service httpd restart
-
- #每月的第一天晚上23点30分重启
- 30 23 1 * * service httpd restart
-
- #每年第一个月第一天晚上23点30分重启
- 30 23 1 1 * service httpd restart
-
- #每周日晚上23点30分,重启
- 30 23 * * 0 service httpd restart
(3)编辑、查看、删除
编辑:
- [root@ggg2 ~]# crontab -e
- crontab: installing new crontab
查看:
- [root@ggg2 ~]# crontab -l
- 30 23 * * 0 service httpd restart
删除:
- [root@ggg2 ~]# crontab -r
-
- [root@ggg2 ~]# crontab -l
- no crontab for root
(4)禁止某些用户使用cron
可以把用户的用户名添加到文件 /etc/cron.deny中。
(5)除了root外,普通用户只能设置、查看、删除自己的计划任务。
root可以用-u参数查看指定用户的任务,比如:
- [root@ggg2 ~]# crontab -u mysql -l
- no crontab for mysql
(6)/etc/contab 管理
可以通过crontab -e来编辑自己的任务,单系统也有自己的例行任务,配置文件是:/etc/crontab.
- [root@ggg2 ~]# 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
和cron相关的文件、目录:
- [root@ggg2 ~]# ls -al /etc | grep cron
- -rw-------. 1 root root 541 Nov 23 2013 anacrontab
- drwxr-xr-x. 2 root root 4096 Aug 18 23:18 cron.d
- -rw-------. 1 root root 0 Nov 23 2013 cron.deny
- -rw-r--r--. 1 root root 457 Sep 27 2011 crontab
-
- drwxr-xr-x. 2 root root 4096 Aug 18 23:17 cron.hourly
- drwxr-xr-x. 2 root root 4096 Aug 18 23:19 cron.daily
- drwxr-xr-x. 2 root root 4096 Sep 27 2011 cron.weekly
- drwxr-xr-x. 2 root root 4096 Aug 18 23:19 cron.monthly
最后几个分别是:小时、日、周、月的要执行的任务,不过这些都是目录,目录中包含了要执行的任务文件:
- [root@ggg2 ~]# ls -al /etc/cron.daily
- total 44
- drwxr-xr-x. 2 root root 4096 Aug 18 23:19 .
- drwxr-xr-x. 117 root root 12288 Nov 13 22:23 ..
- -rwx------. 1 root root 118 Oct 15 2014 cups
- -rwxr-xr-x. 1 root root 196 Jul 18 2013 logrotate
- -rwxr-xr-x. 1 root root 905 Feb 22 2013 makewhatis.cron
- -rwxr-xr-x. 1 root root 174 Sep 24 2012 mlocate.cron
- -rwxr-xr-x. 1 root root 2126 Jul 19 2013 prelink
- -rwxr-xr-x. 1 root root 563 Nov 23 2013 readahead.cron
- -rwxr-xr-x. 1 root root 365 Oct 16 2009 tmpwatch