三十四、Linux系统任务计划cron、chkconfig工具、systemd管理服务、unit介绍、target介绍

一、Linux系统任务计划cron

crontab命令:对任务计划功能的操作用此命令。选项:

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

-e:制定任务计划。

-l:列出任务计划。

-r:删除任务计划。

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

文件内共有五个字段。

从左往右依次为:分、时、日、月、周、用户、命令。

可以不指定用户就是root。

# crontab -e   //编写任务计划,实际是用vim打开了crontab配置文件。

此时打开的配置文件的/var/spool/cron/username。(若用户是root,则cron后是root)。不能vi编辑这个文件,只能用crontab -e编辑,否则出错。

*表示所有。如每周,每天,每月。

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

比如执行一个脚本。

每个字段都可以写范围:

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

1-10日,被2整除的月,周二和周五

# systemctl start crond  启动crond服务,要使用任务计划,就要启动。

# ps aux |grep cron

root        589  0.0  0.0 126236  1612 ?        Ss   01:51   0:00 /usr/sbin/crond -n

root       1460  0.0  0.0 112676   980 pts/0    S+   04:26   0:00 grep --color=auto cron

有这个进程就说明服务已经启动了。

# systemctl status crond   查看它的状态,若启动,则显示绿色的active (running)。若未启动,则无字体有颜色显示,inactive (dead)。

配置文件内写任务计划时,命令写绝对路径,否则可能不执行,因为没在环境变量里。

每次写任务计划时,将正确日志和错误日志都追加保存起来。出问题时就有据可查。

二、Linux系统服务管理-chkconfig

centos6上的服务管理工具是chkconfig,系统内所有预设服务都可以通过/etc/init.d查看到。centos7已经不再延续centos6的服务管理方案了。因此只有几个文件。

# ls /etc/init.d    所有预设服务的目录

functions  netconsole  network  README

# 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:关

centos6及以前是sysv服务管理,centos7是systemd服务管理。

级别:系统启动级别(7之前的用法,7不严格区分级别)

0:关机状态。

1:单用户模式

2:比3少一个NFS服务

3:多用户模式,不带图形

4:保留状态,给用户自定义

5:多用户模式,带图形

6:重启

# chkconfig --level 3 network off

--level指定级别,再服务名,再off或者on。

还可以指定多个级别,--level 345。

也可省略级别,默认对2、3、4、5操作。

服务必须在/etc/init.d/目录内才能增加服务。

增加服务,chkconfig --add network(服务名)

删除服务,chkconfig --del network

可以在chkconfig --list查到。

文件内格式这两点必须有,否则无法识别

# chkconfig: 2345 10 90

# description: Activates/Deactivates all network interfaces configured to \

#              start at boot time.

三、systemd管理服务

# systemctl list-units --all --type=service   列出系统所有服务

常用的命令:

# systemctl enable crond            让某个服务开机启动(.service可省略)

# systemctl disable crond            不让服务开机启动

# systemctl status crond              查看服务状态

# systemctl start crond                 启动某服务

# systemctl stop crond                  停止某服务

# systemctl restart crond               重启某服务

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

四、unit介绍

# ls /usr/lib/systemd/system  下面有很多文件,分类为以下类别:

service:系统服务

target:多个unit组成的组

device:硬件设备

mount:文件系统挂载点

automount:自动挂载点

path:文件或路径

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

slice:进程组

snapshot:systemd快照

socket:进程间通信用的套接字

swap:swap文件

timer:定时器

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

unit相关命令:

# systemctl list-units   //列出正在运行(active)的unit

# systemctl list-units --all     //列出所有的unit(包括失败的,inactive的)

# systemctl list-units --all --state=inactive  //列出所有inactive的unit

# systemctl list-units --all --type=service   //列出所有状态的service

# systemctl list-units --type=service          //列出状态为active的service

# systemctl is-active crond.service            //查看某个unit是否active

五、target介绍

类似于centos6里的启动级别,target支持多个同时启动。它是多个unit的组合,系统启动就是启动了多个unit,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

service、unit和target之间的联系:

1)一个service属于一种unit

2)多个unit一起组成一个target

3)一个target里包含多个service,可以查看文件/usr/lib/systemd/system/sshd.service里面的[install]部分的内容,它就定义了service属于哪个target。