crontab

crontab 任务计划 crond服务 crontab -u -e -l -r 5个*格式:分,时,日,月,周 user command /etc/crontab 配置文件

crontab -e 编辑配置文件
crontab -l 查看任务计划
crontab -r 删除
crontab -u 指定用户

文件 /var/spool/cron/usermane 每个用户的定时任务 格式范围是

分,0-59
时,0-23
日,1-31
月,1-12
周,1-6 (0、7表示周日)

可用格式1-5 表示一个范围 可用格式*/2表示被2整除的数字,比如小时,那就是每隔2个小时 要保证服务是启动状态 systemctl start crond.service

`0 3 * * * /bin/bash /root/sh/123.sh  >>/tmp/123.log  2>>/tmp/321.log`
每天的3点00分 执行一个123.sh脚本,把正确结果放入123.log,错误结果放入321.log里

chkconfig

chkconfig 系统服务管理 Centos 7 还能兼容使用

chkconfig - -list       # 查看所有服务
chkconfig network off        #开机关闭服务  on开机自启动
chkconfig - -level 2 off         #2级别开机关闭

首先先放到/etc/init.d 目录下

chkconfig - -add       #123 手动加入开机自启动
chkconfig - -del        #123 手动删除

6以及之前的版本 chkconfig 使用的服务管理的机制是 SysV 7个运行级别 runlevel

0 关机
1 单用户
2 和3级别只少了NFS服务
3 多用户级别
4 保留
5 图形界面
6 重启 reboot 

systemd

centos 7 使用的服务管理机制是systemd 、target

systemd 系统管理服务

systemctl list-unit-files    #查看所有的包括service、target、socket服务
systemctl list-unlis - -all - -type=service    #只查看所有service服务
systemctl list-units - -type=service    #只查看所有service服务但不包括未激活的任务
systemctl enable crond.service    #开机自启动
systemctl disable crond.service    #关闭自启动
systemctl status crond.service    #查看指定服务状态
systemctl start crond.service    #启动服务
systemctl stop crond,service     #关闭服务
systemctl restart crond.service    #重启服务
systemctl is-enable crond     #检查服务是否开机自启动

unit (systemd)

ls /usr/lib/systemd/system 系统所有unit,分为以下类型

service     #系统服务
target     #多个unit(service)组成的组
device     #硬件设备
mount     #文件系统挂载点
automount     #自动挂载点
path     #文件或路径
scope     #不是由systemd启动的外部进程      
slice     #进程组
snapshot systemd     #快照
socket     #进程间通信套接字
swapswap     #文件
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
systemctl is-enable crond.service     #查看摸个服务是否为enable

target (systemd)

系统为了方便管理用target来管理unit

systemctl list-unit-files - -type=target
systemctl list-dependencies multi-user.target     #查看指定target下面有哪些unit 
systemclt get-default      #查看系统默认的target
systemctl set-default multi-user.target

一个service属于一种类型的unit 多个unit组成了一个target 一个target里面包含了多个service cat /usr/lib/systemd/system/sshd.service 看[install] 部分

+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++

扩展

  1. anacron

  2. xinetd服(默认机器没有安装这个服务,需要yum install xinetd安装)

  3. systemd自定义启动脚本