第8周第1课:chkconfig、systemd、unit、target

10.23 linux任务计划cron

Linux任务计划功能的操作都是通过 crontab 命令来完成的,其常用选项有:

  • [ ] -u:指定某个用户,不加此选项则为当前用户
  • [ ] -e:制定计划任务
  • [ ] -l:列出计划任务
  • [ ] -r:删除计划任务

配置计划任务

crontab的配置文件:/etc/crontab

[root@greenfinch ~]# 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 0 3 * * * /bin/bash /user/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log

每天凌晨3点(*位置不指定数字就代表每天、月、周),当前用户(未指定用户,默认为当前用户)执行该命令(123.sh提前写好的命令脚本),并将正确日志和错误日志记录到/tmp/123.log文件中。

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

每个偶数月(*/2:表示能被2整除)1号到10号的周二和周五的凌晨3点,当前用户(未指定用户,默认为当前用户)执行该命令(123.sh提前写好的命令脚本),并将正确日志和错误日志记录到/tmp/123.log文件中。

启动crond服务/查看服务状态

配置完成后需要启动crond服务:systemctl start crond

查看crond服务状态:

  • ps aux |grep cron
  • systemctl status crond

计划任务存放位置:/var/spool/cron/,所有的计划任务存放在该目录下以用户名命名的文件中,备份时可以使用该文件。

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

10.24 chkconfig工具

chkconfig --list 查看当前系统服务状态

[root@greenfinch ~]# 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@greenfinch ~]# ls /etc/init.d/
functions  netconsole  network  README
  • 更改服务状态
  1. 更改服务所有状态:chkconfig network off(或on)
  2. 在某一运行级别的状态:chkconfig --level 345 network off(或on)

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

  • 添加/删除服务

首先,在添加服务之前,必须把该服务的脚本放到 /etc/init.d/ 目录下

[root@greenfinch ~]# cd /etc/init.d/
[root@greenfinch init.d]# ls
functions  netconsole  network  README
[root@greenfinch init.d]# cp network 123
[root@greenfinch init.d]# ls -l
总用量 40
-rwxr-xr-x. 1 root root  6643 9月  19 06:52 123
-rw-r--r--. 1 root root 15131 9月  12 2016 functions
-rwxr-xr-x. 1 root root  2989 9月  12 2016 netconsole
-rwxr-xr-x. 1 root root  6643 9月  12 2016 network
-rw-r--r--. 1 root root  1160 11月  7 2016 README
  1. 添加执行权限:
[root@greenfinch init.d]# chkconfig --add 123
[root@greenfinch init.d]# chkconfig --list

注意:该输出结果只显示 SysV 服务,并不包含原生 systemd 服务。SysV 配置数据可能被原生 systemd 配置覆盖。 
      如果您想列出 systemd 服务,请执行 'systemctl list-unit-files'。
      欲查看对特定 target 启用的服务请执行
      'systemctl list-dependencies [target]'。

123            	0:关	1:关	2:开	3:开	4:开	5:开	6:关
netconsole     	0:关	1:关	2:关	3:关	4:关	5:关	6:关
network        	0:关	1:关	2:开	3:开	4:开	5:开	6:关
  1. 删除执行权限:
[root@greenfinch init.d]# chkconfig --del 123
[root@greenfinch init.d]# 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:关
  • 关于该服务脚本

Markdown

10.25 systemd管理服务

列出系统所有的服务: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
[root@greenfinch init.d]# systemctl disable crond
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.
[root@greenfinch init.d]# 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

10.26 unit介绍

unit所在目录: /usr/lib/systemd/system/

unit文件类型

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

unit相关命令:

  • [ ] 列出正在运行的unit:systemctl list-units
  • [ ] 列出所有的,包括失败的或者inactive的:systemctl list-units --all
  • [ ] 列出inactive的unit:systemctl list-units --all --state=inactive
  • [ ] 列出状态为active的service:systemctl list-units --type=service
  • [ ] 查看某个服务是否为active:systemct is-active crond.service

10.27 target介绍

  • [ ] 查看所有target文件:systemctl list-unit-files -type=target

  • [ ] 查看指定target下面有哪些unit:systemctl list-dependencies multi-user.target

  • [ ] 查看系统默认的target:systemctl get-default

  • [ ] 设置系统默认的target:systemctl set-default multi-user.target

  • target、service、unit关系

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

查看一个service属于哪个target:

cat /usr/lib/systemd/system/sshd.service ,看[install]部分

转载于:https://my.oschina.net/greenfinch/blog/1615218

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值