linux 之计划任务 at crontab

作者: 我爱矿泉水 | 来源:发表于2017-10-22 13:09 被阅读31次

1. at 命令

1.1 at 用法

at 时间描述

at 6pm Monday
at now + 5 minutes
at 12:04:47 10/22/2017

at>任务描述
at> ctrl+d

atq 查询当前用户正在等待的计划任务
atrm 删除一个正在等待的计划任务
格式:atrm 任务号

at计划任务依赖于atd:

[root@my ~]# systemctl status atd
atd.service - Job spooling tools
   Loaded: loaded (/usr/lib/systemd/system/atd.service; enabled)
   Active: active (running) since Sat 2017-10-21 07:51:33 CST; 22h ago
 Main PID: 958 (atd)
   CGroup: /system.slice/atd.service
           ?..958 /usr/sbin/atd -f

Oct 21 07:51:33 my.912616.com systemd[1]: Started Job spooling tools.
[root@my ~]# 

删除at计划任务

[root@my ~]# atq #查询当前等待执行的计划任务
1   Sun Oct 22 12:14:00 2017 a root
[root@my ~]# atrm 1
[root@my ~]#

1.2 限制

编辑/etc/at.deny,限制用户使用at做计划任务:

at.deny.jpg
[root@my ~]# su - mycms5
Last login: Sun Oct 22 05:52:38 CST 2017 from 192.168.137.3 on pts/0
[mycms5@rhel7 ~]$ at 12:25
You do not have permission to use at.
[mycms5@rhel7 ~]$

2. crontab

linux系统里,可以使用crontab制定管理计划任务时间表。

  • crontab -e 编辑当前用户的计划任务时间表
  • crontab -l 列出当前用户的计划任务时间表
  • crontab -r 删除当前用户的计划任务时间表
  • crontab -u username < -e | -l | -r > 以某一个用户的身份管理
  • man 5 crontab 查看crontab帮助信息

时间表配置文件的格式:

[root@my ~]# 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

时间数值的特殊表示方法:

  • * 表示该范围内的任意时间
  • - 表示间隔的多个不连续时间点
  • - 表示一个连续的时间范围
  • / 指定间隔的时间频率

示例:

  • 0 17 * * 1-5 周一到周五每天17:00
  • 30 8 * * 1,3,5 每周一、三、五的8点30分
  • 0 8-18/2 * * *     8点到18点之间每隔2小时
  • 0 * */3 * * 每隔3天
[root@rhel7 ~]# crontab -l
20 13 * * sun    touch filename
25 13 * * *      links www.chachabei.com/p/youjia_update
[root@rhel7 ~]# crontab -l  -u root
20 13 * * sun    touch filename
25 13 * * *      links www.chachabei.com/p/youjia_update
0 7 * * *      links www.yto.cc
  • crontab -r # 删除当前用户所有的计划任务
  • crontab -e # 进入编辑状态,添加计划任务,或者使用vim的-d删除具体某条任务

2.2 限制crontab

vi /etc/cron.deny,把mycms5用户名写进去

cron.deny.jpg

然后切换到mycms5,发现mycms5已经没法使用crontab了:

[root@rhel7 ~]# su - mycms5
Last login: Sun Oct 22 06:14:45 CST 2017 on pts/1
[mycms5@rhel7 ~]$ crontab -e
You (mycms5) are not allowed to use this program (crontab)
See crontab(1) for more information
[mycms5@rhel7 ~]$ 

相关文章

  • linux 提权-Crontab提权

    前言 记录一下linux提权系列的Crontab计划任务提权的学习过程。 crontab 命令 crontab命令...

  • linux提权入门

    一、crontab提权 linux系统中可以使用crontab -e命令创建计划任务,/var/spool/cro...

  • Linux 计划任务(crontab)

    Linux 计划任务(crontab) 介绍 crontab 是让系统在特定的时间去执行某项任务 系统允许个人用户...

  • linux|计划任务

    计划任务分类 at 一次性计划任务 查看任务 crontab 周期性任务 crontab:如何使用crontab ...

  • linux 系统计划任务cron学习慕课笔记

    linux 系统计划任务cron: 1:检查cron服务 -检查Crontab工具是否安装: -检查crond服务...

  • Linux 计划任务

    Linux 计划任务 crontab 执行Python脚本(以Postgresql为例,自动下架过期商品): Py...

  • 一文精通linux定时任务crontab

    此篇技术博文主要介绍的是crontab,Linux下的计划任务管理工具。涉及内容包括crontab使用配置、常见坑...

  • Crontab 学习笔记

    这是慕课网《Linux中的计划任务之Crontab》的学习记录,方便以后查阅。 从定时重复的工作中解脱出来 1. ...

  • laravel使用scheduler实现计划任务

    传统的计划任务是使用linux的crontab,每次对计划任务进行修改都要上服务器处理,不方便且不安全。larav...

  • linux 编写计划任务 (crontab任务

    linux 编写计划任务 (crontab任务) 转载凯里欧文最后发布于2018-07-06 13:42:40阅读...

网友评论

    本文标题:linux 之计划任务 at crontab

    本文链接:https://www.haomeiwen.com/subject/pfpguxtx.html