1 任务计划
说白了就是运行命令或者脚本的一个定时器, 他可以让我们在休息时间自动给我们执行任相关任务
来看下它的配置文件: cat /etc/crontab
第一行定义了 shell 环境
第二行定义 环境变量
第三行定义了发邮件给 root 用户, 邮件保存路径为 / var/spool/mail/root/
最后定义了任务内容的格式, 从左到右依次为:
分 时 日 月 周 命令
分钟: 0-59
小时: 0-23
日期: 1-31
月份: 1-12
周: 0-7 0,7 都表示星期天
可以指定一个范围: 比如 1-5 周一到周五, 也可以单独指定: 比如 1,2 可以表示每月 1 号和 2 号
* 表示所有时间, */2 可以表示偶数时间段
crontab -e 创建一个任务, 此时相当于 vi 打开一个文本, 里面的操作跟 vi 一致
-l 查看任务
-r 删除任务
-u 后面跟用户名, 指定是哪个用户的计划任务, 不指定默认 root 用户
任务创建完后, 一定要确保任务计划服务启动
systemctl start crond.service
启动任务计划服务
ps aux |grep crond 查看是否启动, 查看是否有 / usr/sbin/crond -n 这一进程
或者
systemctl status crond
查看, 出现绿色字显示的 active(running)表示启动
任务放在了 / var/spool/cron/username 下, username 指相应用户
备注: 不管是计划任务, 还是 iptables 规则, 还是其他 shell 脚本尽量使用绝对路径, 计划任务最好将命令执行结果反馈到日志,>> 2>> , 以便日后有据可查
2 chkconfig
linux 系统服务管理, 类似于 windos 的开机启动管理来熟悉下基本用法:
- chkconfig --list // 查看所有服务
- chkconfig --level 345 network off //345 级别的 network 服务关闭
- chkconfig --del network // 删除 network 服务
- chkconfig --add network // 添加 network 服务
添加服务之前需要先将服务脚本放进 /etc/init.d / 文件夹
3 systemd 管理
centos 7 开始使用的系统服务管理机制
systemctl list-units --all --type=service // 查看所有 service 服务
几个常用的服务相关的命令
- systemctl enable crond.service // 让服务开机启动
- systemctl disable crond // 不让开机启动
- systemctl status crond // 查看状态
- systemctl stop crond // 停止服务
- systemctl start crond // 启动服务
- systemctl restart crond // 重启服务
- systemctl is-enabled crond // 检查服务是否开机启动
4 unit 和 target
unit 是一个守护进程的分装单元, 多个 unit 组成了一个 target
ls /usr/lib/systemd/system // 系统所有 unit, 分为以下类型
service 系统服务
target 多个 unit 组成的组
device 硬件设备
mount 文件系统挂载点
automount 自动挂载点
path 文件或路径
scope 不是由 systemd 启动的外部进程
slice 进程组
snapshot systemd 快照
socket 进程间通信套接字
swap swap 文件
timer 定时器
unit 相关的命令
- systemctl list-units // 列出正在运行的 unit
- systemctl list-units --all // 列出所有, 包括失败的或者 inactive 的
- systemctl list-units --all --state=inactive // 列出 inactive 的 unit
- systemctl list-units --type=service// 列出状态为 active 的 service
- systemctl is-active crond.service // 查看某个服务是否为 active
系统为了方便管理用 target 来管理 unit
- systemctl list-unit-files --type=target
- systemctl list-dependencies multi-user.target // 查看指定 target 下面有哪些 unit
- systemctl get-default // 查看系统默认的 target
- systemctl set-default multi-user.target // 设置系统默认的 target
unit target service 三者的关系:
一个 service 属于一种类型的 unit
多个 unit 组成了一个 target,
一个 target 里面包含了多个 service
cat /usr/lib/systemd/system/sshd.service // 看 [install] 部分, 查看该
service 属于哪一个 target
来源: http://www.bubuko.com/infodetail-2539893.html