Linux任务管理

一、at

单次执行任务。

(1)30分钟后重启

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [root@wc1 Desktop]# at now + 3 minutes  
  2. at> /usr/bin/reboot<EOT>  
  3. job 3 at 2016-11-10 14:22  

这里的<EOT>是按crtl+D 出来的。

(2)查询任务、删除任务

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [root@wc1 Desktop]# atq  
  2. 3   2016-11-10 14:22 a root  
  3.   
  4. [root@wc1 Desktop]# atrm 3  
(3)2016-11-11日0点关闭服务器
[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [root@wc1 Desktop]# at 00:00 2016-11-11   
  2. at> /sbin/shutdown -h now<EOT>  
  3. job 4 at 2016-11-11 00:00  
  4.   
  5. [root@wc1 Desktop]# atq  
  6. 4   2016-11-11 00:00 a root  
(4)把用户名加入文件/etc/at.deny中,禁止用户mysql使用这个功能。
[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [root@wc1 Desktop]# cat /etc/at.deny  
  2.   
  3. [root@wc1 Desktop]# vi /etc/at.deny  
  4. [root@wc1 Desktop]# cat /etc/at.deny  
  5. mysql  
mysql用户使用at时直接报:没有权限使用at。
[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [mysql@wc1 ~]$ at  
  2. You do not have permission to use at.  
  3. [mysql@wc1 ~]$   

二、cron

周期性执行任务

(1)启动cron进程

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [root@ggg2 ~]# service crond start  
  2.   
  3. [root@ggg2 ~]# service crond status  
  4. 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是星期日。

举例:

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. #每分钟重启一次  
  2. *   *   *   *   *   service httpd restart  
  3. */1 *   *   *   *   service httpd restart  
  4.   
  5. #每小时重启  
  6. *   */1 *   *   *   service httpd restart  
  7.   
  8. #从23点开始到3点,每小时重启一次  
  9. *   23-3/1  *   *   *   service httpd restart  
  10.   
  11. #每天晚上23点30分,重启  
  12. 30  23  *   *   *   service httpd restart  
  13.   
  14. #每月的第一天晚上23点30分重启  
  15. 30  23  1   *   *   service httpd restart  
  16.   
  17. #每年第一个月第一天晚上23点30分重启  
  18. 30  23  1   1   *   service httpd restart  
  19.   
  20. #每周日晚上23点30分,重启  
  21. 30  23  *   *   0   service httpd restart  

(3)编辑、查看、删除

编辑:

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [root@ggg2 ~]# crontab -e  
  2. crontab: installing new crontab  

查看:

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [root@ggg2 ~]# crontab -l  
  2. 30  23  *   *   0   service httpd restart  

删除:

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [root@ggg2 ~]# crontab -r  
  2.   
  3. [root@ggg2 ~]# crontab -l  
  4. no crontab for root  

(4)禁止某些用户使用cron

可以把用户的用户名添加到文件 /etc/cron.deny中。


(5)除了root外,普通用户只能设置、查看、删除自己的计划任务。

root可以用-u参数查看指定用户的任务,比如:

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [root@ggg2 ~]# crontab -u mysql -l  
  2. no crontab for mysql  

(6)/etc/contab 管理

可以通过crontab -e来编辑自己的任务,单系统也有自己的例行任务,配置文件是:/etc/crontab.

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [root@ggg2 ~]# cat /etc/crontab  
  2. SHELL=/bin/bash  
  3. PATH=/sbin:/bin:/usr/sbin:/usr/bin  
  4. MAILTO=root  
  5. HOME=/  
  6.   
  7. # For details see man 4 crontabs  
  8.   
  9. # Example of job definition:  
  10. # .---------------- minute (0 - 59)  
  11. # |  .------------- hour (0 - 23)  
  12. # |  |  .---------- day of month (1 - 31)  
  13. # |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...  
  14. # |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat  
  15. # |  |  |  |  |  
  16. # *  *  *  *  * user-name command to be executed  

和cron相关的文件、目录:

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [root@ggg2 ~]# ls -al /etc | grep cron  
  2. -rw-------.   1 root root    541 Nov 23  2013 anacrontab  
  3. drwxr-xr-x.   2 root root   4096 Aug 18 23:18 cron.d  
  4. -rw-------.   1 root root      0 Nov 23  2013 cron.deny  
  5. -rw-r--r--.   1 root root    457 Sep 27  2011 crontab  
  6.   
  7. drwxr-xr-x.   2 root root   4096 Aug 18 23:17 cron.hourly  
  8. drwxr-xr-x.   2 root root   4096 Aug 18 23:19 cron.daily  
  9. drwxr-xr-x.   2 root root   4096 Sep 27  2011 cron.weekly  
  10. drwxr-xr-x.   2 root root   4096 Aug 18 23:19 cron.monthly  

最后几个分别是:小时、日、周、月的要执行的任务,不过这些都是目录,目录中包含了要执行的任务文件:

[plain]  view plain  copy
 print ? 在CODE上查看代码片 派生到我的代码片
  1. [root@ggg2 ~]# ls -al /etc/cron.daily  
  2. total 44  
  3. drwxr-xr-x.   2 root root  4096 Aug 18 23:19 .  
  4. drwxr-xr-x. 117 root root 12288 Nov 13 22:23 ..  
  5. -rwx------.   1 root root   118 Oct 15  2014 cups  
  6. -rwxr-xr-x.   1 root root   196 Jul 18  2013 logrotate  
  7. -rwxr-xr-x.   1 root root   905 Feb 22  2013 makewhatis.cron  
  8. -rwxr-xr-x.   1 root root   174 Sep 24  2012 mlocate.cron  
  9. -rwxr-xr-x.   1 root root  2126 Jul 19  2013 prelink  
  10. -rwxr-xr-x.   1 root root   563 Nov 23  2013 readahead.cron  
  11. -rwxr-xr-x.   1 root root   365 Oct 16  2009 tmpwatch  
  • 1
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值