Linux任务计划 cron

Linux中crontab是用来定期执行程序的命令。当安装完成操作系统之后,默认就会启动此任务调度命令。cron命令每分钟都会定期检查是否有要执行的工作,如果有要执行的工作便会自动执行该工作。而linux任务调度的工作任务主要分为两类:1.系统执行的工作:系统周期性需要执行的工作,如备份系统数据、清理缓存。2.个人执行的工作:某个用户定期要做的工作,例如每隔10分钟检查右键服务器是否有新信,这些工作可由每个用户自行设置。

  • 命令格式:crontab [-u user] file

    • crontab 是用来让使用者在固定时间或固定间隔执行程序只用,换句话说,也就是类似使用者的任务计划。
    • -u user 是指设定指定user的任务计划,这个前提是你必须要有其权限(比如说是root)才能够指定他人的任务计划。如果不使用-u user的话,就是表示设定自己的任务计划。
    • -e :执行文字编辑器来设定任务计划,内定的文字编辑器是vi。
    • -r :删除目前的时程表
    • -l :列出目前的时程表
  • crontab命令的配置文件/etc/crontab

    [root@localhost ~]# 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

  • 使用命令crontab -e进入crontab的配置文件。

    0 3 * * * /bin/bash /uer/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log

在每天凌晨3点执行这个脚本,并将正确错误重定向输出到指定文件中。其中使用命令时使用命令的绝对路径,不知道命令的绝对路径可以使用which查看命令的绝对路径。

0 3 1-10 */2 2,5 user1 /bin/bash /usr/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log

使用user1用户在双数月的1-10号的周二和周五执行脚本,并将正确错误重定向输出到指定文件中。其中*/2表示能被2整除的月,也可以表示为每隔两个月。

  • 想要使刚才设定的计划生效需要启动crond,使用命令systemctl start crond。

    root@localhost ~]# systemctl start crond #启动crond [root@localhost ~]# systemctl status crond #查看crond的状态 ● crond.service - Command Scheduler Loaded: loaded (/usr/lib/systemd/system/crond.service; enabled; vendor preset: enabled) Active: active (running) since 一 2017-12-04 14:46:50 CST; 43min ago Main PID: 516 (crond) CGroup: /system.slice/crond.service └─516 /usr/sbin/crond -n ......

启动成功后,Active:active(runing),如果没有启动,Active:inactive(dead)。

  • 命令syttemctl stop crond 可以使停止crond。

注意:在编写脚本或者任务计划时,所有的命令都要使用绝对路径。在写任务计划的时候每个人物追加一个日志。

  • 查看现有的任务计划

    [root@localhost ~]# crontab -l3

查看当前用户现有的任务计划

  • 文件/var/spool/cron/root的内容为root用户的任务计划。

普通用户的任务计划在/var/spool/cron/用户名 这个文件中。备份用户计划,拷贝一份这个文件就可以。

  • crontab -r 删除当前用户的任务计划
  • crontab -u user1 -r删除user1用户的任务计划。。

Linux系统服务管理-chkconfig

Linux中chkconfig命令用于检查、设置系统的各种服务。chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接(该命令多用于centos6及以前版本)。

  • chkconfig --list 查看当前系统中使用chkconfig的工具的服务都有哪些

    [root@localhost ~]# 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:关

  • 服务所在的位置:/etc/init.d/

    [root@localhost ~]# ls /etc/init.d functions netconsole network README

  • 更改某一服务的状态

    [root@localhost ~]# chkconfig network off/on

打开或关闭 network服务。

  • 更改某一服务的某一运行级别状态

    root@localhost ~]# chkconfig --level 345 network off/on

打开或关闭 345 运行级别。其中,0级别:关机状态;1级别:单用户模式;2级别:比三级别少nfs模式;3级别:多用户模式不带图形;4级别:保留状态;5级别:多用户模式,带图形;6级别:重启。

注意:运行级别配置文件:“/etc/inittab”,centos7已不再使用该文件。

  • 添加或删除服务

    • 首先要将服务脚本放到/etc/init.d/目录下,并添加执行权限,
    • 然后运行命令chkconfig --add /etc/inti.d/脚本名或者直接chkconfig --add 脚本名,将脚本添加到服务列表。
    • 删除列表中的脚本 chkconfig --del 脚本名。
  • 服务列表对脚本的格式的要求 Markdown

systemd 管理服务

systemd 是centos7的服务管理命令。

  • systemctl list-unit -flies

显示所有服务

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

显示所有类型为service的服务。

  • systemctl list-units --type=service

显示所有类型为service并且状态为active的服务。

  • systemctl enable crond

让crond服务开机启动

  • systemctl diable crond

不让crond服务开机启动

  • systemctl status crond

查看crond服务状态

  • systemctl stop crond

停止crond服务

  • systemctl start crond

启动crond服务

  • systemctl restart crond

重启crond服务

  • systemctl is-enabled crond

检查crond服务是否开机启动

[root@localhost ~]# systemctl disable crond
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@localhost ~]# systemctl enable crond
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.

伴随某服务的开/关同时会建立/删除一个指向该服务的软链接“/etc/systemd/system/multi-user.target.wants/crond.service”-->“/usr/lib/systemd/system/crond.service”

unit 介绍

systemd可以管理所有的系统资源。不通的系统资源统称为unit(单位)。

  • /usr/lib/systemd/system/下的文件都叫做unit。

unit的文件类型:

后缀 类型
sercice 系统服务
target 多个unit组成的组
device 硬件设备
mount 文件系统挂载点
autoamount 自动挂载点
path 文件或路径
scope 非systemd启动的外部程序
snapshot systemd快照
socket 进程间通信套接字
swap swap文件
timer 定时器

unit相关的命令

  • systemctl list-unit

列出正在运行的unit

  • systemctl list -unit --all

列出所有unit(包括非运行状态的或启动失败的)

  • systemctl list-unit --all --status=inactive

列出状态(status)为inactive的unit

  • systemctl list-unit type=service

列出状态为active的service(不加--all将会列出状态为active的服务。)

  • systemctl is-active/enable crond

查看某服务是否为active/enable状态

target 介绍

系通为了方便管理,用target管理unit。

  • systemctl list-unit-files--type=target

查看所有的target文件

  • systemctl list-dependencies multi-user.target

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

  • systemctl get-default

查看系统默认target

  • systemctl set-difault multi-user.target

给指定(multi-user.target)设置系统默认target

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

  • 查看一个service属于哪个target

查看文件/usr/lib/systemd/system/xxx.service 中的install项中的WantedBy可以知道service属于哪个target。

[root@localhost ~]# cat /usr/lib/systemd/system/sshd.service
......
[Install]
WantedBy=multi-user.target

由上可知,sshd.service属于multi-user.target