linux任务计划cron:

任务计划配置文件:

/etc/crontab

java cron每周二 周三执行_运维

crontab -u、-e、-l、-r:

crontab -e 编辑任务计划

crontab -l 查看任务计划

crontab -r 删除所有的任务计划

crontab -u 指定一个用户

例如:crontab -u root -l 列出root用户的任务计划

格式:分 时 日 月 周 user command

分:范围0-59

时:范围0-23

日:范围1-31

月:范围1-12

周:范围0-6,0表示周日,也可以用7表示周日

可用格式1,2,3表示1或者2或者3

可用格式*/2表示被2整除数字,比如小时,那就是每隔2小时

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

java cron每周二 周三执行_运维_02

文件/var/spool/cron/username

命令crontab -e实际上打开了 /va/spool/cron/username文件(如果是root则打开了/var/spool/cron/root文件)

要保证服务是启动状态

systemctl start crond.service

ps aux | grep crond

查看服务状态:

systemctl status crond.service

java cron每周二 周三执行_操作系统_03



chkconfig工具:

CentOS 6上的服务管理工具为chkconfig,linux系统所有的预设服务都可以通过查看/etc/init.d/目录得到。CentOS 7中已经不再延续CentOS 6版本的服务管理方案了,但是我们依然可以继续使用chkconfig这个命令。系统预设服务都可以通过这样的命令实现:service 服务名 start | stop | restart。这里的服务名就是/etc/init.d/目录下的这些文件了。启动crond除了可以使用service crond start外,还可以使用/etc/initd.d/crond start。

chkconfig --list

列出所有服务以及每个级别的开启状态

java cron每周二 周三执行_运维_04

chkconfig --level 3 network off

--level 指定启动的级别,后面跟服务名,然后是off或则on

在3级别时network服务关掉

chkconfig --level 345 network off

--level后面可以指定多个级别

chkconfig nginx off | on

nginx在所有级别上都是关| 开的,其中0 1 6 这些级别服务都是关的。

chkconfig --del network

chkconfig可以把某个服务从系统服务中删除

chkconfig --add network

把可执行的脚本文件放到/etc/init.d/目录下后,可以使用此命令加入到系统服务



systemd管理服务:

列出系统所有的服务:

systemctl list-units --all --type=service

java cron每周二 周三执行_操作系统_05

几个常用的服务相关的命令:

systemctl enable crond.service //让服务开机启动

java cron每周二 周三执行_python_06

crond服务的路径是/usr/lib/systemd/system/crond.service,开启时会在/etc/systemd/system/multi-user.target.wants/目录下生成一个软链接

systemctl disable crond //不让开机启动

java cron每周二 周三执行_java cron每周二 周三执行_07

会删掉开启服务时生成的软链接

systemctl status crond //查看状态

java cron每周二 周三执行_任务计划_08

systemctl stop crond //停止服务

systemctl start crond //启动服务

systemctl restart crond //重启服务

systemctl is-enabled crond //检查服务是否开机启动

java cron每周二 周三执行_java cron每周二 周三执行_09

 



unit介绍:

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

• service 系统服务

• target 多个unit组成的组

• device 硬件设备

• mount 文件系统挂载点

• automount 自动挂载点

• path 文件或路径

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

• slice 进程组

• snapshot systemd快照

• socket 进程间通信套接字

• swap  swap文件

• timer 定时器

以上每种类型的文件都为一个unit,正是这些unit才组成了系统的各个资源(各个服务,各个设备等)

unit相关的命令:

systemctl list-units //列出正在运行的unit

java cron每周二 周三执行_java cron每周二 周三执行_10

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



target介绍:

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

systemctl list-unit-files --type=target

列出系统的所有target

systemctl list-dependencies multi-user.target //查看指定target下面有哪些unit

systemctl get-default //查看系统默认的target

systemctl set-default multi-user.target //设置默认的target

ls -l /etc/systemd/system/default.target

查看系统支持的target启动级别:

centos 7中系统启动级别的定义已经和centos 6中不一样了

一个service属于一种类型的unit

多个unit组成了一个target

一个target里面包含了多个service

cat /usr/lib/systemd/system/sshd.service //看[install]部分

查看sshd.service属于哪个target



扩展

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