码迷,mamicode.com
首页 > 其他好文 > 详细

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

时间:2018-03-08 15:55:29      阅读:219      评论:0      收藏:0      [点我收藏+]

标签:unit

                  Linux 任务计划 cron

技术分享图片

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

技术分享图片

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

crontab:

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

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

-l :列出计划任务

-r: 删除计划任务





进入编辑模式 : crontab -e


技术分享图片

技术分享图片

(编写任务计划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小时

技术分享图片表示每天凌晨3点 每个月1-20号 每年的双月,每周的星期2、5去运行任务。


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

  systemctl start crond


检查是否启动成功:

方法一:ps aux |grep cron

方法二:systemctl status crond

技术分享图片

 (停止crond 启动:systemctl stop crond )




 (  要保证服务是启动状态

 systemctl start crond.service)



制定一个计划:

技术分享图片

crontab -l (列出计划任务)

技术分享图片


 crontab文件在哪里呢?

 /var/spool/cron

技术分享图片

(查看root用户的crontab文件。

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


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

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




         Linux系统服务管理-chkconfig

                   chkconfig工具

技术分享图片


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


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

chkconfig --list

技术分享图片

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

 netconsole和network服务所在处?

 /etc/init.d

技术分享图片


把一个服务状态做变更:

 chkconfig network off 

技术分享图片

chkconfig network on

技术分享图片

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

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

技术分享图片


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

1.首先是个shell脚本;

2.

技术分享图片


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




                                       systemd管理服务

                        Linux系统服务管理-systemd

查看系统所有的service:

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

技术分享图片

提示命令:

技术分享图片

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

(1)让服务开机启动

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

(2)不让开机启动

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

技术分享图片

 ( 3 )查看状态:

  systemctl status crond

技术分享图片

(4)停止服务

   systemctl stop crond

(5)启动服务

  systemctl start crond

 (6)重启服务

  systemctl restart crond

 

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

 systemctl is-enabled crond

技术分享图片


技术分享图片技术分享图片技术分享图片

技术分享图片

技术分享图片


enable 会创建一个软链接、disable 则会删除软链接 (软连接文件的目标院文件技术分享图片

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

                        

                             unit介绍

技术分享图片


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

ls /usr/lib/systemd/system 

技术分享图片

 类型:


 (1) service 系统服务 

 (2)target 多个unit组成的组

 技术分享图片

 

  (3)device 硬件设备

  (4)mount 文件系统挂载点

  (5)automount 自动挂载点

  (6)path 文件或路径

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

  (8)slice 进程组

  (9)snapshot systemd快照

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

  (11)swap  swap文件

    (12)timer 定时器




技术分享图片

1.列出正在运行的unit:

 systemctl list-units   

技术分享图片

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

    systemctl list-units --all

 3.列出inactive的unit

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

技术分享图片


4.列出状态为active的service

systemctl list-units --type=service

技术分享图片


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

(如crond服务)

systemctl is-active crond.service


 查看某个服务是否为enable

systemctl is-enabled crond.service

技术分享图片



                                    target介绍

技术分享图片

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

系统用target管理unit

1.列出系统里所有的target

systemctl list-unit-files --type=target

技术分享图片


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

技术分享图片


3.查看系统默认的target:

   systemctl get-default

技术分享图片

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


4.设置系统默认的target

systemctl set-default multi-user.target

技术分享图片


技术分享图片




一个target里包含了多个service


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

(如:sshd.service)

技术分享图片


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





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

标签:unit

原文地址:http://blog.51cto.com/13589255/2084208

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!