控制服务和守护进程

控制服务和守护进程

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
●   ├─selinux-policy-migrate-local-changes@targeted.service
●   ├─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 6centos7
启动服务service httpd startsystemctl start httpd.service
关闭服务service httpd stopsystemctl stop httpd.service
重启服务service httpd restartsystemctl restart httpd.service
查看服务状态service httpd statussystemctl status httpd.service
设置服务开机启动chkconfig httpd onsystemctl enable httpd.service
设置服务开机关闭chkconfig httpd offsystemctl disable httpd.service
查看服务开机启动chkconfig –list systemctlsystemctl 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  
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值