Day 34 linux任务计划cron

linux任务计划cron

  • 介绍
    大部分系统工做都是经过按期执行脚原本执行,这就要借助linux 的cron功能了
  • crontab命令
    linux的计划功能的操做都是经过crontab命令来完成的
  • 选项
    -u:表示指定某个用户,不加则为当前用户
    -e:表示指定计划任务
    -l:表示列出计划任务
    -r:表示删除计划任务
  • crontab的配置文件
    路径:cat /etc/crontab
    • 从左至右分别是分、时、日、月、周和命令行。由于每一年的周都和日都不会相同,因此能区分是否是同一年
[root@centos001 ~]# cat /etc/crontab 
SHELL=/bin/bash
PATH=/sbin:/bin:/usr/sbin:/usr/bin
MAILTO=root

# For details see man 4 crontabs

# Example of job definition:
# .---------------- minute (0 - 59)
# |  .------------- hour (0 - 23)
# |  |  .---------- day of month (1 - 31)
# |  |  |  .------- month (1 - 12) OR jan,feb,mar,apr ...
# |  |  |  |  .---- day of week (0 - 6) (Sunday=0 or 7) OR sun,mon,tue,wed,thu,fri,sat
# |  |  |  |  |
# *  *  *  *  * user-name  command to be executed

计划任务的语法

  • 分范围0-59,时范围0-23,日范围1-31,月范围1-12,周1-7
  • 可用格式1-5表示一个范围1到5
  • 可用格式1,2,3表示1或者2或者3
  • 可用格式*/2表示被2整除的数字,好比小时,那就是每隔2小时
  • 要保证服务是启动状态
0 3 1-10 */2 2,5 /bin/bash  /usr/local/sbin/123.sh > /tmp/123.sh >>/tmp/123.log>>/tmp/123.
log

检查crond服务是否开启

:该服务如不开启,将不会执行计划任务
1.开启服务命令:sytemctl start crond
2.查看服务是否开启命令一:ps aux |grep cron 如有最下的命令 则表示已开启html

[root@centos001 ~]# ps aux |grep cron
root       740  0.0  0.1 126232  1676 ?        Ss   12月04   0:00 /usr/sbin/crond -n
root      6460  0.0  0.0 112676   984 pts/0    S+   01:11   0:00 grep --color=autocron

3..查看服务是否开启命令二:systemctl status crond
linux

chkconfig工具

  • 介绍 1.centos6上的服务管理工具为chkconfig,centos7已经再也不延续6的服务管理方案了 2.linxu晓得全部系统预设服务均可已经过查看ls /etc/init.d/获得
[root@centos001 ~]# ls /etc/init.d/
functions  netconsole  network  README
  • chkconfig --list命令
    列出全部的服务及每一个级别的开启状态
[root@centos001 ~]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关	2:开	3:开	4:开	5:开	6:关
  • 说明 数字(0.6)为系统的启动级别(7以前的用法),0、1和6被系统保留。
    其中0做为shutdown动做,1做为重启至单用户模式,6为重启。
    通常的linux系统实现中,都使用了二、三、四、5几个级别。
    2别哦是无NFS支持的多用户模式,3表示彻底多用户模式(最经常使用),4表示保留个用户自定义,5表示图形登陆方式
  • 更改某级别下的开启状态
    • 指定某个级别是关闭仍是开启,固然但是多级别
[root@centos001 ~]# chkconfig --level 3 network off 
[root@centos001 ~]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关[root@centos001 ~]# chkconfig --level 345 network off
[root@centos001 ~]# chkconfig --list

注:该输出结果只显示 SysV 服务,并不包含
原生 systemd 服务。SysV 配置数据
可能被原生 systemd 配置覆盖。 

      要列出 systemd 服务,请执行 'systemctl list-unit-files'。
      查看在具体 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关	2:开	3:关	4:关	5:关	6:关
	2:开	3:关	4:开	5:开	6:关
  • 省略级别,默认是针对级别二、三、四、5的操做
  • 把某个服务加入系统或删除
chkconfig --del network
 chkconfig --add network

systemd管理服务

  • 介绍 systemd服务管理是在centos7中的,支持多个服务并发启动
    • 列出系统全部的服务
      systemctl list-units --all --type=service
  • 选项
    systemctl enable crond.service //让服务开机启动
    systemctl disable crond //不让开机启动
    systemctl status crond //查看状态
    systemctl stop crond //中止服务
    systemctl start crond //启动服务
    systemctl restart crond //重启服务
    systemctl is-enabled crond //检查服务是否开机启动

unit

  • 所在目录 /usr/lib/systemd/system/
  • 文件类型 service 系统服务
    target 多个unit组成的组
    device 硬件设备
    mount 文件系统挂载点
    automount 自动挂载点
    path 文件或路径
    scope 不是由systemd启动的外部进程
    slice 进程组
    snapshot systemd快照
    socket 进程间通讯套接字
    swap swap文件
    timer 定时器
  • 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
    systemctl is-active crond.service //查看某个服务是否为active

target

系统为了方便管理用target来管理unit
systemctl list-unit-files --type=target
systemctl list-dependencies multi-user.target //查看指定target下面有哪些unit
systemctl get-default //查看系统默认的target
systemctl set-default multi-user.target
一个service属于一种类型的unit
多个unit组成了一个target
一个target里面包含了多个service
cat /usr/lib/systemd/system/sshd.service //看[install]部分centos

扩展

  1. anacron
    http://blog.csdn.net/strikers1982/article/details/4787226bash

  2. xinetd服(默认机器没有安装这个服务,须要yum install xinetd安装)
    http://blog.sina.com.cn/s/blog_465bbe6b010000vi.html并发

  3. systemd自定义启动脚本
    http://www.jb51.net/article/100457.htmssh

相关文章
相关标签/搜索