本章 Blog 相关 Linux 知识点
linux 任务计划:
一次性任务计划命令: at ,batch , 依赖进程 atd
周期性任务计划命令: crontab ,anacron , 依赖进程 crond
at,batch 命令及选项
at,batch 是可以处理进执行一次就结束调度的命令. 不过在执行 at 时, 必须有 atd 服务支持才行 . 若 atd 默认未启动, 则 at 命令就会失效 .batch 是利用 at 来进行命令的执行, 系统空闲时才执行后台任务 .
交互式: 让用户在 at> 提示符输入多个要执行的命令
批处理: 将任务的命令写入文件由 at 进行调用
建议使用命令绝对路径, 使用 Ctrl+d 键提交任务
- # at TIME
- # at -m : 当 at 的工作完成后, 即使没有输出信息, 依然以 email 方式通知该任务已完成
- # at -l : 相当于 atq 命令, 列出目前系统上面所有 at 调度任务
- # at -d JOBS: 相当于 atrm 命令, 取消 JOBS 调度 , 例 # at -d 1 取消第一个调度任务
- # at -c job : 列出该 job 的实际命令内容
- # batch 不能指定时间, 它自动选择系统空闲时间执行
- [root@study ~]# chkconfig --add atd 依赖 atd 服务
- [root@study ~]# chkconfig atd on
- [root@study ~]# at now +5 minutes 从现在开始第 5 分钟执行
- at> /bin/ls /etc/passwd
- at> /bin/sync
- at> /sbin/shutdown -r now
- at> <EOT>
- job 5 at 2018-05-06 19:21
- [root@study ~]# at -l
- 5 2018-05-06 19:21 a root
- 3 2018-05-06 18:24 b root
- [root@study ~]# at -d 3
- [root@study ~]# atq
- 5 2018-05-06 19:21 a root
- [root@study ~]# at -m
- Garbled time
- [root@study ~]# at -c 5
- #!/bin/sh
- # atrun uid=0 gid=0
- # mail root 0
- umask 22
- HOSTNAME=study.itwish.cn; export HOSTNAME
- SHELL=/bin/bash; export SHELL
- HISTSIZE=1000; export HISTSIZE
- ...
- [root@study ~]# batch
- at> /bin/ls /etc/issue
- at> /bin/sync
- at> <EOT>
mail 命令及选项
mail 命令, 并使用 Ctrl+D 键 提交邮件
- # mail 查看邮件, 并使用号码选择要查看的邮件, 使用 quit 退出
- # mail root 向 root 用户发送邮件
- # mail -s "How are"root
- # mail -s "Ceshi" root < /etc/passwd 指定 / etc/passwd 文件作为发送 root 用户的邮件内容
- [root@study ~]# yum install mailx -y 安装 mailx 邮件
- [root@study ~]# mail 查看邮件
- Heirloom Mail version 12.4 7/29/08. Type ? for help.
- "/var/spool/mail/root": 2 messages 2 new
- >N 1 root Tue May 8 09:42 38/1511 "ces"
- N 2 root Tue May 8 09:43 18/583 "ces"
& 1 输入邮件号查看邮件内容
- Message 1:
- From root@study.localdomain Tue May 8 09:42:50 2018
- Return-Path: <root@study.localdomain>
- X-Original-To: root
- Delivered-To: root@study.localdomain
Date: Tue, 08 May 2018 09:42:50 +0800
- To: root@study.localdomain
- Subject: ces
- User-Agent: Heirloom mailx 12.4 7/29/08
- Content-Type: text/plain; charset=us-ascii
- From: root@study.localdomain (root)
- Status: R
- how are
- & 2
- Message 2:
- From root@study.localdomain Tue May 8 09:43:05 2018
- Return-Path: <root@study.localdomain>
- X-Original-To: root
- Delivered-To: root@study.localdomain
Date: Tue, 08 May 2018 09:43:05 +0800
- To: root@study.localdomain
- Subject: ces
- User-Agent: Heirloom mailx 12.4 7/29/08
- Content-Type: text/plain; charset=us-ascii
- From: root@study.localdomain (root)
- Status: R
how are you ?
& quit 退出
crontab ,anacron 命令及选项
周期性任务: cron ,
# yum install crontabs -y 安装 crond 进程及 crontab 命令
建议命令写全路径 , 日志目录为 /var/log/cron
cron 任务命令依赖于 crond 进程 ,crond: 守护进程, 运行在后台, 随时监听着进程等待执行家目录, 主目录 home directory
系统 cron:
- # chkconfig crond on 配置开机运行 crond 进程
- # service crond start 启动 crond 进程
- # service crond status 查看 crond 进行是否运行
- # ps -aux | grep crond 查看运行中是否包含 crond 进程
配置文件: /etc/crontab
- [root@bogon ~]# 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
选项: 分钟 小时 天 月 周 用户 命令
注意: 添加用户名称
例: */1 * * * * root /bin/echo "hello word" 表示每分钟 root 用户执行 / bin/echo "hello word" 语句
01 00 * * 1 root /etc/rc.d/init.d/httpd restart 表示每周一的 0 点 1 分 root 用户执行 httpd 重启服务
时间表示法:
1, 每个时间位都应该使用其可用的有效范围的值
2, 某时间位上的 * 表示对应位的所有的有效取值
3, - 表示连续的时间点取值 , 如 30-35
4, , 表示离散的时间点取值 , 如 3 ,5
5, */# 表示在指定的时间范围内每隔# 执行一次 , 如 */2 * * * */bin/echo "hello word" &> /dev/null 表示每隔 2 分钟输出语句 "hello word", 通过输出重定向而避免接收邮件
用户 cron:
使用 crontab 命令来实现, 配置文件 /etc/spool/cron/ 目录中以登录名命名的文件
- # crontab -l 查看自己的 crontab 任务列表
- # crontab -e 通过 editor 变量中定义的编辑器打开自己的 cron 配置文件, 编辑单独的任务都是用 - e 选项, 无论是删除还是新建
- # crontab -r 移除 crontab 文件
如果是管理员 # crontab -u username 为 username 用户指定 crontab 作业 , 例 :crontab -e -u docker 为用户 docker 指定 crontab 作业
anacron : 最小刻度是天 , 是 crontab 的补充, 用于检查 crontab 中某任务在过去一个周期是否没有执行; 如果没有执行, 则在开机以后的某个时间点让其执行一次, 无论其周期是否到达
- [root@study ~]# vi /etc/crontab
- SHELL=/bin/bash
- PATH=/sbin:/bin:/usr/sbin:/usr/bin
- MAILTO=root
- HOME=/
- # 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
- */1 * * * * root /bin/echo "hello word"
- 42 15 * * * root /etc/rc.d/init.d/httpd restart
- [root@study ~]# tail -f /var/log/cron 查看 cron 日志
- May 8 15:42:01 study crond[1361]: (*system*) RELOAD (/etc/crontab)
- May 8 15:42:01 study CROND[1756]: (root) CMD (/bin/echo "hello word")
- May 8 15:42:01 study CROND[1757]: (root) CMD (/etc/rc.d/init.d/httpd restart)
- [root@study ~]# mail 查看邮件确认
- Heirloom Mail version 12.4 7/29/08. Type ? for help.
- >N 1 Cron Daemon Tue May 8 15:42 23/775 "Cron <root@study> /bin/echo"hello word""N 2 Cron Daemon Tue May 8 15:42 24/824"Cron <root@study> /etc/rc.d/init.d/httpd restart"
- [root@study ~]# crontab -e 为登录用户配置 cron 任务
- no crontab for root - using an empty one
- */1 * * * * /bin/ls /etc/passwd
- [root@study ~]# crontab -l
- */1 * * * * /bin/ls /etc/passwd
- [root@study ~]# crontab -u docker -e 为用户 docker 指定 cron 任务
- no crontab for docker - using an empty one
- */1 * * * * /bin/cat /etc/passwd
- [root@study ~]# crontab -l
- */1 * * * * /bin/ls /etc/passwd
- [root@study ~]# crontab -r 删除本用户 cron 任务
- [root@study ~]# crontab -u docker -l
- */1 * * * * /bin/cat /etc/passwd
- [root@study ~]# crontab -u docker -r
- [root@study ~]# cd /var/spool/cron/ 查看 / var/spool/cron / 目录, 确认那个用户存在 cron 任务
- [root@study cron]# ls
文件访问控制列表 facl
FACL:Filesystem Acess Control List
Linux(和其他 Unix 等 POSIX 兼容的操作系统)有一种被称为访问控制列表 (ACL) 的权限控制方法, 它是一种权限分配之外的普遍范式. 例 如, 默认情况下你需要确认 3 个权限组: owner,group 和 other. 而使用 ACL, 利用文件扩展属性保存额外的访问控制权限, 你可以增加权限或其他用户或组别. 可以允许指定的用户 A,B,C 拥有写权限而不再是让他们整个组拥有写权限.
# setfacl 设定 facl 权限
-m: 设定权限
[u|g]:UID:perm
d:[u|g]:UID:perm 继承父目录 facl 权限
m::perm
-x: 取消 facl 权限
- [u|g]:UID
- m:
-R: 为某一个目录设置默认的访问控制列表. 意味着在这个目录下的文件将自动继承目录的访问控制列表权限.
例
- # setfacl -m u:hadoop:rw inittab 配置文件 inittab 对用户 hadoop 具有读写权限
- # setfacl -x u:hadoop inittab 取消 facl 权限
- [root@study root]## yum install acl 安装 acl 功能
- [root@study root]# vi /etc/fstab
- ....
/dev/mapper/vg0-home /home ext4 defaults,acl 1 2 激活磁盘分区 ACL 功能
- ....
- [root@study ~]# mount -o remount /home 重新挂载 / home 分区
- [root@study ~]# useradd hadoop
- [root@study ~]# useradd docker
- [root@study ~]# cd /home
- [root@study home]# setfacl -m u:docker:rw ./a.txt
- [root@study home]# setfacl -m u:hadoop:rw ./a.txt
- [root@study home]# getfacl a.txt
- # file: a.txt
- # owner: root
- # group: root
- user::rw-
- user:hadoop:rw-
- user:docker:rw-
- group::r--
- mask::rw-
- other::r--
- [root@study home]# cat /etc/issue > a.txt
- [root@study home]# su - docker
- [docker@study ~]$ vi /home/a.txt
- CentOS release 6.9 (Final)
Kernel \r on an \m
本章知识点汇总
- # chkconfig atd on 在 2-5 级别下开机自动启动该进程
- # chkconfig crond on 开机启动 crond 进程服务
- # chconfig postfix on 开机启动 postfix 邮件进程服务
- # service atd start 启动 atd 进程服务
- # service crond start 启动 crond 服务
- # service postfix start 启动邮件服务
- # at TIME 执行一次性任务调度
- # atq 查询 at 任务 , 相当于 # at -l
- # atr 删除 at 任务 , 相当于# at -d
- # mail -s "Subject" Username 发送主题为 Subject 的邮件到 Username 用户
- # crontab -r 删除本用户的 crontab 任务
- # crontab -l -u Username 显示 Username 用户的任务列表
- # crontab -e 编辑本用户的 crontab 任务
- # setfacl -m [u|g]:Username:Permis Filename 添加 FACL 功能
- # setfacl -x [u|g]:Username:Permis Filename 取消 FACL 功能
来源: http://www.bubuko.com/infodetail-2592828.html