八周一次课(5月11日)linux任务计划cron、chkconfig工具、systemd管理服务、unit介绍、target介绍

10.23 linux任务计划cronlinux

配置文件, cat  /etc/crontabshell

重点是上图第二个红色框框的内容,前面的五个“ ”,分别表示分钟、小时、日期、月份、星期。user-name 表示用户名,没有的话默认是 root ,command to be executed 表示要执行的命令。vim

输入命令 crontab -e,回车,就会进入到配置文件里bash

用法和 vim 是同样的,按字母“i”进入编辑模式,“Esc”退出编辑,“:wq”保存并退出, “:q”退出,“:q!”强制退出。
如今来说解一下任务计划:ssh


“0 3 ”:表示天天的凌晨3点。表示全部的范围。分钟位就是0-59,小时位表示0-23,日期表示1-31,月份表示1-12,星期表示1-7。
“0 3 1-10 /2 2,5 ”:表示双月1-10号的周二和周五凌晨3点。其中,月份这边“/2”表示能够被2除,就是双月的意思,小时这边/2就表示每隔两小时。
这边没有年份,是由于星期能够肯定惟一性,每年的月份和星期都不同。
0 3 1-10 /2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log 这个任务计划里,/bin/bash 表示用户,/usr/local/sbin/123.sh 表示文件里面有个shell脚本,执行里面的命令。>>/tmp/123.log 2>>/tmp/123.log 表示任务计划的结果所有追加到文件 /tmp/123.log 里面。
任务计划写入完毕,保存并退出,还须要再启用服务,输入命令 systemctl start crond,回车便可。而后再检查一下服务有没有启动,socket

输入命令 ps aux |grep cron 查看进程工具

 systemctl status crond   查看状态命令行

若是是绿色的就表示已启动,停用的话没有颜色显示。rest

注意事项:
shell脚本里面的命令要使用绝对路径,不然会没有执行。
不在配置文件里面的PTAH中的命令就须要使用绝对路径,还有一种方法就是将命令加入到配置文件的PTAH中。
最稳妥的办法仍是直接写绝对路径比较好,PTAH里面的内容不要去动。
写任务计划的时候,最好所有追加到一个日志文件里,正确和错误的输出都要写上,这样才有据可查。
怎么备份?使用命令 crontab -l,能够看到任务计划。crontab 的文件在 /var/spool/cron/ 目录里,按用户名分红多个文件,例如 root 用户在 /var/spool/cron/root 里。这个文件里面的内容就是命令 crontab -l 执行结果列出的内容。因此备份的话,直接复制这个文件就能够了。选项 -r 是删除的意思。
总结:
crontab -e 编辑
crontab -l 查看
crontab -r 删除日志

10.24 chkconfig工具

chkconfig 这个工具的应用是在 CentOS6 及之前的版本,在CentOS7版本里面没有使用了,可是为了跟以前的版本兼容,依然可使用 chkconfig。

命令 chkconfig --list  查看当前系统使用的chkconfig服务

能够看到只有有两个服务netconsole和network,那么其余的服务在哪里呢?注意阅读上图的注意内容,大概意思就是CentOS6 及之前的版本使用的服务是基于SysV服务,而在CentOS7版本里面使用的服务是基于systemd服务。
chkconfig 这个工具中仅剩的4个服务,存在于文件 /etc/init.d/ 中

netconsole和network都有7个级别,其中networkd的二、三、四、5级别都是开启的状态,使用命令 chkconfig network off 以后,就所有关闭了,使用命令 chkconfig network on 以后,又能够开启。这7个级别分别表示:0 表示关机,1表示单用户模式,2表示多用户模式(不带 图形,少nfs服务),3表示多用户模式(不带图形),4表示保留级别(暂时没用),5表示多用户模式(带图形),6表示重启。在CentOS6 及之前的版本中才有这些级别之分,开机时定位在哪一个级别,就会启动哪一个级别的模式。如今的CentOS7版本就没有这些级别之分了。在CentOS6 及之前的版本中,更改文件 /etc/inittab 能够去定义开机的运行级别

chkconfig --level3 network off   3级别关闭

chkconfig --level45 network off  45级别关闭

chkconfig --level345 etwork on   345级别开启

能够把脚本加入到服务列表里面,自定义一个服务,添加的服务要在 /etc/init.d/ 目录下,才能够添加成功

输入命令 chkconfig --del 123  删除自定义服务

总结:
chkconfig --list 列出全部的服务
chkconfig network off 关闭服务
chkconfig network on 开启服务
chkconfig --level 3 network off 关闭3级别服务
chkconfig --level 45 network off 同时关闭4和5级别服务
chkconfig --add 123 将123服务添加到chkconfig工具里

 10.25 systemd管理服务

systemctl list-unit-files     //列出全部启动文件

systemctl list-units --all --type=service  //仅查看service文件

 systemctl list-units --type=service     // 列出启动的service 文件

systemctl enable crond.service   //让服务开机启动,这边的 .service 是能够省略的。
systemctl disable crond  //禁止服务开机启动
systemctl status crond   //查看服务状态

systemctl stop crond //中止服务
systemctl start crond  //启动服务
systemctl restart crond  //重启服务
systemctl is-enabled crond   //检查服务是否开机启动

10.26 unit介绍

软连接文件指向的原文件是 /usr/lib/systemd/system/crond.service ,

使用命令ls  /usr/lib/systemd/system/ 

这个路径下有不少类型的文件和目录,这些文件都叫 unit 。
主要有如下几种类型:
service 系统服务
target 多个unit组成的组
device 硬件设备
mount 文件系统挂载点
automount 自动挂载点
path 文件或路径
scope 不是由systemd启动的外部进程
slice 进程组
snapshot systemd 快照
socket 进程间通讯套接字
swap swap文件
timer 定时器
接着进入 /usr/lib/systemd/system/ 目录下,查看 runlevel,见下图,

entOS7这边也有7个运行级别,分别是0-6的target软连接文件,指向的是后面的路径。这7个运行级别,与CenOS6及以前的版本,基本上均可以对应起来。
接着来看一下,unit的几个相关命令,

systemctl list-units   //查看运行的unit

systemctl list-unit --all  //查看全部的unit

systemctl list-units --all --state=inactive   //指定状态查看unit

systemctl list-units --type=service   //列出全部正在运行的、类型为 service 的 Unit 

 systemctl list-units --all --type=service   //列出全部类型为service的unit

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

10.27 target介绍

 systemctl list-unit-files --type=target    //列出系统里的全部target

.

systemctl list-dependencies multi-user.target   //查看指定target有哪些unit

systemctl get-default  // 查看默认的target

更改 target 类型,能够更改开机的启动级别,相似CentOS6及之前版本的更改配置文件来实现更改运行级别。 

systemctl set-default multi-user.target    //设置默认的 target

上图中,文件 /etc/systemd/system/default.target 就是一个软连接文件
unit 和 service 的关系以下:
一个 service 属于一种类型的 unit
多个 unit 组成了一个 target
一个 target 里面包含了多个 service

总结:

cat /etc/crontab 查看任务计划的配置文件
“ user-name command to be executed ” 五个“ ”分别表示分钟、小时、日期、月份、星期,user-name 表示用户名,没有的话默认是 root ,command to be executed 表示要执行的命令。
crontab -e 进入配置文件,输入命令行保存退出
0 3 1-10 星/2 2,5 /bin/bash /usr/local/sbin/123.sh >>/tmp/123.log 2>>/tmp/123.log 这个任务计划里,/bin/bash 表示用户,/usr/local/sbin/123.sh 表示文件里面有个shell脚本,执行里面的命令。>>/tmp/123.log 2>>/tmp/123.log 表示任务计划的结果所有追加到文件 /tmp/123.log 里面。
systemctl start crond 重启服务
ps aux |grep cron 检查进程
systemctl status crond 查看状态
crontab -e 编辑
crontab -l 查看
crontab -r 删除

chkconfig --list 查看当前系统使用 chkconfig 工具的服务
chkconfig network off 关闭服务
chkconfig network on 开启服务
chkconfig --level 3(或345) network off 关闭指定的一个或多个级别服务
chkconfig --level 3(或345) network on 开启指定的一个或多个级别服务
cd /etc/init.d/
cp network 123 这两步操做为下面的增长或删除自定义服务作准备
chkconfig --add 123 增长自定义服务
chkconfig --del 123 删除自定义服务

systemctl list-unit-files 查看全部的unit服务
systemctl list-units --all --type=servic 仅查看 service 类型的服务
systemctl list-units --type=service 只列出 active 状态的文件
systemctl enable crond.service 让服务开机启动,这边的 .service 是能够省略的。
systemctl disable crond 禁止服务开机启动
systemctl status crond 查看服务状态
systemctl stop crond 中止服务
systemctl start crond 启动服务
systemctl restart crond 重启服务
systemctl is-enabled crond 检查服务是否开机启动

systemctl list-units 列出正在运行的unit
systemctl list-units --all 列出全部,包括失败的或者inactive的
systemctl list-units --all --state=inactive 列出指定状态inactive的unit
systemctl list-units --type=service 列出指定状态service的unit,failed 状态也会出现
systemctl list-units --type=service --state=active 仅列出指定状态service的unit
systemctl list-units --all --type=service 列出所有状态的 service
systemctl list-units --type=service 列出状态为active的service
systemctl is-active crond.service 查看某个服务是否为active

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  cat /usr/lib/systemd/system/sshd.service 查看 ssh.service 属于哪个 target unit 和 service 的关系以下: 一个 service 属于一种类型的 unit 多个 unit 组成了一个 target 一个 target 里面包含了多个 service

相关文章
相关标签/搜索