天天看點

控制服務和守護程序

控制服務和守護程序

文章目錄

    • 控制服務和守護程序
    • 1.centos6
    • 2.centos7
    • 3.centos6和centos7服務指令對比
    • 4.用三種腳本監控服務

1.centos6

指令說明 輸入指令
啟動服務 service httpd start
關閉服務 service httpd stop
重新開機服務 service httpd restart
檢視服務狀态 service httpd status
設定服務開機啟動 chkconfig httpd on
設定服務開機關閉 chkconfig httpd off
檢視服務開機啟動 chkconfig –list systemctl l

例子

[[email protected] ~]# service httpd start
[[email protected] ~]# service httpd stop
[[email protected] ~]# service httpd restart
[[email protected] ~]# chkconfig httpd on
注意:正在将請求轉發到“systemctl enable vsftpd.service”。
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[[email protected] ~]# chkconfig httpd off
注意:正在将請求轉發到“systemctl disable httpds.ervice”。
Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service
[[email protected] ~]# service httpd status
Redirecting to /bin/systemctl status httpd.service
● vsftpd.service - httpd daemon
   Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
   Active: active (running) since 一 2018-11-19 10:32:32 CST; 3h 35min ago
 Main PID: 3158 (vsftpd)
   CGroup: /system.slice/httpd.service
           └─3158 /usr/sbin/httpd/etc/vsftpd/httpd.conf

11月 19 10:32:32 localhost.localdomain systemd[1]: Starting httpd ftp daemon...
11月 19 10:32:32 localhost.localdomain systemd[1]: Started httpd ftp daemon.
           

2.centos7

指令說明 輸入指令
啟動服務 systemctl start httpd.service
關閉服務 systemctl stop httpd.service
重新開機服務 systemctl restart httpd.service
檢視服務狀态 systemctl status httpd.service
設定服務開機啟動 systemctl enable httpd.service
設定服務開機關閉 systemctl disable httpd.service
檢視服務開機啟動 systemctl list-units –type=service
重載或重新開機服務 reload-or-restart name.service
重載或條件式重新開機服務 reload-or-try-restart name.service
禁止設定為開機自啟 mask name.service
取消禁止設定為開機自啟 umask name.service
檢視服務依賴關系 systemctl list-dependencies name.service
檢視服務是否激活與否 systemctl is-active httpd.service
檢視開機是否啟動 systemctl enable httpd.service
禁止服務開機自動啟動 systemctl disable httpd.service
設定某服務開機自動啟動 systemctl enable name.service
禁止服務開機自動啟動 systemctl isolate name.target
檢視所有服務的開機自動啟動狀态(是否開機自啟) systemctl list-units --type service
檢視所有的級别 systemctl list -units --type target --all
檢視預設運作級别 get-default
設定預設運作級别 set-default name.target
切換至emergency模式(驅動不會加載,系統不會初始化,服務不會啟動) emergency
關機 halt
關機 poweroff
重新開機 reboot
挂起系統,此時不能關機,否則無用 suspend
建立并儲存系統快照,下次系統重新開機時會自動載入快照 hibernate
混合睡眠,快照并挂起 hybrid-sleep
[[email protected] ~]#systemctl start httpd.service
[[email protected] ~]#systemctl stop httpd.service
[[email protected] ~]#systemctl restart httpd.service
[[email protected] ~]#systemctl status httpd.service
[[email protected] ~]# systemctl enable httpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[[email protected] ~]# systemctl disable httpd.service
Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.
[[email protected] ~]# systemctl list-units –type=service
0 loaded units listed. Pass --all to see loaded but inactive units, too.
To show all installed unit files use 'systemctl list-unit-files'.
[[email protected] ~]# systemctl list-dependencies httpd.service
httpd.service
● ├─-.mount
● ├─system.slice
● └─basic.target
●   ├─microcode.service
●   ├─rhel-dmesg.service
●   ├─[email protected]
●   ├─paths.target
●   ├─slices.target
●   │ ├─-.slice
●   │ └─system.slice
●   ├─sockets.target
●   │ ├─dbus.socket
●   │ ├─dm-event.socket
●   │ ├─systemd-initctl.socket
●   │ ├─systemd-journald.socket
●   │ ├─systemd-shutdownd.socket
●   │ ├─systemd-udevd-control.socket
●   │ └─systemd-udevd-kernel.socket
●   ├─sysinit.target
●   │ ├─dev-hugepages.mount
●   │ ├─dev-mqueue.mount
[[email protected] ~]# systemctl is-active httpd.service
unknown
[[email protected] ~]# systemctl  enable httpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[[email protected] ~]# systemctl disable httpd.service 
Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.
           

3.centos6和centos7服務指令對比

指令名稱 centos 6 centos7
啟動服務 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 off systemctl disable httpd.service
檢視服務開機啟動 chkconfig –list systemctl systemctl list-units –type=service

4.用三種腳本監控服務

舉例

監控端口注意引用指令需要用$()把指令放在括号中

例如監控21vsftpd端口

關于端口監控的腳本方法
第一種方法
#!/bin/bash
a=$(netstat -lntup|grep vsftpd|awk -F ':' '{print $4}')
if [ $a -eq 21 ]
 then
    echo 'vsftpd啟動了'
 else [ $a -ge 21 ]
    echo 'vsftpd停止運作'
fi
第二種方法
#!/bin/bash
a=$(systemctl status vsftpd.service|awk 'NR==3 {print $3}')
if [ $a == '(running)' ]
 then       
    echo 'vsftpd正在運作'
 else [ $a == '(dead)' ]
    echo 'vsftpd停止運作'     
fi  
第三種方法
#!/bin/bash
a=$(systemctl status vsftpd.service|awk 'NR==3 {print $3}')
case $a in
\(running\))
echo 'vsftpd正在運作'
;;          
\(dead\))   
echo 'vsftp已停止運作'
;;
esac  
           

繼續閱讀