一、 linux任务计划cron
关于cron任务计划功能的操作都是通过crontab这个命令来完成的。
其中常用的选项有:
-u :指定某个用户,不加-u选项则为当前用户;
linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍和target介绍讲解

-e :制定计划任务;
linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍和target介绍讲解

从左到右:在12月5日(这一天必须是星期2)的10点01分执行命令 echo “ok” > /root/cron.log

编写格式:分 时 日 月 周 user command

-l :列出计划任务;
linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍和target介绍讲解

-r :删除计划任务
linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍和target介绍讲解

备份:
crontab -e 实际上是打开了 “/var/spool/cron/username” (如果是root则打开的是/var/spool/cron/root)这个文件,所以不要直接去编辑那个文件,因为可能会出错,所以一定要使用 crontab -e 来编辑,另外备份的话,直接复制一份这个目录下的文件即可:
linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍和target介绍讲解

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

要保证服务是启动/停止状态:
systemctl start/stop crond

查看状态:
systemctl status crond
linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍和target介绍讲解

查看是否在运行:
ps aux |grep cron
linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍和target介绍讲解

制定计划建议都使用追加命令 >>,把正确和错误的都追加进一个文件里记录。

二、chkconfig工具(系统服务管理)
其实这就是系统所有的预设服务了,如network,cron 等等服务(service 服务名 start|stop|restart)

1、chkconfig –list
linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍和target介绍讲解

2、 chkconfig –level 3 network off
linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍和target介绍讲解
(不加 –level 3 就是将0-6个级别都关掉)

3、chkconfig –level 345 network off

linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍和target介绍讲解

4、chkconfig –add network
linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍和target介绍讲解

创建的文件必须是shell脚本,而且脚本里必须得有以下两行,否则不能被识别出来:
linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍和target介绍讲解

5、chkconfig –del network
linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍和target介绍讲解

三、 systemd管理服务

1、systemctl list-units –all –type=service
linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍和target介绍讲解

2、几个常用的服务相关的命令
systemctl enable crond.service //让服务开机启动
linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍和target介绍讲解

systemctl disable crond //不让开机启动
linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍和target介绍讲解

systemctl status crond //查看状态
linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍和target介绍讲解

systemctl stop crond //停止服务
systemctl start crond //启动服务
systemctl restart crond //重启服务

systemctl is-enabled crond //检查服务是否开机启动
linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍和target介绍讲解

3、ls /usr/lib/systemd/system //系统所有unit,分为以下类型
service 系统服务
target 多个unit组成的组
device 硬件设备
mount 文件系统挂载点
automount 自动挂载点
path 文件或路径
scope 不是由systemd启动的外部进程
slice 进程组
snapshot systemd快照
socket 进程间通信套接字
swap swap文件
timer 定时器

linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍和target介绍讲解

四、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
linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍和target介绍讲解

systemctl is-active crond.service //查看某个服务是否为active
linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍和target介绍讲解

五、target介绍
系统为了方便管理用target来管理unit :

systemctl list-unit-files –type=target

查看指定target下面有哪些unit:
systemctl list-dependencies multi-user.target
linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍和target介绍讲解

查看系统默认的target:
systemctl get-default
linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍和target介绍讲解

systemctl set-default multi-user.target

一个service属于一种类型的unit
多个unit组成了一个target
一个target里面包含了多个service

看一个service属于哪一个target
看[install]部分:
cat /usr/lib/systemd/system/crond.service
linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍和target介绍讲解

扩展
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

相关文章: