首页 > 系统服务 > 详细

Linux系统的任务计划

时间:2018-02-23 14:01:52      阅读:227      评论:0      收藏:0      [点我收藏+]
crontab周期性任务设置

# yum install cronie -y
# systemctl status crond.service
# systemctl start crond.service
# systemctl enable crond.service
[root@localhost etc]# cat /etc/crontab 
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# 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 //编辑计划任务列表 
-u //指定所管理的计划任务属于哪个用户,默认时针对当前用户,一般只有root用户才用权限使用此选项
-l //列表显示计划任务
-r //删除计划任务列表

用户cron任务配置存放目录

[root@localhost etc]# ls /var/spool/cron/

crontab -e

50 7 * * * /usr/bin/systemctl start sshd
50 22 * * * /usr/bin/systemctl stop sshd
0 * */5 * *  /usr/bin/rm -rf /var/ftp/pub/*
30 7 * * 6 /usr/bin/sytemctl restart httpd
30 17 * * 1,3,5 /usr/bin/tar jcvf httpdconf.tar.bz2 /etc/httpd

注意:在设置非每分钟都执行的任务时,“分钟”字段也应该填写一个具体的时间数值,而不要保留为默认的“*”,否则将会在每分钟执行一次计划任务。

确认root用户的计划任务列表的内容

[root@localhost etc]# crontab -l
50 7 * * * /usr/bin/systemctl start sshd
50 22 * * * /usr/bin/systemctl stop sshd
0 * */5 * *  /usr/bin/rm -rf /var/ftp/pub/*
30 7 * * 6 /usr/bin/sytemctl restart httpd
30 17 * * 1,3,5 /usr/bin/tar jcvf httpdconf.tar.bz2 /etc/httpd

确认jerry用户的计划任务列表的内容

[root@localhost ~]# crontab -l -u jerry
55 23 * * 0 /bin/cp /etc/passwd /home/jerry/pwd.txt
[root@localhost ~]# ls -l /var/spool/cron/jerry 
-rw-------. 1 jerry jerry 52 2月  23 13:38 /var/spool/cron/jerry

使用jerry用户登录后,查看并删除自己的计划任务列表

[jerry@localhost ~]$ crontab -l
55 23 * * 0 /bin/cp /etc/passwd /home/jerry/pwd.txt
[jerry@localhost ~]$ crontab -r
[jerry@localhost ~]$ crontab -l
no crontab for jerry

当需要按照同一周期运行多个任务时,通常会将相关命令操作编写成脚本文件,然后在计划任务配置中加载该脚本并执行

Linux系统的任务计划

原文:http://blog.51cto.com/13480443/2072333

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!