计划任务cron,chkconfig工具,systemd管理服务,unit,target

10.23 linux任务计划cronhtml

10.24 chkconfig工具linux

10.25 systemd管理服务vim

10.26 unit介绍windows

10.27 target介绍centos

扩展ssh

  1. anacron http://blog.csdn.net/strikers1982/article/details/4787226
  2. xinetd服(默认机器没有安装这个服务,须要yum install xinetd安装) http://blog.sina.com.cn/s/blog_465bbe6b010000vi.html
  3. systemd自定义启动脚本 http://www.jb51.net/article/100457.htm

10.23 linux任务计划crontab

crontab命令被用来提交和管理用户的须要周期性执行的任务,与windows下的计划任务相似,当安装完成操做系统后,默认会安装此服务工具,而且会自动启动crond进程,crond进程每分钟会按期检查是否有要执行的任务,若是有要执行的任务,则自动执行该任务。socket

  • 执行须要开启crond服务
systemctl start crond 
systemctl status crond
systemctl stop crond
//分别是开启、查看、中止crond服务
  • /etc/crontab 配置文件

crontab命令 命令效果 命令含义
crontab -e vim /var/spool/cron/root 编辑计划
crontab -l cat /var/spool/cron/root 查看计划
crontab -r rm /var/spool/cron/root 删除计划
crontab -ue user1 vim vim /var/spool/cron/user1 user1计划
[root@axiang ~]# crontab -e  //设置任务计划
[root@axiang ~]# systemctl start crond
[root@axiang ~]# ps aux | grep cron
root       527  0.0  0.1 126220  1612 ?        Ss   20:18   0:00 /usr/sbin/crond -n
root      2593  0.0  0.0 112664   972 pts/0    S+   21:51   0:00 grep --color=auto cron
[root@axiang ~]# systemctl status crond //查看状态以下图

[root@axiang ~]# crontab -l //查看当前设置
1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f
[root@axiang ~]# cat /var/spool/cron/root //若是想备份、从这里复制
1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f
[root@axiang ~]# crontab -r //删除
[root@axiang ~]# crontab -l
no crontab for root
[root@axiang ~]# crontab -u root -l //制定用户
crontab -e 
//编写任务,进入相似vim的页面写任务计划

时间可选描述方式:工具

  1. 分范围0-59,时范围0-23,日范围0-31,月范围0-12,周1-6
  2. 可用格式1-5表示一个范围1到5
  3. 可用格式1,2,3表示1或者2或者3
  4. 可用格式*/2表示被2整除的数字,好比小时,那就是每隔2小时

小实验: 注意:命令尽可能使用绝对路径,建议追加执行日志“>>/tmp/rigth.log 2>>/tmp/wrong.log”centos7

10.24 chkconfig工具

chkconfig命令检查、设置系统的各类服务。这是Red Hat公司遵循GPL规则所开发的程序,它可查询操做系统在每个执行等级中会执行哪些系统服务,其中包括各种常驻服务。谨记chkconfig不是当即自动禁止或激活一个服务,它只是简单的改变了符号链接(该命令多用于centos6及之前版本)。操作系统

常见参数

  • --list:查看在使用chkconfig命令的服务的状态
  • --add:增长指定服务
  • --del:删除指定服务
  • --level:指定某系统服务要在系统某运行级别中开启或关毕。

示例:

chkconfig --list 查看当前系统服务状态
ls /etc/init.d/ 查看现有服务 chkconfig --level 345 network off/on 开启某一级别的服务

运行级别配置文件:“/etc/inittab”,centos7已再也不使用该文件。

  • 添加/删除服务
    首先,在添加服务以前必须把该服务的脚本按规定格式 放到“/etc/init.d/”目录下并添加执行权限。而后执行命令:
ls /etc/init.d/
123  functions  netconsole  network  README

添加/删除:
chkconfig --add /etc/init.d/123
chkconfig --list

123            	0:关	1:关	2:开	3:开	4:开	5:开	6:关
netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关	2:开	3:开	4:开	5:开	6:关

chkconfig --del /etc/init.d/123

10.25 systemd管理服务

systemctl命令是系统服务管理器指令,它实际上将 service 和 chkconfig 这两个命令组合到一块儿。

systemctl命令

任务 旧指令 新指令
使某服务开机启动 chkconfig --level 3 httpd on systemctl enable httpd.service
使某服务开机不启动 chkconfig --level 3 httpd off systemctl disable httpd.service
检查服务状态 service httpd status systemctl status httpd.service(服务详细信息),systemctl is-active httpd.service(仅显示状态为Active的服务信息)
显示全部已启动的服务 chkconfig --list systemctl list-units (--all)--type=service(--all同时显示inactive状态的服务)
启动某服务 service httpd start systemctl start httpd.service
中止某服务 service httpd stop systemctl stop httpd.service
重启某服务 service httpd restart systemctl restart httpd.service
检测某服务是否开机启动 systemctl is-enabled httpd.service
systemctl enable crond
systemctl disable crond
ls /etc/systemd/system/
  • 伴随某服务的开/关会创建/删除一个指向该服务的软连接“/etc/systemd/system/multi-user.target.wants/crond.service”-->“/usr/lib/systemd/system/crond.service”

10.26 unit介绍

  • unit所在目录:/usr/lib/systemd/system/

  • unit文件类型

后缀 类型
service 系统服务
target 多个unit组成的组
device 硬件设备
mount 文件系统挂载点
autoamount 自动挂载点
path 文件或路径
scope 非systemd启动的外部程序
snapshot systemd快照
socket 进程间通讯套接字
swap swap文件
timer 定时器
  • unit相关命令
命令 任务
systemctl list-units 列出正在运行的unit
systemctl list-units --all 列出全部unit(包括非运行状态的或启动失败的)
systemctl list-units --all --status=inactive 列出inactive状态的unit
systemctl list-units --type=service 列出状态为active的service
systemctl is-active/enabled crond 查看某服务是否为active/enabled状态

10.27 target介绍

系统为了方便管理,因此使用target来管理unit。

  • 相关操做:
任务 命令
查看全部target文件 systemctl list-unit-files --type=target
查看指定target下面有哪些unit systemctl list-dependencies multi-user.target
查看系统默认target systemctl get-default
设置系统默认target systemctl set-default multi-user.target

target、service、unit关系

  • 一个service属于一种类型的unit,多个unit组成一个target,一个target包含多个service。

  • cat /usr/lib/systemd/system/sshd.service //查看service属于那个target

相关文章
相关标签/搜索