我們對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