控制服務和守護進程

控制服務和守護進程

1.centos6

命令說明 輸入命令
啓動服務 service httpd start
關閉服務 service httpd stop
重啓服務 service httpd restart
查看服務狀態 service httpd status
設置服務開機啓動 chkconfig httpd on
設置服務開機關閉 chkconfig httpd off
查看服務開機啓動 chkconfig –list systemctl l

例子

[root@centos6 ~]# service httpd start
[root@centos6 ~]# service httpd stop
[root@centos6 ~]# service httpd restart
[root@centos6 ~]# 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.
[root@centos6 ~]# chkconfig httpd off
注意:正在將請求轉發到“systemctl disable httpds.ervice”。
Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service
[root@centos6 ~]# 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
[root@centos7 ~]#systemctl start httpd.service
[root@centos7 ~]#systemctl stop httpd.service
[root@centos7 ~]#systemctl restart httpd.service
[root@centos7 ~]#systemctl status httpd.service
[root@centos7 ~]# systemctl enable httpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@centos7 ~]# systemctl disable httpd.service
Removed symlink /etc/systemd/system/multi-user.target.wants/httpd.service.
[root@centos7 ~]# 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'.
[root@centos7 ~]# 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
[root@centos7 ~]# systemctl is-active httpd.service
unknown
[root@centos7 ~]# systemctl  enable httpd.service
Created symlink from /etc/systemd/system/multi-user.target.wants/httpd.service to /usr/lib/systemd/system/httpd.service.
[root@centos7 ~]# 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  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章