美文网首页
cron/chkconfig/systemd/unit/targ

cron/chkconfig/systemd/unit/targ

作者: hch004 | 来源:发表于2018-01-30 08:19 被阅读0次

linux任务计划cron

cron工具用来制定任务计划,大部分系统管理工作都是通过定期自动执行某个脚本来完成,如何制定就靠cron工具

-u:表示指定某个用户,不加-u选项则默认当前用户。

-e:表示制定计划任务。

-l:表示列出计划任务。

-r:表示删除计划任务。

通过以下命令查看计划任务的配置文件,了解计划文件的内容编写格式

[root@hch ~]# cat /etc/crontab

模拟编写一个任务计划使用crontab -e命令来操作

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

编写好后保存并退出,执行以下命令开启该任务计划

[root@hch ~]# systemctl start crond

使用以下命令查看查看执行状态

[root@hch ~]# systemctl status crond

使用以下命令停止任务计划

[root@hch ~]# systemctl stop crond

chkconfig工具

在Centos 6上的服务管理工具为chkconfig工具,Linux系统所有预设的服务都已可以通过/etc/init.d/目录得到

[root@hch ~]# ls /etc/init.d/

functions  netconsole  network  README

只有屈指可数的几个文件是因为Centos 7已经不再延续Centos 6版本的服务管理方案,但是我们依然可以继续使用chkconfig这个命令。

使用命令chkconfig --list 列出所有的服务及其每个级别的开启状态

[root@hch ~]# chkconfig --list

图中我们可以看到执行命令后系统所提示的内容,说明内容并没有包含Centos 7原生的systemd服务,而仅仅列出了SysV服务,也就是说Centos 7之前的版本采用的服务都是SysV

这里的级别(数字0-6)为系统启动级别(现在的Centos 7已经不再严格区分级别的概念),运行级别016倍系统保留。其中0作为shutdown动作,1作为重启至单用户模式,6为重启。一般都是用2345级别,2表示无NFS支持的多用户模式,3表示完全多用户模式(也是最常用的级别),4保留给用户自定义,5表示图形登录方式

更改某级别下的开启状态[root@hch ~]# chkconfig --level 3 network off

[root@hch ~]# chkconfig --list |grep network

network 0:关 1:关 2:开 3:关 4:开 5:开 6:关

这里--level指定级别,后面跟服务名,然后是设定开关(offon开),还可以指定多个级别

[root@hch ~]# chkconfig --level 245 network off

[root@hch ~]# chkconfig --list |grep network

network 0:关 1:关 2:关 3:关 4:关 5:关 6:关

还可以省略掉级别,但默认是针对2345级别来操作的

[root@hch ~]# chkconfig network on

[root@hch ~]# chkconfig --list |grep network

network 0:关 1:关 2:开 3:开 4:开 5:开 6:关

chkconfig还有以下功能

[root@hch ~]#  chkconfig --del network //移除指定的服务

[root@hch ~]#  chkconfig --add network //添加指定的服务

systemd管理服务

使用systemd以下命令列出系统所有的服务

[root@hch ~]# systemctl list-units --all --type=service

关于systemd常用的相关命令

[root@hch ~]# systemctl enable crond.service //让某个服务开启(.service可以省略)

[root@hch ~]# systemctl disable crond //不让开机启动

[root@hch ~]# systemctl status crond //查看服务状态

[root@hch ~]# systemctl start crond //启动某个服务

[root@hch ~]# systemctl stop crond //停止某个服务

[root@hch ~]# systemctl restart crond //重启某个服务

[root@hch ~]# systemctl is-enable crond //查看某个服务是否开机启动

unit介绍

使用以下命令查看启动脚本文件

[root@hch ~]# ls /usr/lib/systemd/system/

这个路径下有很多文件,这里简单列出一小部分,这些文件有多种类型,不同类型的文件都为一个unit,正式这些unit组成了系统的各个资源。unit可以归类为以下几种

sercive:系统服务

target:多个unit组成的组

device:硬件设备

mount:文件系统挂载点

automount:自动挂载点

path:文件或路径

unit相关的命令

[root@hch ~]# systemctl list-units //列出正在运行(active)的unit

[root@hch ~]# systemctl list-units --all //列出所有的unit(包括失败的、inactive的)

[root@hch ~]# systemctl list-units --all --state=inactive //列出所有inactive的unit

[root@hch ~]# systemctl list-units --all --type=service //列出所有状态的service

[root@hch ~]# systemctl list-units --type=service //列出状态的active的service

[root@hch ~]# systemctl is-active crond.service //查看某个unit是否active

target介绍

target其实是多个unit的组合,为了管理方便,就是用target来管理这些unit

查看当前系统所有的target

[root@hch ~]# systemctl list-unit-files --type=target

查看一个target包含的所有unit

[root@hch ~]# systemctl list-dependencies multi-user.target

关于target相关的命令

[root@hch ~]# systemctl get-default //查看系统默认的target

multi-user.target

[root@hch ~]# systemctl set-default multi-user.target //设置默认的target

相关文章

网友评论

      本文标题:cron/chkconfig/systemd/unit/targ

      本文链接:https://www.haomeiwen.com/subject/emjqzxtx.html