2018-1-29 8周1次课 cron,chkconfig,systemd,unit,target

10.23 linux任务计划cron

(任务计划配置文件,定义几个环境变量)

格式:分钟 小时 日期 月份 周 用户 命令

进入crontab配置文件:crontab -e

范围:1-10                   比如:1月到10月

双数时间:*/2              比如:每2天

独立时间:1,2,3            比如:每周的周1,周2,周3

启动cron服务:systemctl start crond

检查是否启动:ps aux | grep crond 查看crond是否启动或者systemctl status crond是否是绿色运行

如果任务计划放到配置文件中,但是没有执行,很有可能是脚本用的是一个命令,而不是绝对路径

有两种解决方法:1,命令写绝对路径;2,把命令的路径加入到配置文件中的PATH变量中

建议:每写一个任务计划都要写上追加日志,正确和错误输出

·列出任务计划:crontab -l

例子:新建任务计划

[[email protected] ~]# crontab -e

[[email protected] ~]# crontab -l

1 10 * 2 * /usr/bin/find /tmp/ -type f -mtime +100 |xargs rm -f

任务计划会保存在/var/spool/cron/下对应用户名的文件中,备份改文件就可以备份任务计划

·删除任务计划:crontab -r

[[email protected] ~]# crontab -r

[[email protected] ~]# crontab -l

no crontab for root

·指定用户:crontab -u

[[email protected] ~]# crontab -u root -l

no crontab for root

参考:http://man.linuxde.net/




10.24 chkconfig工具

★CentOS7系统中已经不再使用chkconfig工具,因此学习重心放在systemd上

·使用chkconfig工具的服务有哪些:chkconfig --list

服务脚本所在位置:/etc/init.d/

[[email protected] ~]# ls /etc/init.d/

functions  netconsole  network  README

·关闭服务:chkconfig 服务名 off

·打开服务:chkconfig 服务名 on

·更改运行级别:更改/etc/inittab,不过CentOS7开始已经不使用了

·指定某个级别的开启关闭:

(--level在前后都可以,没有区别)

·指定多个级别的开启关闭:

(345中间不加逗号)

0和6是不可以开启的

·自定义服务并加入服务列表:chkconfig --add 服务名

[[email protected] ~]# 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:关
[[email protected] ~]# ls /etc/init.d
functions  netconsole  network  README
[[email protected] ~]# cp /etc/init.d/network /etc/init.d/123
[[email protected] ~]# ls /etc/init.d/
123  functions  netconsole  network  README
[[email protected] ~]# chkconfig --add 123
[[email protected] ~]# 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:关

(首先,自定义的服务要在/etc/init.d目录下,其次,文件的内容有格式)

服务的格式中,红色箭头所指的两行必须要有,才能被识别

·删除自定义服务:chkconfig --del 服务名




10.25 systemd管理服务

·列出所有服务:systemctl list-unit-files

[[email protected] ~]# systemctl list-unit-files

(内容太多不详细展示)

·列出service服务:systemctl list-units --all --type=service

[[email protected] ~]# systemctl list-units --all --type=service


·列出激活状态的service服务:systemctl list-units --type=service

(内容太多不详细展示)

·让服务开机启动:systemctl enable 服务名

·不让服务开机启动:systemctl disable 服务名

[[email protected] ~]# systemctl enable crond.service## 可以不加.service
[[email protected] ~]# systemctl disable crond
Removed symlink /etc/systemd/system/multi-user.target.wants/crond.service.

·查看服务状态:systemctl status 服务名

·停止服务:systemctl stop 服务名

·启动服务:systemctl start 服务名

·重启服务:systemctl restart 服务名

[[email protected] ~]# systemctl stop crond
[[email protected] ~]# systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; disabled; vendor preset: enabled)
Active: inactive (dead)
1月 23 20:23:29 localhost.localdomain systemd[1]: Started Command Scheduler.
1月 23 20:23:29 localhost.localdomain systemd[1]: Starting Command Scheduler...
1月 23 20:23:29 localhost.localdomain crond[567]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 35% if used.)
1月 23 20:23:30 localhost.localdomain crond[567]: (CRON) INFO (running with inotify support)
1月 23 22:26:56 localhost.localdomain systemd[1]: Stopping Command Scheduler...
1月 23 22:26:56 localhost.localdomain systemd[1]: Stopped Command Scheduler.
[[email protected] ~]# systemctl start crond
[[email protected] ~]# systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; disabled; vendor preset: enabled)
Active: active (running) since 二 2018-01-23 22:27:18 CST; 1s ago
Main PID: 1551 (crond)
CGroup: /system.slice/crond.service
└─1551 /usr/sbin/crond -n

1月 23 22:27:18 localhost.localdomain systemd[1]: Started Command Scheduler.
1月 23 22:27:18 localhost.localdomain systemd[1]: Starting Command Scheduler...
1月 23 22:27:18 localhost.localdomain crond[1551]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 9% if used.)
1月 23 22:27:19 localhost.localdomain crond[1551]: (CRON) INFO (running with inotify support)
1月 23 22:27:19 localhost.localdomain crond[1551]: (CRON) INFO (@reboot jobs will be run at computer's startup.)
[[email protected] ~]# systemctl restart crond
[[email protected] ~]# systemctl status crond
● crond.service - Command Scheduler
Loaded: loaded (/usr/lib/systemd/system/crond.service; disabled; vendor preset: enabled)
Active: active (running) since 二 2018-01-23 22:27:31 CST; 2s ago
Main PID: 1560 (crond)
CGroup: /system.slice/crond.service
└─1560 /usr/sbin/crond -n

1月 23 22:27:31 localhost.localdomain systemd[1]: Started Command Scheduler.
1月 23 22:27:31 localhost.localdomain systemd[1]: Starting Command Scheduler...
1月 23 22:27:31 localhost.localdomain crond[1560]: (CRON) INFO (RANDOM_DELAY will be scaled with factor 77% if used.)
1月 23 22:27:32 localhost.localdomain crond[1560]: (CRON) INFO (running with inotify support)
1月 23 22:27:32 localhost.localdomain crond[1560]: (CRON) INFO (@reboot jobs will be run at computer's startup.)

·检查服务是否开机启动:systemctl is-enabled 服务名

[[email protected] ~]# systemctl is-enabled crond
disabled
[[email protected] ~]# systemctl enable crond
Created symlink from /etc/systemd/system/multi-user.target.wants/crond.service to /usr/lib/systemd/system/crond.service.
[[email protected] ~]# systemctl is-enabled crond
enabled

服务的配置文件内容:/etc/systemd/system/multi-user.target.wants/crond.service(软连接)

实际文件在:/usr/lib/systemd/system/crond.service

如果disable服务,那么会挪走软连接




10.26 unit介绍

系统所有unit,在 /usr/lib/systemd/system 中

·分为以下类型:

·service                              系统服务

·target                               多个unit组成的组

·device                               硬件设备

·mount                              文件系统挂载点

·automount                       自动挂载点

·path                                  文件或路径

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

·slice                                  进程组

·snapshot systemd            快照

·socket                               进程间通信套接字

·swap                                 swap文件

·timer                                定时器

unit相关命令:

·列出正在运行的unit:systemctl list-units

[[email protected] ~]# systemctl list-units

·列出所有unit(包括失败的或者inactive的):systemctl list-units --all

[[email protected] ~]# systemctl list-units --all[object Object]

·列出inactive的unit:systemctl list-units --all --state=inactive

[[email protected] ~]# systemctl list-units --all --state=inactive

·列出状态为active的service:systemctl list-units --type=service

[[email protected] ~]# systemctl list-units --type=service

·查看某个服务是否为active:systemctl is-active crond.service

[[email protected] ~]# systemctl is-active crond.service
active

类似于:systemctl is-enabled crond.service




10.27 target介绍

·列出系统中所有的target:systemctl list-unit-files --type=target

[[email protected] ~]# systemctl list-unit-files --type=target

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

[[email protected] ~]# systemctl list-dependencies multi-user.target

(target下面可以继续含有target)

·查看系统默认的target:systemctl get-default

[[email protected] ~]# systemctl get-default
multi-user.target

·更改系统默认target:systemctl set-default multi-user.target

[[email protected] ~]# systemctl set-default multi-user.target
Removed symlink /etc/systemd/system/default.target.
Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/multi-user.target.

(更改系统默认target去更改类似于CentOS6中的运行级别)

·一个service属于一种类型的unit

·多个unit组成了一个target

·一个target里面包含了多个service

·查看某个service属于哪个target:看[install]部分

[[email protected] ~]# 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



2018-1-29 8周1次课 cron,chkconfig,systemd,unit,target

原文地址:http://blog.51cto.com/11530642/2066595

时间: 2024-08-29 08:41:09

2018-1-29 8周1次课 cron,chkconfig,systemd,unit,target的相关文章

Linux20180511八周一次课cron chkconf systemd unit target

八周一次课(5月11日) 10.23 linux任务计划cron10.24 chkconfig工具10.25 systemd管理服务10.26 unit介绍10.27 target介绍扩展1. anacron http://blog.csdn.net/strikers1982/article/details/47872262. xinetd服(默认机器没有安装这个服务,需要yum install xinetd安装)http://blog.sina.com.cn/s/blog_465bbe6b01

2018.1.29 8周1次课

八周一次课(1月29日) 10.23 linux任务计划cron 10.24 chkconfig工具 10.25 systemd管理服务 10.26 unit介绍 10.27 target介绍 10.23 linux任务计划cron 其实大部分系统管理工作都是通过定期自动执行某个脚本来完成的, 那么如何定期执行某个脚本呢? 这就要借助Linux的cron功能了 Linux任务计划功能的操作都是通过crontab命令来完成的, 其常用的选项有以下几个. -u:表示指定某个用户,不加- u选项则为当

2018.3.29 14周4次课

十四周四次课(3月29日) 16.1 Tomcat介绍 16.2 安装jdk 16.3 安装Tomcat 16.1 Tomcat介绍 Tomcat是Apache软件基金会(Apache Software Foundation)的Jakarta项目中的一个核心项目,由Apache.Sun和其他一些公司及个人共同开发而成. java程序写的网站用tomcat+jdk来运行 tomcat是一个中间件,真正起作用的,解析java脚本的是jdk jdk(java development kit)是整个ja

2018.1.9 5周2次课

五周第二次课(1月9日) 7.6 yum更换国内源 7.7 yum下载rpm包 7.8/7.9 源码包安装 7.6 yum更换国内源 1.恢复之前备份的文件 2. 进入"/etc/yum.repos.d"目录 3.删除"CentOS-Base.repo"文件 4.下载"163.repo"文件 wget http://mirrors.163.com/.help/CentOS7-Base-163.repo curl -O http://mirrors

2018.1.16 6周2次课

六周第二次课(1月16日) 9.4/9.5 sed 9.4/9.5 sed 其实grep工具的功能还不够强大,它实现的只是查找功能,而不能把查找的内容替换.以前用vim操作文档的时候,可以查找也可以替换, 但只限于在文本内部操作,而不能输出到屏幕上.sed工具以及后面要介绍的awk工具就能把替换的文本输出到屏幕上,而且还有其他更丰富的功能.sed和awk都是流式编辑器,是针对文档的行来操作的. sed  '/x/'p filename:匹配x字符 sed  -n  '/x/'p  filenam

2018.3.1 10周2次课

十周第二次课(3月1日) 11.14/11.15 Apache和PHP结合 11.16/11.17 Apache默认虚拟主机 11.14/11.15 Apache和PHP结合 配置httpd支持php httpd主配置文件/usr/local/apache2.4/conf/httpd.conf vim /usr/local/apache2.4/conf/httpd.conf   //修改以下4个地方 ServerName 搜索ServerName,把#ServerName www.example

2018.4.23 17周4次课

十七周4次课(4月23日) 20.20 告警系统主脚本 20.21 告警系统配置文件 20.22 告警系统监控项目 20.20 告警系统主脚本 创建告警系统的目录: [[email protected] /usr/local/sbin]# mkdir mon [[email protected] /usr/local/sbin]# ls mon  nginx_log_rotate.sh [[email protected] /usr/local/sbin]# cd mon [[email pro

2018.3.29 二周第四次课

特殊权限set_uid 概念:set_uid,当普通用户执行passwd命令时,可以**临时**拥有root权限. set_uid 该权限针对二进制**可执行文件**,如:ls passwd 如下命令,查看 [[email protected] ~]# which passwd #查看这个命令在哪/usr/bin/passwd[[email protected] ~]# ls -l /usr/bin/passwd #查看passwd文件权限-rwsr-xr-x. 1 root root 2783

2017.12.29 3周5次课

三周第五次课(12月29日) 4.5/4.6 磁盘格式化  4.7/4.8 磁盘挂载 4.9 手动增加swap空间 4.5/4.6 磁盘格式化 centos7系统支持的文件格式如下: 在/etc/filesystems里查看 "xfs"是centos7默认的文件系统,之前的版本都是"ext" 命令[mke2fs]:格式化成ext2/ext3/ext4的文件系统 常用的选项有(mkfs.ext2/ext3/ext4也适用): '-b' 分区时设定每个数据区块占用空间大