天天看点

控制服务和守护进程

控制服务和守护进程

文章目录

    • 控制服务和守护进程
    • 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  
           

继续阅读