脚本:
#!/bin/bash
systemctl status httpd.service |grep "run">/dev/null # 查看apache服务的状态,不打印
if [ $? -eq 0 ];then
#如果上面命令执行成功 返回 0
echo "apache正在运行"
else
#否则,返回值为非0,则重新启动服务,返回结果不打印
echo "apache已关闭,正在启动" ...
systemctl start httpd.service >/dev/null 2> /dev/null
echo "apache服务器已重起:"
fi
~
验证
重启服务:
[root@node143 ~]# systemctl restart httpd.service
查看状态:
[root@node143 ~]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since 五 2021-02-26 23:31:50 CST; 4s ago
Docs: man:httpd(8)
man:apachectl(8)
Process: 103914 ExecStop=/bin/kill -WINCH ${MAINPID} (code=exited, status=0/SUCCESS)
Main PID: 103936 (httpd)
Status: "Processing requests..."
CGroup: /system.slice/httpd.service
├─103936 /usr/sbin/httpd -DFOREGROUND
├─103937 /usr/sbin/httpd -DFOREGROUND
├─103938 /usr/sbin/httpd -DFOREGROUND
├─103939 /usr/sbin/httpd -DFOREGROUND
├─103940 /usr/sbin/httpd -DFOREGROUND
└─103941 /usr/sbin/httpd -DFOREGROUND
2月 26 23:31:50 node143 systemd[1]: Starting The Apache HTTP Server...
2月 26 23:31:50 node143 httpd[103936]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name,...message
2月 26 23:31:50 node143 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.
启动脚本,启动脚本之前记得给执行权限
[root@node143 ~]# ./apapche.sh
httpd is running
关闭服务:
[root@node143 ~]# systemctl stop httpd.service
直接启动脚本验证:
[root@node143 ~]# ./apapche.sh
httpd is down,and starting....
httpd is running.
查看状态是否为running,是的话说明脚本有效
[root@node143 ~]# systemctl status httpd.service
● httpd.service - The Apache HTTP Server
Loaded: loaded (/usr/lib/systemd/system/httpd.service; disabled; vendor preset: disabled)
Active: active (running) since 五 2021-02-26 23:34:19 CST; 8s ago
Docs: man:httpd(8)
man:apachectl(8)
Main PID: 106903 (httpd)
Status: "Processing requests..."
CGroup: /system.slice/httpd.service
├─106903 /usr/sbin/httpd -DFOREGROUND
├─106904 /usr/sbin/httpd -DFOREGROUND
├─106905 /usr/sbin/httpd -DFOREGROUND
├─106906 /usr/sbin/httpd -DFOREGROUND
├─106907 /usr/sbin/httpd -DFOREGROUND
└─106908 /usr/sbin/httpd -DFOREGROUND
2月 26 23:34:19 node143 systemd[1]: Starting The Apache HTTP Server...
2月 26 23:34:19 node143 httpd[106903]: AH00558: httpd: Could not reliably determine the server's fully qualified domain name,...message
2月 26 23:34:19 node143 systemd[1]: Started The Apache HTTP Server.
Hint: Some lines were ellipsized, use -l to show in full.