定时任务(cron,at)

cron

  • 列出当前用户已有定时任务crontab -l

  • 添加新任务 crontab -e,然后编辑文件
    定时任务(cron,at)

  • cron执行定期任务:minute hour dayofmonth month dayofweek <command>

    • *代表所有
    • / 代表每隔n长时间
    • - 代表一段时间范围
    • , 代表独立的时间
    • 例子
      • 每周一下午4:15运行:15 16 * * 1 <command>
      • 每两个小时运行一次:0 */2 * * * <command>
      • 每小时的第3和第15分钟运行:3,15 * * * * <command>
      • 每隔两天的上午8点到11点的第3和第15分钟执行:3,15 8-11 */2 * * <command>
      • 每个月最后一天运行:00 12 * * * if [ $(date +%d -d tommorrow) = 01 ] ; then; <command>
    • 参考:https://www.cnblogs.com/dalianpai/p/11813950.html
  • 把脚本放到指定目录可以自动按照指定间隔定期执行:

    • /etc/cron.hourly/etc/cron.daily/etc/cron.monthly/etc/cron.weekly

at

  • at命令指定系统定时运行一次脚本: at -f <file> time
    • 如果指定的时间已经过去了,则在第二天运行
    • 时间格式
      • 小时分钟:14:15
      • AM/PM: 10:15 PM
      • 特定时间:now, noon, midnight, 17:00 tomorrow
      • 标准日期格式:MMDDYY, MM/DD/YY, DD.MM.YY
        • 例如 20:00 2008-10-1
      • 增量时间:now +10min ,6:00 pm + 3 days
    • 列出等待执行的作业:atq
    • 删除作业: atrm <job num>
      • 只能删除你提交的,不能删除其他人的