Linux 任务计划 cron

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_任务计划

任务计划的配置文件: /etc/crontab

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_任务计划_02

(图中:MAILTO 表示发送任务计划给谁)

crontab:

-u:表示指定某个用户,不加-u则为当前用户

-e :表示制定计划任务(进入到编辑模式)

-l :列出计划任务

-r: 删除计划任务





进入编辑模式 : crontab -e


十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_unit_03

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_unit_04

(编写任务计划crond服务是如果执行一个bash/命令,写它的绝对路径)

指定执行范围:

  (

 分范围0-59,时范围0-23,日范围1-31,月范围1-12,周1-7

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

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

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

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_任务计划_05表示每天凌晨3点 每个月1-20号 每年的双月,每周的星期2、5去运行任务。


编写完任务需要启动crond这个服务需要执行:

  systemctl start crond


检查是否启动成功:

方法一:ps aux |grep cron

方法二:systemctl status crond

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_任务计划_06

 (停止crond 启动:systemctl stop crond )




 (  要保证服务是启动状态

 systemctl start crond.service)



制定一个计划:

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_任务计划_07

crontab -l (列出计划任务)

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_任务计划_08


 crontab文件在哪里呢?

 /var/spool/cron

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_unit_09

(查看root用户的crontab文件。

   如果查看其他用户的crontab文件: cat /var/spool/cron/用户名


备份计划任务:cp  /var/spool/cron/用户名 /绝对路径/

           cp  /var/spool/cron/root /tmp/cansheng/root.bk




         Linux系统服务管理-chkconfig

                   chkconfig工具

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_unit_10


(在centos7之前都是用chkconfig工具的,但是在centos7后也能用,但是不作为里面的工具)


查看当前有哪些系统服务使用chkconfig工具:

chkconfig --list

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_unit_11

(此时只有netconsole和network服务了)

 netconsole和network服务所在处?

 /etc/init.d

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_任务计划_12


把一个服务状态做变更:

 chkconfig network off 

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_unit_13

chkconfig network on

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_unit_14

指定某级别的状态:关闭/开启

chkconfig --level 3 network off 

chkconfig --level 235 network off (切记不要把0和6开启)



修改配置文件,更改运行级别:vi /etc/inittab (在centos7已经是不使用)




把一个自定义的脚本加入到我们的服务列表:(例如自定义脚本名为:123)

1.进入到 /etc/init.d文件中

2.把自定义脚本(123)放到 /etc/init.d 下(mv/cp

3.chkconfig --add 123

4.查看是否加进来了:chkconfig --list

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_unit_15


自定义的脚本名字可以自定义,但是内容有格式:

1.首先是个shell脚本;

2.

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_unit_16


删除自定义的服务:chkconfig --del 123




                                       systemd管理服务

                        Linux系统服务管理-systemd

查看系统所有的service:

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

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_任务计划_17

提示命令:

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_任务计划_18

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

(1)让服务开机启动

  systemctl enable  crond.service   (.service可以省略)

(2)不让开机启动

  systemctl disable  crond.service   (.service可以省略)disable表示开机不再启动

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_任务计划_19

 ( 3 )查看状态:

  systemctl status crond

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_任务计划_20

(4)停止服务

   systemctl stop crond

(5)启动服务

  systemctl start crond

 (6)重启服务

  systemctl restart crond

 

  (7)检查服务是否开机启动:

 systemctl is-enabled crond

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_任务计划_21


十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_unit_03十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_unit_03十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_unit_03

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_unit_03

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_unit_26


enable 会创建一个软链接、disable 则会删除软链接 (软连接文件的目标院文件十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_unit_27

/usr/lib/systemd/system/crond.service)

                        

                             unit介绍

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_unit_28


查看系统所有unit ,及其类型:

ls /usr/lib/systemd/system 

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_任务计划_29

 类型:


 (1) service 系统服务 

 (2)target 多个unit组成的组

 十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_unit_30

 

  (3)device 硬件设备

  (4)mount 文件系统挂载点

  (5)automount 自动挂载点

  (6)path 文件或路径

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

  (8)slice 进程组

  (9)snapshot systemd快照

  (10)socket 进程间通信套接字

  (11)swap  swap文件

    (12)timer 定时器




十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_unit_31

1.列出正在运行的unit:

 systemctl list-units   

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_任务计划_32

2.列出所有的unit,包括失败或者inactive的

    systemctl list-units --all

 3.列出inactive的unit

systemctl list-units --all --state=inactive

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_任务计划_33


4.列出状态为active的service

systemctl list-units --type=service

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_任务计划_34


5.  查看某个服务是否为active

(如crond服务)

systemctl is-active crond.service


 查看某个服务是否为enable

systemctl is-enabled crond.service

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_任务计划_35



                                    target介绍

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_任务计划_36

(一个target由若干个unit组成)

系统用target管理unit

1.列出系统里所有的target

systemctl list-unit-files --type=target

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_任务计划_37


2.查看指定target下面有哪些unit (如:multi-user.target)

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_任务计划_38


3.查看系统默认的target:

   systemctl get-default

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_unit_39

multi-user.target等同于centos6的运行级别3


4.设置系统默认的target

systemctl set-default multi-user.target

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_unit_40


十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_unit_41




一个target里包含了多个service


查看某个服务属于哪个service 。看[install]部分

(如:sshd.service)

十(6)任务计划 cron、chkconfig工具、systemd管理服务、unit、target_unit_42


target是由多个unit组成的,service属于unit的一类