Linux 周期任务
一次性任务
在某个特定的时间,执行一次后被清除
相关命令/进程
- at 命令
- atd进程
在centos6中,系统服务的名称: /etc/init.d/atd
查看系统上该进程时候启动:
[root@ecs-t6-large-2-linux-20190824103606 ~]# ps -ef | grep atd
root 4472 1 0 Sep04 ? 00:00:00 /usr/sbin/atd -f
root 9628 9222 0 18:21 pts/0 00:00:00 grep --color=auto atd
创建一次性任务
格式:
- at [HH:MM]
- at [HH:MM] [yyyy-mm-dd]
- at now + 数字 [minutes |hours | days | weeks]
例: 创建定时任务, 在下一分钟,创建一个文件
[root@ecs-t6-large-2-linux-20190824103606 init.d]# at 18:33
at> touch 123
at> <EOT>
job 3 at Sat Sep 14 18:33:00 2019
[root@ecs-t6-large-2-linux-20190824103606 init.d]# ll
total 44
-rw-r--r-- 1 root root 0 Sep 14 18:33 123
保存定时任务快捷键 ctrl + d
将文件中的命令当作定时任务
格式: at 时间 -f 文件
例: 五分钟后执行 /root/test.sh
at now + 5 minutes -f /root/test.sh
查询当前系统上的一次性定时任务
- 命令1:
at -l
[root@ecs-t6-large-2-linux-20190824103606 init.d]# at -l
没任何结果说明没有一次性任务
- 命令2:
atq
[root@ecs-t6-large-2-linux-20190824103606 init.d]# atq
没任何结果说明没有一次性任务
[root@ecs-t6-large-2-linux-20190824103606 init.d]# atq
5 Sat Sep 14 20:00:00 2019 a root
5 是任务号
根据任务编号删除指定的一次性任务
- 命令1
atrm [编号]
- 命令2
at -d
查看一次性任务的具体内容
at -c [任务号]
创建的一次性任务文件所在位置
/var/spool/at/a*
在这个路径下,全部a开头的文件都在这里面
[root@ecs-t6-large-2-linux-20190824103606 init.d]# ll /var/spool/at/
total 8
-rwx------ 1 root root 3085 Sep 14 18:37 a00005018ee170
drwx------. 2 root root 4096 Sep 14 18:33 spool
任务执行过之后,这个文件就会消失
在那些文件中显示哪些用户是否可以使用定时任务
- /etc/at.deny :这个名单中的用户不可以使用定时任务
- /etc/at.allow: 这个名单中的用户可以使用
周期性任务
按照预订的计划重复执行任务
相关命令/进程
- crontab命令
- crond进程
在centos6中,周期任务对应的服务是: /etc/init.d/crond
查看系统上该进程是否被启动
[root@ecs-t6-large-2-linux-20190824103606 ~]# ps -ef | grep crond
root 4474 1 0 Sep04 ? 00:00:00 /usr/sbin/crond -n
root 9699 9222 0 18:23 pts/0 00:00:00 grep --color=auto crond
创建的周期任务文件所在位置
/var/spool/cron/用户名
所以查看系统上是否存在周期任务可以如下:
[root@ecs-t6-large-2-linux-20190824103606 init.d]# cat /var/spool/cron/root
5 11 * * * /tmp/touch_file
cron 服务的配置文件
[root@ecs-t6-large-2-linux-20190824103606 init.d]# 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
注意上面的PATH,这个PATH可能和系统中的PATH不同,这就意味着,当我们命令行敲某些命令时可以根据系统的PATH找到这些命令,但是如果周期任务的PATH中缺少一些路径,就可能导致周期运行命令时失败
- 查看系统的PATH, (我的默认是完全相同的)
[root@ecs-t6-large-2-linux-20190824103606 init.d]# echo $PATH
/sbin:/bin:/usr/sbin:/usr/bin
如果真的出现了不能执行的命令怎么办? 可以在命令前面加上绝对路径 /bin/ 命令
例子: 创建一个脚本文件
[root@ecs-t6-large-2-linux-20190824103606 tmp]# cat touch_file
touch a$RANDOM
执行这个脚本
[root@ecs-t6-large-2-linux-20190824103606 tmp]# bash touch_file
[root@ecs-t6-large-2-linux-20190824103606 tmp]# ll
total 48
-rw-r--r-- 1 root root 0 Sep 14 18:59 a21698
给这个脚本添加x, 让其可执行
[root@ecs-t6-large-2-linux-20190824103606 tmp]# chmod +x touch_file
[root@ecs-t6-large-2-linux-20190824103606 tmp]# ll
-rwxr-xr-x 1 root root 15 Sep 14 18:59 touch_file
再次执行:
[root@ecs-t6-large-2-linux-20190824103606 tmp]# ./touch_file
[root@ecs-t6-large-2-linux-20190824103606 tmp]# ll
total 48
-rw-r--r-- 1 root root 0 Sep 14 18:59 a21698
-rw-r--r-- 1 root root 0 Sep 14 19:01 a3460
当我们切换到其他目录时, 只能通过添加绝对路径才能运行脚本
[root@ecs-t6-large-2-linux-20190824103606 tmp]# bash /tmp/touch_file
添加到周期任务中
查看cron服务的日志文件
/var/log/cron
从这个日志文件中可以看到历史任务执行记录
管理cron计划任务
- 编辑计划任务:crontab -e [-u 用户名]
- 查看计划任务:crontab -l [-u 用户名]
- 删除计划任务:crontab -r [-u 用户名]
周期任务的格式
[root@ecs-t6-large-2-linux-20190824103606 init.d]# 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-59 0-23 1-31 1-12 0-7
0 17 * * 1- 5 周一到周五的每天17:00
30 8 * * 1,3,5 星期1,3,5 每天八点半
0 8-18 * * * 每天的8-18点
0 12 * * 每隔三天的12点
系统级别的计划任务及其扩展anacrontab
适用于下面的两种情况
linux 主机存在定时任务, 但主机又不是时时刻刻开机, 通过如下配置,可以实现, 一开机执行错过的定时任务
我有一个脚本, 需要每天都运行一次,但是什么时候运行我们并不关心,但是得运行
[root@ecs-t6-large-2-linux-20190824103606 ~]# cat /etc/anacrontab
# /etc/anacrontab: configuration file for anacron
# See anacron(8) and anacrontab(5) for details.
SHELL=/bin/sh
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root
# the maximal random delay added to the base delay of the jobs
RANDOM_DELAY=45
# the jobs will be started during the following hours only
# 每天的3点到22点 都可能会启动任务
START_HOURS_RANGE=3-22
#period in days delay in minutes job-identifier command
# 每隔多少天执行后面的命令 延迟时间 动作的表示 执行的命令
1 5 cron.daily nice run-parts /etc/cron.daily
7 25 cron.weekly nice run-parts /etc/cron.weekly
@monthly 45 cron.monthly nice run-parts /etc/cron.monthly
这个延迟的解释是 , 假如我的八点开机的, 八点在 3-22点之间, 但是任务不会一开机立即执行, 会延迟[5分钟]执行
延迟也可以是 0-45 随机时间
run-parts 命令
run-parts + 路径
执行该目录下的脚本
Linux 周期任务的更多相关文章
- Linux 驱动开发
linux驱动开发总结(一) 基础性总结 1, linux驱动一般分为3大类: * 字符设备 * 块设备 * 网络设备 2, 开发环境构建: * 交叉工具链构建 * NFS和tftp服务器安装 3, ...
- Linux系统编程(24)——信号的生命周期
信号生命周期为从信号发送到信号处理函数的执行完毕. 对于一个完整的信号生命周期(从信号发送到相应的处理函数执行完毕)来说,可以分为三个重要的阶段,这三个阶段由四个重要事件来刻画:信号诞生:信号在进程中 ...
- Linux内存管理 (14)匿名页面生命周期
专题:Linux内存管理专题 关键词:匿名页面.换入.换出. 如果要将匿名页面的生命周期进行划分,大概就是诞生.使用.换出.换入和销毁. 内核中使用匿名页面的地方有很多,产生缺页中断之后匿名页面就诞生 ...
- linux中的周期调度器
2017-06-27 上篇文章简要介绍了Linux进程调度,以及结合源代码窥探了下CFS的调度实例.但是没有深入内部区分析调度下面的操作,比如就绪队列的维护以及进程时间的更新等.本节就这些问题做深入讨 ...
- linux(一)export的生命周期
本文从shell执行的角度分析export变量的生命周期 # 只对当前shell环境起作用,比如通过不同的远程ssh就是不同的shell环境 export k=v 当运行一个.sh文件或者是shell ...
- Linux进程调度与源码分析(二)——进程生命周期与task_struct进程结构体
1.进程生命周期 Linux操作系统属于多任务操作系统,系统中的每个进程能够分时复用CPU时间片,通过有效的进程调度策略实现多任务并行执行.而进程在被CPU调度运行,等待CPU资源分配以及等待外部事件 ...
- [转贴]Linux内核LTS长期支持版生命周期
Linux内核LTS长期支持版生命周期 https://blog.51cto.com/dangzhiqiang/1894026 搞不懂长期支持版本的特点和区别. 党志强关注0人评论4371人阅读201 ...
- linux进程管理(linux命令安装、进程生命周期、进程状态)
1 linux下如何杀掉进程 1)找到包名所占用的端口: ps aux | grep cbs_portal-1.0.1.jar(包名) 2)杀掉进程: kill 10942(端口号) PS: //-- ...
- 【Microsoft Azure 的1024种玩法】六、使用Azure Cloud Shell对Linux VirtualMachines 进行生命周期管理
[文章简介] Azure Cloud Shell 是一个用于管理 Azure 资源的.可通过浏览器访问的交互式经验证 shell. 它使用户能够灵活选择最适合自己工作方式的 shell 体验,本篇文章 ...
随机推荐
- 大型情感剧集Selenium:3_元素定位 #华为云·寻找黑马程序员#
关于昨天的文章 今天有朋友反馈,代码运行的时候,selenium提示警告 DeprecationWarning: use options instead of chrome_options drive ...
- Too many open files的四种解决办法
[摘要] Too many open files有四种可能:一 单个进程打开文件句柄数过多,二 操作系统打开的文件句柄数过多,三 systemd对该进程进行了限制,四 inotify达到上限. 领导见 ...
- ubuntu下仅仅获取网卡一的ip地址 && shell中字符串拼接
问题描述: ubuntu下仅仅获取网卡一的ip地址 问题背景: eth0,eth1,eth2……代表网卡一,网卡二,网卡三…… lo代表127.0.0.1,即localhost | 问题描述: 已知字 ...
- 严格次短路的求法-spfa
#include<iostream> #include<cstdio> #include<algorithm> #include<queue> #inc ...
- 自定义 cell
自定义 cell 1 什么是自定义 cell 自定义 cell 即 tableView,collectionView,scrollView中的 cell 使用的时候不能满足我们使用 cell 的需求, ...
- windows下RocketMQ下载安装教程
一.下载(原文链接:http://www.studyshare.cn/software/details/1183/0 ) 1.官网下载:下载地址 2.百度网盘下载:下载地址 提取码:0g5a ja ...
- idea结合maven小项目
整体构造 (修改 POM 文件 )parent <?xml version="1.0" encoding="UTF-8"?> <project ...
- CodeForces-Round524 A~D
A. Petya and Origami time limit per test 1 second memory limit per test 256 megabytes input stan ...
- 洛谷 题解 P1736 【创意吃鱼法】
题目大意 给出一个 \(n \times m \ (1 \leq n, \ m \leq 2500)\) 的 \(01\) 矩阵,让你在其中找到一个最大的子矩阵使得该子矩阵除了一条对角线上的数字均为 ...
- LNMP架构的搭建
第9章 LNMP架构的搭建 9.1 什么是LNMP 9.1.1 LNMP的组成 L linux N nginx:实现静态的服务处理 M ...