10.23 linux任务计划cron
10.24 chkconfig工具
10.25 systemd管理服务
10.26 unit介绍
10.27 target介绍
扩展
1. anacron http://blog.csdn.net/strikers1982/article/details/478722
2. xinetd服(默认机器没有安装这个服务,需要yum install xinetd安装) http://blog.sina.com.cn/s/blog_465bbe6b010000vi.html
3. systemd自定义启动脚本 http://www.jb51.net/article/100457.htm
一、linux任务计划cron
linux中任务计划必不可以少,它可以定时执行一些备份数据、重启服务器等操作,操作可能是一个脚本或者命令。
crontab命令:
- 格式:分 时 日 月 周 user command
分范围:0-59,时范围:0-60,日范围:0-31,月范围1-12,周范围:1-7
指定范围:1-5 ,指定1到5范围
可以用1,2,3表示1或者2或者3
*/2表示被2整除的数字,比如小时,那就是每隔2小时
- 文件/var/spool/cron/username
- 可用格式1-5表示一个范围1到5
- 可用格式1,2,3表示1或者2或者3
- 可用格式*/2表示被2整除的数字,比如小时,那就是每隔2小时
- 要保证服务是启动状态
- systemctl start crond.service
- 任务计划的配置文件:
cat /etc/crontab
文件中有几个环境变量:
- SHELL=/bin/bash
- PATH:环境变量,命令的路径
- MAILTO:发邮件给谁
下面的是crontab格式说明:
# 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 //需要执行的命令
- 新增定时任务:
crontab -e //进入编辑模式,和vim一样,按i进行编辑
新增一个凌晨3点执行的脚本,*代表所有的意思。并且把日志和错误日志输出
新增一个1-10号,双月执行的脚本,也就是每两个月才会执行的脚本,*/2这样就可以符合条件了
0 3 1-10 */2 * /bin/bash /usr/local/sbin/test.sh /tmp/test.log 2>> /tmp/test2.log
新增一个周二、周五才执行的脚本:
0 3 * * 2,5 /bin/bash /usr/local/sbin/test.sh /tmp/test.log 2>> /tmp/test2.log
如果想用年份可以用星期确定唯一性,比如说今年的6月18号和明年的6月18号的星期肯定是不同的
- 需要启动crond服务,定时任务才会生效
systemctl start crond.service //启动crond服务
- 检查是否启动成功
ps aux |grep cron
或者:systemctl start crond 查看,如果状态是绿色就说明成功了
- 任务计划不执行的原因分析
写了定时任务,crond服务也是正常,但是就是不执行。
原因可能为:脚本中命令未使用绝对路径,解决:要么将命令写一个绝对路径,要么将命令的路径加入到PATH变量里面去
建议追加一个日志,这样就可以根据日志来判断。
- 任务计划备份
crontab文件存在位置/var/spool/cron/username,需要备份的时候直接复制即可。
二、chkconfig工具
chkconfig工具是linux服务的管理工具,cron、iptables、firewalld、mysql等都是服务。chkconfig可以管理这些服务开机是否启动等。
chkconfig工具在centos6和之前的版本中使用,centos7中已经逐渐的不在使用了,为了过度centos7还可以使用,但是后面趋势是会被遗弃。
- 列出系统所有服务
chkconfig --list
0-6 表示7个启动级别(centos6及以前):0:关机 1: 单用户 2:多用户模式(不带nfs服务) 3: 多用户模式(不带图形)
4: 保留级别 5: 多用户(带有图形) 6: 重启
- 需要注意的是:启动脚本需要放到/etc/init.d/目录下
- 关闭netword服务
chkconfig network off
- 指定某一服务的某个级别开启或关闭
chkconfig --level 3 network off //指定第三级别network服务关闭
chkconfig --level 345 network on //指定network中的3,4,5级别开启
- 将一个脚本加入到服务列表列表中
首先把脚本放到/etc/init.d/目录下
脚本名称没有要求,但是格式有要求:
1、必须是一个shell脚本
2、需指定运行级别,第10位开启,第90关闭
示例:init.d目录下 cp network服务 随便起一个名字,进行添加
[root@wxy01 init.d]# cp network 123
[root@wxy01 init.d]# chkconfig --add 123
结果:
删除:chkconfig --del 123
三、systemd管理服务
- systemctl list-units --all --type=service
- 几个常用的服务相关的命令
- systemctl enable crond.service //让服务开机启动
- systemctl disable crond //不让开机启动
- systemctl status crond //查看状态
- systemctl stop crond //停止服务
- systemctl start crond //启动服务
- systemctl restart crond //重启服务
- systemctl is-enabled crond //检查服务是否开机启动
在centos6或之前的版本中用chkconfig工具去管理系统的服务,在centos7中使用systemd。
- 列出所有的service
- 设置服务开机启动
systemctl enable crond.service //.service 可以不加
- 关闭开机启动
systemctl disable crond
- 查看状态
systemctl status crond
- 启动服务
systemctl start crond
- 停止服务
systemctl stop crond
- 检查服务是否开机启动
systemctl is-enabled crond
四、unit介绍
lssystemctl 状态如果是enabled 会在 /usr/lib/systemd/system 目录下生产软连接,这些文件都叫做unit。
unit分为以下类型:
- service 系统服务
- target 多个unit组成的组
- device 硬件设备
- mount 文件系统挂载点
- automount 自动挂载点
- path 文件或路径
- scope 不是由systemd启动的外部进程
- slice 进程组
- snapshot systemd快照
- socket 进程间通信套接字
- swap swap文件
- timer 定时器
unit相关的命令
列出正在运行的unit:
systemctl list-units
列出所有的unit:
systemctl list-units --all
列出inactive状态的unit
systemctl list-units --all --state=inactive
列出状态为active的service
systemctl list-units --type=service
查看某个服务是否为active
systemctl is-active crond.service
五、target介绍
- 系统为了方便管理target来管理unit
target相关的命令
列出系统中所有的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
- 一个service属于一种类型的unit
- 多个unit组成了一个target
- 一个target里面包含了多个service
- cat /usr/lib/systemd/system/sshd.service 看[install]部分
/