扩展:

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任务计划cron

任务计划:我们可能凌晨备份数据,重启服务等等,这样在某个时间自动的执行脚本或命令,

1. 查看 任务计划配置文件 (写入格式)

[root@hao-001 ~]# cat /etc/crontab

clipboard.png

2. 编写 任务计划

[root@hao-001 ~]# crontab -e

i 进入编辑模式

*表示全部,

每天的凌晨三点都执行,不限定日月周  执行12.sh脚本, 正确输出日志12.log  错误输出日志21.log

0分 3时 * * * /bin/bash /usr/local/sbin/12.sh >>/tmp/12.log 2>>/tmp/21.log

例1:

12  14 1-20号  每隔2个  星期1-星期6   执行命令  执行的脚本  >>正确输出日志路径  2>>错误输出日志路径

12  14  1-20  */2   1-6  /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/321.log

例2:

格式: 分 时 日 月 周 执行命令  执行的脚本  >>正确输出日志路径  2>>错误输出日志路径

格式: 分 时 日 月 周 /bin/bash  /usr/local/sbin/123.sh >>/tmp/1234.log  2>>/tmp/4321.log

详解:

*  表示(////)全部范围   注意: 执行命令要填写绝对路径!!!

范围:0-59

范围:0-23

范围:1-31

范围:1-12

范围:0-6(0表示星期天,7也表示星期天)

可用格式:  1-5(表示一个范围1到5)

可用格式:  1,2,3(表示1或2或3)  比如:星期一,星期二,星期三

可用格式:  */2(表示被2整除的数字)比如小时,每隔2小时; 比如月,双月,被2整除的月,每隔两个月

3. 启动 crond进程 :

[root@hao-001 ~]# systemctl start crond

4. 搜索 crond进程是否启动 ?

[root@hao-001 ~]# ps aux |grep crond

clipboard.png

5. 查看 crond进程状态(判断是否启动?图中绿色表示启动了) :

[root@hao-001 ~]# systemctl status crond

clipboard.png

6. 关闭 crond进程 :

[root@hao-001 ~]# systemctl stop crond

7. 列出 任务计划

[root@hao-001 ~]# crontab -l

8. 列出 指定用户任务计划

[root@hao-001 ~]# crontab -u root -l

9. 查看 任务计划存放目录(文件是用户名命名文件) :

(备份用户的任务计划,直接备份这个cron目录即可!!!)

[root@hao-001 ~]# ls /var/spool/cron/

10. 删除 任务计划

[root@hao-001 ~]# crontab -r

10.24 chkconfig工具(服务)

1. 列出 当前系统使用chkconfig工具的服务有哪些?运行级 是什么?

[root@hao-001 ~]# chkconfig --list

clipboard.png

注意:2级别  3级别  4级别  5级别 根据自己需求,可开/可关

注意:0级别 1级别 6级别  必须

0级别   关机状态

1级别   单用户模式

2级别   多用户模式(带nfs服务)

3级别   多用户模式(不带图形,没有nff服务)

4级别   保留状态(暂时没用)

5级别   多用户模式(带图形)

6级别   重启

2. 查看 启动脚本服务(文件脚本) :

[root@hao-001 ~]# ls /etc/init.d

clipboard.png

3. 关闭 network服务 3级别 :

[root@hao-001 ~]# chkconfig --level 3 network off

4. 关闭 network服务 2级别3级别4级别5级别 :

[root@hao-001 ~]# chkconfig --level 2345 network off

5. 开启 network服务 2级别3级别4级别5级别 :

[root@hao-001 ~]# chkconfig --level 2345 network on

6. 查看 指定的network服务 运行级别 :

[root@hao-001 ~]# chkconfig --list network

7.0 进入 cd /etc/init.d目录下:

[root@hao-001 ~]# cd /etc/init.d

7.1 创建 123脚本(自定义),到/etc/init.d目录下:

[root@hao-001 ~]# cp network 123

[root@hao-001 ~]# chkconfig --list

7.2 新增 自定义服务 chkconfig服务列表下 :

[root@hao-001 ~]# chkconfig --add 123

[root@hao-001 ~]# chkconfig --list

clipboard.png8. 删除 自定义服务

[root@hao-001 ~]# chkconfig --del network

注意:新增加的自定义服务脚本,按格式,添加到/etc/init.d/目录下

clipboard.png

10.25 systemd管理服务

1. systemd列出所有units服务,类型为servie

[root@hao-001 ~]# systemctl list-units --all --type=service

2. 未激活状态active不再列出

[root@hao-001 ~]# systemctl list-units  --type=service

3. crond服务 开机不启动

[root@hao-001 ~]# systemctl disable crond

4. crond服务 开机启动

[root@hao-001 ~]# systemctl enable crond.service

5. 检查 crond服务 是否开机启动

[root@hao-001 ~]# systemctl is-enabled crond

6. 查看 crond服务状态 :

[root@hao-001 ~]# systemctl status crond.service

clipboard.png

7. 停止 crond服务 :

[root@hao-001 ~]# systemctl stop crond

8. 启动 crond服务 :

[root@hao-001 ~]# systemctl start crond

9. 重启 crond服务 :

[root@hao-001 ~]# systemctl restart crond

10.26 unit介绍

1. 列出 系统所有unit

[root@hao-001 ~]# ls /usr/lib/systemd/system

unit分为以下类型:

service  系统服务

target    多个unit组成的组

device   硬件设备

mount   文件系统挂载点

automount   自动挂载点

path   文件或路径

scope   不是由systemd启动的外部进程

slice   进程组

snapshot   systemd快照

socket   进程间通信套接字

swap   swap文件

timer    定时器

unit相关的命令

1. 列出 正在运行unit

[root@hao-001 ~]# systemctl list-units  

2. 列出 所有的(包括失败的或者inactive的) :

[root@hao-001 ~]# systemctl list-units --all  

3. 指定列出 状态为inactiveunit

[root@hao-001 ~]# systemctl list-units --all --state=inactive  

4. 指定列出 状态为activeservice

[root@hao-001 ~]# systemctl list-units --type=service    

5. 指定查看 crond.service服务,是否为active

[root@hao-001 ~]# systemctl is-active crond.service  

6. 指定查看 crond.service服务,是否为enabled

[root@hao-001 ~]# systemctl is-enabled crond.service  

10.27 target介绍

•系统为了方便管理,用target管理unit

1. 列出 系统所有target

[root@hao-001 ~]# systemctl list-unit-files --type=target

2. 查看 指定target下面有哪些unit

[root@hao-001 ~]# systemctl list-dependencies multi-user.target

3. 查看 系统默认target

[root@hao-001 ~]# systemctl get-default

spacer.gifclipboard.png

4. 设置(更改) 默认target

[root@hao-001 ~]# systemctl set-default multi-user.target


•一个service一种类型unit

target多个unit组成的

• 一个target里面,包含了多个(若干)service

5. 查看 sshd.serviceservice,属于哪个target

[root@hao-001 ~]#  cat /usr/lib/systemd/system/sshd.service

spacer.gifclipboard.png