Linux命令之systemctl命令


aihuafeng66t
克里斯蒂亚诺诺 2024-01-05 20:46:39 50710 赞同 0 反对 0
分类: 资源
Linux命令之systemctl命令

一、systemctl命令简介
  CentOS 5使用SysV init;CentOS 6使用Upstart,CentOS 7使用Systemd管理守护进程。centos7采用 systemd管理,服务独立的运行在内存中,服务响应速度快,但占用更多内存。独立服务的服务启动脚本都在目录 /usr/lib/systemd/system里。Systend的新特性:

系统引导时实现服务的并行启动;
按需激活进程;
系统实现快照;
基于依赖关系定义服务的控制逻辑;
  systemctl可用于内省和控制“systemd”系统和服务管理器的状态。centos7.x系统环境下我们经常使用此命令启停服务,实际上此命令除了其他独立服务还有很多其他用途。

二、systemctl使用示例
1、查看命令帮助
[root@s153 system]# systemctl --help
systemctl [OPTIONS…] {COMMAND} …

2、启动服务
  接下来的操作实例以管理xinetd服务为例。

[root@s153 system]# systemctl start xinetd

3、查看服务状态
[root@s153 system]# systemctl status xinetd
● xinetd.service - Xinetd A Powerful Replacement For Inetd
Loaded: loaded (/usr/lib/systemd/system/xinetd.service; enabled; vendor preset: enabled)
Active: active (running) since 三 2022-07-20 10:29:26 CST; 1min 53s ago
Process: 15831 ExecStart=/usr/sbin/xinetd -stayalive -pidfile /var/run/xinetd.pid $EXTRAOPTIONS (code=exited, status=0/SUCCESS)
Main PID: 15832 (xinetd)
CGroup: /system.slice/xinetd.service
└─15832 /usr/sbin/xinetd -stayalive -pidfile /var/run/xinetd.pid

4、停止服务
[root@s153 system]# systemctl stop xinetd

5、查看服务是否活跃
[root@s153 system]# systemctl is-active xinetd
inactive
[root@s153 system]# systemctl start xinetd
[root@s153 system]# systemctl is-active xinetd
active

6、重新加载服务配置
  reload是在不重启服务的情况下重新加载配置文件。

[root@s153 system]# systemctl reload xinetd

7、重启服务
  restart命令实际上是先stop,然后start。

[root@s153 system]# systemctl restart xinetd
————————————————

13、设置服务开机自启动
[root@s153 system]# systemctl enable xinetd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/xinetd.service to /usr/lib/systemd/system/xinetd.service.

14、取消服务开机自启动
[root@s153 system]# systemctl disable xinetd.service
Removed symlink /etc/systemd/system/multi-user.target.wants/xinetd.service.

15、查看机器信息
[root@s153 system]# systemctl list-machines
NAME STATE FAILED JOBS
s153 (host) running 0 0

1 machines listed.

16、查看系统环境变量
[root@s153 system]# systemctl show-environment
LANG=zh_CN.UTF-8
PATH=/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin

17、重新加载unit文件
  如果手动修改了unit文件,可以使用此命令重新加载。

[root@s153 system]# systemctl daemon-reload

18、创建一个系统快照
[root@s153 system]# systemctl snapshot wuhs
wuhs.snapshot

19、删除指定快照
[root@s153 system]# systemctl delete wuhs

20、查看服务是否开机自启动
[root@s153 system]# systemctl is-enabled xinetd.service
enabled

21、杀死服务
[root@s153 system]# systemctl kill xinetd
[root@s153 system]# systemctl is-failed xinetd
inactive
————————————————

3、unit file结构
文件通常由三部分组成:

[Unit]: 定义与Unit类型无关的通用选项;用于提供unit的描述信息,unit行为及依赖关系等。
[Service]:与特定类型相关的专用选项;此处为Service类型。
[Install]:定义由"systemctl enable"及"systemctl disable"命令在实现服务启用或禁用时用到的一些选项。
4、Unit段的常用选项
Description:描述信息,意义性描述;
After:定义unit的启动次序;表示当前unit应晚于哪些unit启动;其功能与Before相反;
Requies:依赖到其它的units;强依赖,被依赖的units无法激活时,当前的unit即无法激活;
Wants:依赖到其它的units;弱依赖;
Confilcts:定义units 的冲突关系;
5、Service段的常用选项
Type:用于定义影响ExecStart及相关参数的功能的unit进程类型;
类型有:simple、forking、oneshot、dbus、notify、idle。
EnvironmentFile:环境配置文件;
ExecStart:指明启动unit要运行的命令或脚本;ExecStart, ExecStartPost
ExecStop:指明停止unit要运行的命令或脚本;
Restart:
6、Install段的常用配置:
Alias:
RequiredBy:被哪些unit所依赖;
WantBy:被哪些unit所依赖;
7、Unit文件样例
[root@s153 system]# cat chronyd.service
[Unit]
Description=NTP client/server
Documentation=man:chronyd(8) man:chrony.conf(5)
After=ntpdate.service sntp.service ntpd.service
Conflicts=ntpd.service systemd-timesyncd.service
ConditionCapability=CAP_SYS_TIME

[Service]
Type=forking
PIDFile=/var/run/chronyd.pid
EnvironmentFile=-/etc/sysconfig/chronyd
ExecStart=/usr/sbin/chronyd $OPTIONS
ExecStartPost=/usr/libexec/chrony-helper update-daemon
PrivateTmp=yes
ProtectHome=yes
ProtectSystem=full

[Install]
WantedBy=multi-user.target
————————————————

如果您发现该资源为电子书等存在侵权的资源或对该资源描述不正确等,可点击“私信”按钮向作者进行反馈;如作者无回复可进行平台仲裁,我们会在第一时间进行处理!

评价 0 条
克里斯蒂亚诺诺L1
粉丝 0 资源 831 + 关注 私信
最近热门资源
银河麒麟桌面操作系统备份用户数据  126
统信桌面专业版【全盘安装UOS系统】介绍  121
银河麒麟桌面操作系统安装佳能打印机驱动方法  114
银河麒麟桌面操作系统 V10-SP1用户密码修改  105
最近下载排行榜
银河麒麟桌面操作系统备份用户数据 0
统信桌面专业版【全盘安装UOS系统】介绍 0
银河麒麟桌面操作系统安装佳能打印机驱动方法 0
银河麒麟桌面操作系统 V10-SP1用户密码修改 0
作者收入月榜
1

prtyaa 收益393.62元

2

zlj141319 收益218元

3

1843880570 收益214.2元

4

IT-feng 收益209.03元

5

风晓 收益208.24元

6

777 收益172.71元

7

Fhawking 收益106.6元

8

信创来了 收益105.84元

9

克里斯蒂亚诺诺 收益91.08元

10

技术-小陈 收益79.5元

请使用微信扫码

加入交流群

请使用微信扫一扫!