日常运维(6),linux任务计划crontab命令,服务管理chkconfig命令,systemd管理服务,unit介绍, target介绍

linux任务计划

crontab命令
被用来提交和管理用户的需要周期性执行的任务,与windows下的计划任务类似,当安装完成操作系统后,默认会安装此服务工具,并且会自动启动crond进程,crond进程每分钟会定期检查是否有要执行的任务,如果有要执行的任务,则自动执行该任务。

crontab [-uelr]
-u 指定用户,默认是当前用户
-e 编辑计划任务
-l 列出计划任务
-r 删除计划任务
etc/crontab 配置文件

crontab -e 实际上是打开了/var/spool/cron/username文件, 此文件不能直接编辑(会报错),要用crontab -e命令编辑

编辑计划的格式
分 时 日 月 周 user command

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

可用格式1-5,表示范围1到5
可用格式1,2,3 表示1或2或3
可用格式*/2,表示被2整除的数字,如果是小时就是每隔2小时

要保证服务是启动状态,才能执行

启动服务
systemctl start cron.service

检查服务进程
ps aux |grep cron

检查服务状态
systemctl status crond
Active:active (running)表示已经启动
Active:inactive (dead)表示没有启动

[root@localhost ~]# cat /etc/crontab 查看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
[root@localhost ~]#


服务管理
chkconfig命令
检查、设置系统的各种服务。这是Red Hat公司遵循GPL规则所开发的程序,它可查询操作系统在每一个执行等级中会执行哪些系统服务,其中包括各类常驻服务。谨记chkconfig不是立即自动禁止或激活一个服务,它只是简单的改变了符号连接。

chkconfig --list 列出所有服务及其每个级别的开启状态
(只显示属于SysV工具的服务,不包含原生systemd工具的服务,可用systemctl list-unit-files命令查看)
chkconfig --level 3 network off 关闭3级网络服务
chkconfig --level 345 network off 关闭3级,4级,5级网络服务(不能加,号)
chkconfig --del network 删除网络服务
chkconfig --add network 添加网络服务(先将服务脚本加进/etc/init.d/目录下再添加服务)
chkconfig --network off 关闭网络服务
chkconfig --network on 开启网络服务

例:
[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:关
[root@localhost ~]# cat /etc/init.d/network 查看network服务脚本
#! /bin/bash 首先必须是shell脚本
#
# network Bring up/down networking
#
# chkconfig: 2345 10 90 运行级别2345,第10位启动,第90位关闭 这两部分必须要有
# description: Activates/Deactivates all network interfaces configured to \ 这两部分必须要有
# start at boot time.
#
### BEGIN INIT INFO
# Provides: $network
# Should-Start: iptables ip6tables NetworkManager-wait-online NetworkManager $network-pre
# Short-Description: Bring up/down networking
# Description: Bring up/down networking
### END INIT INFO

systemd管理服务

systemctl命令
是系统服务管理器指令,它实际上将 servicechkconfig 这两个命令组合到一起

systemctl list-units --all --type=service 列出所有服务
systemctl list-units --type=service 列出所有激活的服务
systemctl list-units-files 列出所有单元文件

几个常用的服务相关的命令
systemctl enable crond.service 开启服务开机启动
systemctl disable crond.service 禁止服务开机启动
systemctl status crond 查看服务状态
systemctl start crond 启动服务
systemctl stop crond 停止服务
systemctl restart crond 重启服务
systemctl is-enable crond 检查服务是否开机启动

unit类型
ls /usr/lib/systemd/system 系统所有unit,分为以下类型

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

systemctl list-units 列出正在运行的unit
systemctl list-units --all 列出所有unit
systemctl list-units --all --state=inactive 列出不活动的unit
systemctl list-units --type=service 列出所有活动的service
systemctl is-active ***.service 查看某个服务是否在活动

target单元组

systemctl list-unit-files --type=target 列出所有单元组
systemctl list-dependencies multi-user.target 查看指定单元组下的单元
systemctl get-default 查看系统默认的单元组
systemctl set-default multi-user.target 设置默认的单元组

一个service属于一种类型的unit,多个unit组成了一个target
一个target里边包含了多个service或其他类型的unit

cat /usr/lib/systemd/system/sshd.service 看install部分内容
[root@localhost ~]# cat /usr/lib/systemd/system/sshd.service
[Unit]
Description=OpenSSH server daemon
Documentation=man:sshd(8) man:sshd_config(5)
After=network.target sshd-keygen.service
Wants=sshd-keygen.service

[Service]
Type=notify
EnvironmentFile=/etc/sysconfig/sshd
ExecStart=/usr/sbin/sshd -D $OPTIONS
ExecReload=/bin/kill -HUP $MAINPID
KillMode=process
Restart=on-failure
RestartSec=42s

[Install]
WantedBy=multi-user.target
[root@localhost ~]#


猜你喜欢

转载自blog.csdn.net/langyue919/article/details/80288941