CentOS 7 上systemctl 的用法

  我们对service和chkconfig两个命令都不陌生,systemctl 是管制服务的主要工具, 它整合了chkconfig 与 service功能于一体。

systemctl is-enabled iptables.service
systemctl is-enabled servicename.service #查询服务是否开机启动
systemctl enable *.service #开机运行服务
systemctl disable *.service #取消开机运行
systemctl start *.service #启动服务
systemctl stop *.service #停止服务
systemctl restart *.service #重启服务
systemctl reload *.service #重新加载服务配置文件
systemctl status *.service #查询服务运行状态
systemctl --failed #显示启动失败的服务

注:*代表某个服务的名字,如http的服务名为httpd

例如在CentOS 7 上安装http

[root@CentOS7 ~]# yum -y install httpd
启动服务(等同于service httpd start)
systemctl start httpd.service
停止服务(等同于service httpd stop)
systemctl stop httpd.service
重启服务(等同于service httpd restart)
systemctl restart httpd.service
查看服务是否运行(等同于service httpd status)
systemctl status httpd.service
开机自启动服务(等同于chkconfig httpd on)
systemctl enable httpd.service
开机时禁用服务(等同于chkconfig httpd on)
systemctl disable httpd.service
查看服务是否开机启动 (等同于chkconfig --list)

注意:

  原服务都安装在/usr/lib/systemd/system/目录下,当我们使用systemctl添加一项服务开机启动的时候,linux帮我们在/etc/systemd/system/multi-user.target.wants/目录下面创建了对应服务的软连接,所以开机启动,当我们设置服务开机不启动,软连接会从/etc/systemd/system/multi-user.target.wants/下面移除对应的软连接:

(1)查看/usr/lib/systemd/system/  系统原有的所有服务(有好多服务)

[root@VM_0_12_centos ~]# ls /usr/lib/systemd/system/
acpid.service
arp-ethers.service
atd.service
auditd.service
autovt@.service
basic.target
basic.target.wants
blk-availability.service
bluetooth.target
brandbot.path
brandbot.service
chrony-dnssrv@.service
chrony-dnssrv@.timer
..........

(2)查看mysqld服务并且到/etc/systemd/system/multi-user.target.wants/目录下查看:

[root@VM_0_12_centos ~]# ls /usr/lib/systemd/system |grep mysqld  #原服务目录下存在mysqld服务
mysqld.service
[root@VM_0_12_centos ~]# ls /etc/systemd/system/multi-user.target.wants/ |grep    
mysql                                       #开机启动目录下没有mysqld服务 
[root@VM_0_12_centos ~]# systemctl is-enabled mysqld.service      #mysqld开机不启动
disabled

(3)设置mysql服务开机启动并查看目录

[root@VM_0_12_centos ~]# systemctl enable mysqld.service  #设置mysql服务开机启动
Created symlink from /etc/systemd/system/mysql.service to /usr/lib/systemd/syste
m/mysqld.service.
Created symlink from /etc/systemd/system/multi-user.target.wants/mysqld.service
to /usr/lib/systemd/system/mysqld.service.
[root@VM_0_12_centos ~]# ll /etc/systemd/system/multi-user.target.wants/ |grep
mysql                                  #查看开机启动目录下mysqld.service的软连接
lrwxrwxrwx  1 root root 38 Apr  6 15:37 mysqld.service -> /usr/lib/systemd/syste
m/mysqld.service
[root@VM_0_12_centos ~]# ll /usr/lib/systemd/system |grep mysqld  #原服务目录下查看文件
-rw-r--r--  1 root root 1073 Dec  9 16:11 mysqld.service

一个正常的服务器最少需要的服务:

  httpd(apache http)

  iptables.service

  mysql.service

   nginx.service

原文地址:https://www.cnblogs.com/qlqwjy/p/8727900.html