Linux - systemctl - 守护进程(服务)、开机启动等工具

Linux Systemctl是一个系统管理守护进程、工具和库的集合,用于取代System V、service和chkconfig命令,初始进程主要负责控制systemd系统和服务管理器。通过Systemctl –help可以看到该命令主要分为:查询或发送控制命令给systemd服务,管理单元服务的命令,服务文件的相关命令,任务、环境、快照相关命令,systemd服务的配置重载,系统开机关机相关的命令。

作为初始化系统,systemd进程作为系统中的1号进程是毋庸置疑的,让我们来看一下:

[root@localhost system]# ps -ef|grep systemd
root         1     0  0 9月17 ?       00:00:03 /usr/lib/systemd/systemd --switched-root --system --deserialize 21
root       564     1  0 9月17 ?       00:00:01 /usr/lib/systemd/systemd-journald
root       602     1  0 9月17 ?       00:00:00 /usr/lib/systemd/systemd-udevd
root       857     1  0 9月17 ?       00:00:00 /usr/lib/systemd/systemd-logind
dbus       862     1  0 9月17 ?       00:00:51 /bin/dbus-daemon --system --address=systemd: --nofork --nopidfile --systemd-activation
root     18492 15440  0 09:37 pts/1    00:00:00 grep --color=auto systemd

第一行就是pid为的进程,/usr/lib/systemd/systemd。也可以直接用进程号来查看:

[root@localhost system]# ps u -p 1
USER       PID %CPU %MEM    VSZ   RSS TTY      STAT START   TIME COMMAND
root         1  0.0  0.1 191320  4352 ?        Ss   9月17   0:03 /usr/lib/systemd/systemd --switched-root --system --deserialize 21

但在ubuntu系统中,1 号进程为/sbin/init ,其实它是到 /lib/systemd/systemd 的链接文件,只不过起了一个别名。

 

1. 列出所有可用单元

# systemctl list-unit-files

2. 列出所有运行中单元

# systemctl list-units

3. 列出所有失败单元

1

# systemctl –failed

4. 检查某个单元(如 crond.service)是否启用

1

# systemctl is-enabled crond.service

5. 列出所有服务

1

# systemctl list-unit-files –type=service

6. Linux中如何启动、重启、停止、重载服务以及检查服务(如 httpd.service)状态

1

2

3

4

5

# systemctl start httpd.service

# systemctl restart httpd.service

# systemctl stop httpd.service

# systemctl reload httpd.service

# systemctl status httpd.service

注意:当我们使用systemctl的start,restart,stop和reload命令时,终端不会输出任何内容,只有status命令可以打印输出。

7. 如何激活服务并在开机时启用或禁用服务(即系统启动时自动启动mysql.service服务)

1

2

3

# systemctl is-active mysql.service

# systemctl enable mysql.service

# systemctl disable mysql.service

8. 如何屏蔽(让它不能启动)或显示服务(如ntpdate.service)

1

2

3

4

# systemctl mask ntpdate.service

ln -s ‘/dev/null/etc/systemd/system/ntpdate.service'

# systemctl unmask ntpdate.service

rm /etc/systemd/system/ntpdate.service'

9. 使用systemctl命令杀死服务

1

# systemctl kill crond

10. 列出所有系统挂载点

1

# systemctl list-unit-files –type=mount

11. 挂载、卸载、重新挂载、重载系统挂载点并检查系统中挂载点状态

1

2

3

4

5

# systemctl start tmp.mount

# systemctl stop tmp.mount

# systemctl restart tmp.mount

# systemctl reload tmp.mount

# systemctl status tmp.mount

12. 在启动时激活、启用或禁用挂载点(系统启动时自动挂载)

1

2

3

# systemctl is-active tmp.mount

# systemctl enable tmp.mount

# systemctl disable tmp.mount

13. 在Linux中屏蔽(让它不能启用)或可见挂载点

1

2

3

4

# systemctl mask tmp.mount

ln -s ‘/dev/null/etc/systemd/system/tmp.mount'

# systemctl unmask tmp.mount

rm /etc/systemd/system/tmp.mount'

14. 列出所有可用系统套接口

1

# systemctl list-unit-files –type=socket

15. 检查某个服务的所有配置细节

1

# systemctl show mysql

16. 获取某个服务(httpd)的依赖性列表

1

# systemctl list-dependencies httpd.service

17. 启动救援模式

1

# systemctl rescue

18. 进入紧急模式

1

# systemctl emergency

19. 列出当前使用的运行等级

1

# systemctl get-default

20. 启动运行等级5,即图形模式

1

# systemctl isolate runlevel5.target

1

# systemctl isolate graphical.target

21. 启动运行等级3,即多用户模式(命令行)

 

1

# systemctl isolate runlevel3.target

1

# systemctl isolate multiuser.target

22. 设置多用户模式或图形模式为默认运行等级

1

2

# systemctl set-default runlevel3.target

# systemctl set-default runlevel5.target

23. 重启、停止、挂起、休眠系统或使系统进入混合睡眠

1

2

3

4

5

# systemctl reboot

# systemctl halt

# systemctl suspend

# systemctl hibernate

# systemctl hybrid-sleep

对于不知运行等级为何物的人,说明如下。
Runlevel 0 : 关闭系统
Runlevel 1 : 救援,维护模式
Runlevel 3 : 多用户,无图形系统
Runlevel 4 : 多用户,无图形系统
Runlevel 5 : 多用户,图形化系统
Runlevel 6 : 关闭并重启机器

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值