linux7的service,centos7的服务治理-systemd

文章目录

[隐藏]

经常用到的高频命令小结

unit的类型

centos6 chkconfig VS 使用systemd的service unit治理centos7服务

serivice unit配置

target unit: 的含义是服务组,表示一组服务

查看在运行的服务

查看开机是否会启动

设置开机自动启动细节

设置启动级别的命令

经常用到的高频命令小结

- 所有服务unit放在这里 ll /usr/lib/systemd/system - 默认启动级别 [root@n1 ~]# ll /etc/systemd/system/default.target lrwxrwxrwx 1 root root 41 Mar 4 09:02 /etc/systemd/system/default.target -> /usr/lib/systemd/system/multi-user.target - 开机启动的服务 ll /etc/systemd/system/multi-user.target.wants/ - 哪些服务开机会启动 [root@n1 ~]# systemctl list-unit-files --type service |grep enable autovt@.service enabled crond.service enabled docker.service enabled getty@.service enabled ntpdate.service enabled rsyslog.service enabled sshd.service enabled sysstat.service enabled - 目前哪些服务处于active状态 [root@n1 ~]# systemctl list-units --type service |grep active crond.service loaded active running Command Scheduler dbus.service loaded active running D-Bus System Message Bus docker.service loaded active running Docker Application Container Engine getty@tty1.service loaded active running Getty on tty1 - 查看某个服务是否开机自启 systemctl is-enabled httpd.service 或 systemctl status httpd.service

systemd 有很多unit,其中service.unit是管理系统服务的.

fc45039a97a59021d34bd752b4eb8805.png

unit的类型

/usr/lib/systemd/system

5bf9febded5a3ab9002dcc2999bf51c4.png

查看管理系统服务的相关的, 即 .service后缀的

[root@n1 system]# ls /usr/lib/systemd/system/*.service|more /usr/lib/systemd/system/arp-ethers.service /usr/lib/systemd/system/auditd.service /usr/lib/systemd/system/autovt@.service /usr/lib/systemd/system/blk-availability.service /usr/lib/systemd/system/brandbot.service ... 特点: 不需要可执行权限,其内容也不能执行 仅是systemd的调用的配置文件

src=”https://www.centos.bz/wp-content/uploads/2018/03/3-20.png” alt=”” >centos6 chkconfig VS 使用systemd的service unit治理centos7服务

e9da6af78708f80996732e3c3ccd3bc8.png

ls /usr/lib/systemd/system/*.service|more 下以service开头的文件,都是和系统服务有关的, 其下还有别的后缀的服务.是别的unit的配置文件.

4df252e3a35a513608b1c74d7aa75284.png

serivice unit配置

821c9979c8d64d29ec29a6732f7906e2.png

target unit: 的含义是服务组,表示一组服务

将很多服务放在一坨, 开启启动时候加载这个文件.

启动级别

/usr/lib/systemd/system /etc/systemd/system runlevel0.target -> poweroff.target runlevel2.target -> multi-user.target runlevel3.target -> multi-user.target runlevel4.target -> multi-user.target runlevel5.target -> graphical.target runlevel6.target -> reboot.target

注意: centos7里,运行级别234没区别.

查看在运行的服务

我们看ACTIVE就ok了,SUB这一列可以不用理会

[root@n1 system]# systemctl list-units --type=service UNIT LOAD ACTIVE SUB DESCRIPTION crond.service loaded active running Command Scheduler dbus.service loaded active running D-Bus System Message Bus docker.service loaded active running Docker Application Container Engine getty@tty1.service loaded active running Getty on tty1 httpd.service loaded active running The Apache HTTP Server

查看开机是否会启动

[root@n1 system]# systemctl list-unit-files --type=service UNIT FILE STATE arp-ethers.service disabled auditd.service disabled autovt@.service enabled #开机会启动 blk-availability.service disabled#开机不会启动 brandbot.service static #这类不用管 conntrackd.service disabled

systemd管理服务入门http://www.ruanyifeng.com/blog/2016/03/systemd-tutorial-part-two.html

设置开机自动启动细节

[root@n1 ~]# yum install psmisc -y [root@n1 ~]# pstree systemd─┬─agetty ├─crond ├─dbus-daemon ├─dockerd─┬─docker-containe─┬─docker-containe─┬─mysqld───20*[{mysqld}] │ │ │ └─8*[{docker-containe}] │ │ └─9*[{docker-containe}] │ ├─docker-proxy───3*[{docker-proxy}] │ └─11*[{dockerd}] ├─httpd───8*[httpd] ├─lvmetad ├─polkitd───5*[{polkitd}] ├─rsyslogd───2*[{rsyslogd}] ├─sshd───sshd───bash───pstree ├─systemd-journal ├─systemd-logind └─systemd-udevd

[root@n1 system]# systemctl enable httpd Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.

相当于执行

ln -s /usr/lib/systemd/system/httpd.service /etc/systemd/system/multi-user.target.wants/httpd.service

disable相当于删除

[root@n1 system]# systemctl disable httpd Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.

[root@n1 system]# ll /etc/systemd/system/multi-user.target.wants total 0 lrwxrwxrwx. 1 root root 37 Dec 27 04:44 crond.service -> /usr/lib/systemd/system/crond.service lrwxrwxrwx 1 root root 38 Dec 26 21:05 docker.service -> /usr/lib/systemd/system/docker.service lrwxrwxrwx 1 root root 37 Mar 4 03:39 httpd.service -> /usr/lib/systemd/system/httpd.service lrwxrwxrwx. 1 root root 39 Dec 26 21:00 ntpdate.service -> /usr/lib/systemd/system/ntpdate.service lrwxrwxrwx. 1 root root 40 Dec 27 04:44 remote-fs.target -> /usr/lib/systemd/system/remote-fs.target lrwxrwxrwx. 1 root root 39 Dec 27 04:44 rsyslog.service -> /usr/lib/systemd/system/rsyslog.service lrwxrwxrwx. 1 root root 36 Dec 27 04:44 sshd.service -> /usr/lib/systemd/system/sshd.service lrwxrwxrwx. 1 root root 39 Dec 27 05:00 sysstat.service -> /usr/lib/systemd/system/sysstat.service

设置启动级别的命令

- 设置默认启动级别 [root@n1 ~]# systemctl set-default graphical.target Removed symlink /etc/systemd/system/default.target. Created symlink from /etc/systemd/system/default.target to /usr/lib/systemd/system/graphical.target. - 查看默认的启动级别 [root@n1 ~]# systemctl get-default graphical.target - 手动修改启动级别(先将原来的rm了) [root@n1 ~]# ln -sv /usr/lib/systemd/system/multi-user.target /etc/systemd/system/default.target ln: failed to create symbolic link ‘/etc/systemd/system/default.target’: File exists [root@n1 ~]# rm -rf /etc/systemd/system/default.target [root@n1 ~]# ln -sv /usr/lib/systemd/system/multi-user.target /etc/systemd/system/default.target ‘/etc/systemd/system/default.target’ -> ‘/usr/lib/systemd/system/multi-user.target’ [root@n1 ~]# systemctl get-default multi-user.target - 查看服务状态 [root@n1 ~]# systemctl is-enabled httpd.service disabled

原文出处:cnblogs -> https://www.cnblogs.com/iiiiher/p/8503665.html

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值