【Linux】④软件服务的管理命令

改动时间2020-3-12 21:15:17

自己常用

1
2
3
4
5
6
7
# 软链接【建立软链接到环境变量路径中,查看环境变量`echo $PATH`。注意:追加到root下,普通用户是访问不了的】
ln -s /app/nginx/sbin/nginx /usr/bin

# 开机自启动
echo /app/nginx/sbin/nginx >> /etc/rc.local
# 要想上一步生效,还要赋执行权限,使脚本可以运行
chmod +x /etc/rc.local

服务的分类

  • Linux服务
    1
    2
    3
    4
    1. RPM包默认安装的服务
    + 独立的服务
    + 基于xinetd服务
    2. 源码包安装的服务
  • RPM包与源码包的安装服务的区别就是安装位置不同
    1
    2
    3
    4
    5
    6
    7
    8
    9
    * 源码包一般在`/usr/local/`
    * RPM包安装在默认位置
    + `/etc/init.d/`
    + `/etc/sysconfig/`初始化环境配置文件位置
    + `/etc/`配置文件位置
    + `/etc/xinetd.conf`xinetd配置文件
    + `/etc/xinetd.d/`基于xinetd服务的启动脚本
    + `/var/lib/`服务产生的数据放在这里
    + `/var/log/`日志

init.d和rc.local

  • rc.local
    /etc/rc.d/rc.local用于添加开机启动命令,/etc/rc.local/etc/rc.d/rc.local的软连接
    在这里插入图片描述
  • service命令其实是去执行/etc/init.d目录下的程序。/etc/init.d/etc/rc.d/init.d的硬连接
    在这里插入图片描述
    在这里插入图片描述

service

1
2
3
4
5
6
7
8
# 把服务添加到service服务中
ln -s /app/nginx/sbin/nginx /etc/init.d/

# 使用service来控制nginx启动,停止,重启。
# 跟把服务软连接到PATH路径中同理,但命令前面多了一个service进行控制。
service nginx
service nginx -s stop
service nginx -s reload

systemctl

systemctl命令兼容了service。意思就是systemctl也会去/etc/init.d目录下,查看,执行相关程序
我认为:就不用去了解systemctl了。使用service就行了。

systemd是Linux系统最新的初始化系统(init),作用是提高系统的启动速度,尽可能启动较少的进程,尽可能更多进程并发启动。
systemd对应的进程管理命令是systemctl,位置是/etc/systemd

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
1. 列出所有可用单元 
# systemctl list-unit-files
查看开启自启动的服务,disabled查看禁用自启动的服务
# systemctl list-unit-files | grep enabled

2. 列出所有运行中单元
# systemctl list-units

3. 列出所有失败单元
# systemctl –failed

4. 检查某个单元(如 crond.service)是否启用
# systemctl is-enabled crond.service

5. 列出所有服务
# systemctl list-unit-files –type=service

6. Linux中如何启动、重启、停止、重载服务以及检查服务(如 httpd.service)状态
# systemctl status httpd.service
# systemctl start httpd.service
# systemctl stop httpd.service
# systemctl restart httpd.service
# systemctl reload httpd.service
注意:当我们使用systemctl的start,restart,stop和reload命令时,终端不会输出任何内容,只有status命令可以打印输出。

7. 如何激活服务并在开机时启用或禁用服务(即系统启动时自动启动mysql.service服务)
# systemctl is-active mysql.service
# systemctl enable mysql.service
# systemctl disable mysql.service

8. 如何屏蔽(让它不能启动)或显示服务(如ntpdate.service)
# systemctl mask ntpdate.service
ln -s /etc/systemd/system/ntpdate.service

# systemctl unmask ntpdate.service
rm /etc/systemd/system/ntpdate.service

9. 使用systemctl命令杀死服务
# systemctl killcrond

10. 列出所有系统挂载点
# systemctl list-unit-files –type=mount

11. 挂载、卸载、重新挂载、重载系统挂载点并检查系统中挂载点状态
# systemctl start tmp.mount
# systemctl stop tmp.mount
# systemctl restart tmp.mount
# systemctl reload tmp.mount
# systemctl status tmp.mount

12. 在启动时激活、启用或禁用挂载点(系统启动时自动挂载)
# systemctl is-active tmp.mount
# systemctl enable tmp.mount
# systemctl disable tmp.mount

13. 在Linux中屏蔽(让它不能启用)或可见挂载点
# systemctl mask tmp.mount
ln -s /etc/systemd/system/tmp.mount

# systemctl unmask tmp.mount
rm /etc/systemd/system/tmp.mount

14. 列出所有可用系统套接口
# systemctl list-unit-files –type=socket

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

16. 获取某个服务(httpd)的依赖性列表
# systemctl list-dependencies httpd.service

chkconfig

设置开机启动

1
2
3
[root@localhost ~]$ ls /etc/init.d/httpd     # /etc/init.d/目录下必须有启动脚本
[root@localhost ~]$ chkconfig --add httpd # 添加服务,以便让chkconfig指令管理它
[root@localhost ~]$ chkconfig httpd on # 设置开机运行该服务,默认是设置2345等级开机运行服务
1
2
3
4
5
6
[root@localhost ~]$ chkconfig --list                 # 列出所有被chkconfig管理的服务
[root@localhost ~]$ chkconfig --add httpd # 添加指定的服务,让chkconfig指令管理它
[root@localhost ~]$ chkconfig --del httpd # 删除指定的服务,不再让chkconfig指令管理它
[root@localhost ~]$ chkconfig httpd on # 设置开机运行服务,需要先执行 --add 才能执行该命令
[root@localhost ~]$ chkconfig httpd off # 设置开机不运行服务,需要先执行 --add 才能执行该命令
[root@localhost ~]$ chkconfig --level 35 httpd on # 设置服务在等级3和5时开机运行服务,默认是设置2345等级开机运行服务
1
2
3
4
5
6
7
[root@localhost ~]$ chkconfig --list                                      # 等级0:关机
atop 0:off 1:off 2:off 3:off 4:off 5:off 6:off # 等级1:单用户模式/救援模式
auditd 0:off 1:off 2:off 3:off 4:on 5:off 6:off # 等级2:无网络连接的多用户命令行模式
crond 0:off 1:off 2:on 3:on 4:on 5:on 6:off # 等级3:有网络连接的多用户命令行模式
ipset 0:off 1:off 2:on 3:on 4:on 5:on 6:off # 等级4:不可用
iptables 0:off 1:off 2:off 3:off 4:on 5:off 6:off # 等级5:带图形界面的多用户模式
mysql 0:off 1:off 2:on 3:on 4:on 5:on 6:off # 等级6:重启