使用systemctl管理服务(nginx)
首先调整好路径信息,修改配置文件
vim /usr/lib/systemd/system/nginx.service
[Unit]
Description=The nginx HTTP and reverse proxy server
After=syslog.target network.target remote-fs.target nss-lookup.target
[Service]
Type=forking
PIDFile=/var/run/nginx.pid
ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf
ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf
ExecReload=/usr/sbin/nginx -s reload
ExecStop=/usr/sbin/nginx -s stop
ExecQuit=/usr/sbin/nginx -s quit
PrivateTmp=true
[Install]
WantedBy=multi-user.target
由于进程已经存在,编辑好之后,执行启动命令是失败的。
[root@mcw1 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Fri 2021-11-12 09:15:54 CST; 51s ago
Process: 2378 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=1/FAILURE)
Process: 2376 ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Nov 12 09:15:53 mcw1 nginx[2378]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Nov 12 09:15:53 mcw1 nginx[2378]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Nov 12 09:15:53 mcw1 nginx[2378]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Nov 12 09:15:54 mcw1 nginx[2378]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Nov 12 09:15:54 mcw1 nginx[2378]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Nov 12 09:15:54 mcw1 systemd[1]: nginx.service: control process exited, code=exited status=1
Nov 12 09:15:54 mcw1 nginx[2378]: nginx: [emerg] still could not bind()
Nov 12 09:15:54 mcw1 systemd[1]: Failed to start The nginx HTTP and reverse proxy server.
Nov 12 09:15:54 mcw1 systemd[1]: Unit nginx.service entered failed state.
Nov 12 09:15:54 mcw1 systemd[1]: nginx.service failed.
手动执行检查命令,发现是被占用的,也就是由于nginx已经是启动状态,无论怎么弄都不行
[root@mcw1 ~]# /usr/sbin/nginx -c /etc/nginx/nginx.conf
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
把nginx服务停掉
[root@mcw1 ~]# ps -ef|grep nginx
root 1925 1 0 08:33 ? 00:00:00 nginx: master process nginx
nginx 2220 1925 0 09:10 ? 00:00:00 nginx: worker process
root 2411 1829 0 09:19 pts/0 00:00:00 grep --color=auto nginx
[root@mcw1 ~]# kill 1925
[root@mcw1 ~]# ps -ef|grep nginx
root 2413 1829 0 09:19 pts/0 00:00:00 grep --color=auto nginx
[root@mcw1 ~]#
用systemctl启动nginx
[root@mcw1 ~]# systemctl start nginx
这时查看进程是没有问题的
[root@mcw1 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2021-11-12 09:20:03 CST; 3s ago
Process: 2511 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Process: 2510 ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 2514 (nginx)
CGroup: /system.slice/nginx.service
├─2514 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─2515 nginx: worker process
Nov 12 09:20:03 mcw1 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Nov 12 09:20:03 mcw1 nginx[2510]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Nov 12 09:20:03 mcw1 nginx[2510]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Nov 12 09:20:03 mcw1 systemd[1]: Started The nginx HTTP and reverse proxy server.
用systemctl停掉nginx,也米有问题
[root@mcw1 ~]# systemctl stop nginx
[root@mcw1 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: inactive (dead)
Nov 12 09:17:01 mcw1 systemd[1]: nginx.service failed.
Nov 12 09:20:03 mcw1 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Nov 12 09:20:03 mcw1 nginx[2510]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Nov 12 09:20:03 mcw1 nginx[2510]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Nov 12 09:20:03 mcw1 systemd[1]: Started The nginx HTTP and reverse proxy server.
Nov 12 09:20:23 mcw1 systemd[1]: Stopping The nginx HTTP and reverse proxy server...
Nov 12 09:20:23 mcw1 systemd[1]: Stopped The nginx HTTP and reverse proxy server.
Nov 12 09:20:23 mcw1 systemd[1]: [/usr/lib/systemd/system/nginx.service:12] Unknown lvalue 'ExecQuit' in section 'Service'
Nov 12 09:20:23 mcw1 systemd[1]: [/usr/lib/systemd/system/nginx.service:12] Unknown lvalue 'ExecQuit' in section 'Service'
Nov 12 09:20:26 mcw1 systemd[1]: [/usr/lib/systemd/system/nginx.service:12] Unknown lvalue 'ExecQuit' in section 'Service'
用systemctl启动和重启nginx,都米有问题
[root@mcw1 ~]# systemctl start nginx
[root@mcw1 ~]# systemctl restart nginx
[root@mcw1 ~]# systemctl status nginx
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: active (running) since Fri 2021-11-12 09:20:38 CST; 4s ago
Process: 2548 ExecStop=/usr/sbin/nginx -s stop (code=exited, status=0/SUCCESS)
Process: 2554 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Process: 2553 ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Main PID: 2556 (nginx)
CGroup: /system.slice/nginx.service
├─2556 nginx: master process /usr/sbin/nginx -c /etc/nginx/nginx.conf
└─2557 nginx: worker process
Nov 12 09:20:38 mcw1 systemd[1]: Starting The nginx HTTP and reverse proxy server...
Nov 12 09:20:38 mcw1 nginx[2553]: nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
Nov 12 09:20:38 mcw1 nginx[2553]: nginx: configuration file /etc/nginx/nginx.conf test is successful
Nov 12 09:20:38 mcw1 systemd[1]: Started The nginx HTTP and reverse proxy server.
[root@mcw1 ~]#
用systemctl停掉nginx。然后直接用命令启动nginx。再使用systemctl重启管理,这下子就不行了。
[root@mcw1 ~]# systemctl stop nginx.service
[root@mcw1 ~]# nginx
[root@mcw1 ~]# systemctl restart nginx.service
Job for nginx.service failed because the control process exited with error code. See "systemctl status nginx.service" and "journalctl -xe" for details.
[root@mcw1 ~]#
总结:也就是如果用systemtl管理的话,后面也用systemctl就行,直接用命令启停服务,也可以。但是两者交叉管理服务,很有可能出问题
根据报错提示查看错误详情。显示有地址已经在使用的错误信息,说明服务已经启动了,但是命令直接启动的,没有用systemctl启动,所以后面的查看,启动,重启,等操作用systemctl会出错。
[root@mcw1 ~]# systemctl status nginx.service
● nginx.service - The nginx HTTP and reverse proxy server
Loaded: loaded (/usr/lib/systemd/system/nginx.service; disabled; vendor preset: disabled)
Active: failed (Result: exit-code) since Fri 2021-11-12 09:28:14 CST; 3min 1s ago
Process: 2712 ExecStart=/usr/sbin/nginx -c /etc/nginx/nginx.conf (code=exited, status=1/FAILURE)
Process: 2711 ExecStartPre=/usr/sbin/nginx -t -c /etc/nginx/nginx.conf (code=exited, status=0/SUCCESS)
Nov 12 09:28:13 mcw1 nginx[2712]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Nov 12 09:28:13 mcw1 nginx[2712]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Nov 12 09:28:13 mcw1 nginx[2712]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Nov 12 09:28:14 mcw1 nginx[2712]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Nov 12 09:28:14 mcw1 nginx[2712]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Nov 12 09:28:14 mcw1 systemd[1]: nginx.service: control process exited, code=exited status=1
Nov 12 09:28:14 mcw1 nginx[2712]: nginx: [emerg] still could not bind()
Nov 12 09:28:14 mcw1 systemd[1]: Failed to start The nginx HTTP and reverse proxy server.
Nov 12 09:28:14 mcw1 systemd[1]: Unit nginx.service entered failed state.
Nov 12 09:28:14 mcw1 systemd[1]: nginx.service failed.
[root@mcw1 ~]# journalctl -xe
Nov 12 09:28:14 mcw1 nginx[2712]: nginx: [emerg] bind() to 0.0.0.0:80 failed (98: Address already in use)
Nov 12 09:28:14 mcw1 nginx[2712]: nginx: [emerg] bind() to [::]:80 failed (98: Address already in use)
Nov 12 09:28:14 mcw1 polkitd[902]: Unregistered Authentication Agent for unix-process:2705:524584 (system bus name :1.122, object path /org/freedesktop/PolicyK
Nov 12 09:28:14 mcw1 systemd[1]: nginx.service: control process exited, code=exited status=1
Nov 12 09:28:14 mcw1 nginx[2712]: nginx: [emerg] still could not bind()
Nov 12 09:28:14 mcw1 systemd[1]: Failed to start The nginx HTTP and reverse proxy server.
-- Subject: Unit nginx.service has failed
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit nginx.service has failed.
--
-- The result is failed.
Nov 12 09:28:14 mcw1 systemd[1]: Unit nginx.service entered failed state.
Nov 12 09:28:14 mcw1 systemd[1]: nginx.service failed.
Nov 12 09:30:01 mcw1 systemd[1]: Started Session 23 of user root.
-- Subject: Unit session-23.scope has finished start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit session-23.scope has finished starting up.
--
-- The start-up result is done.
Nov 12 09:30:01 mcw1 systemd[1]: Starting Session 23 of user root.
-- Subject: Unit session-23.scope has begun start-up
-- Defined-By: systemd
-- Support: http://lists.freedesktop.org/mailman/listinfo/systemd-devel
--
-- Unit session-23.scope has begun starting up.
Nov 12 09:30:01 mcw1 CROND[2718]: (root) CMD (/usr/sbin/ntpdate time.nist.gov >/dev/null 2>&1)
使用systemctl管理服务(nginx)的更多相关文章
- 二进制编译安装nginx并加入systemctl管理服务
一.安装nginx所需环境 # yum install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel -y 二.安装ngi ...
- Linux_CentOS 中systemctl 管理服务、防火墙 firewalld 以及 SELinux 配置
使用 systemctl 管理服务 systemctl 就是 service 和 chkconfig 这两个命令的整合,在 CentOS 7 就开始被使用了,systemctl是系统服务管理器命令,它 ...
- Linux学习-透过 systemctl 管理服务
透过 systemctl 管理单一服务 (service unit) 的启动/开机启动与观察状态 一般来说,服务的启动有两个阶段,一 个是『开机的时候设定要不要启动这个服务』, 以及『你现在要不要启动 ...
- RHEL7使用systemctl管理服务
1. systemctl命令 说明 systemctl start [unit type] 启动服务 systemctl stop [unit type] 停止服务 syste ...
- 使用systemctl管理服务
系统服务,开机不需要登录就能运行的程序(相当于开机自启)/usr/lib/systemd/system 用户服务,需要登录后才能运行的程序/usr/lib/systemd/user 目录下又存在两种类 ...
- 使用 Centos 7 的 systemctl 管理服务
CentOS 7的服务systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户(user)之分,像需要开机不登陆就能运行的程序,存系统服务里即:/usr/lib/ ...
- CentOS7下普通账号通过systemctl管理服务需要输入root密码问题
问题描述: 使用普通账号test通过systemctl启动系统服务提示需要输入root密码: 解决方案: 根据上面提示得知权限由polkit进行管理,对应的是org.freedesktop.syste ...
- CentOS7 使用systemctl来管理服务
1.建立service文件 以tomcat为例 , 建立tomcat.service文件 #服务说明 [Unit] #服务描述 Description=Tomcat Service #前置需要启动的服 ...
- 手动编译安装Libvirt之后利用systemctl管理libvirtd服务
因为要给特殊的虚拟机关联文件指定selinux标签,而默认的Libvirt没有这个功能,所以需要修改LIbvirt源代码,重新编译安装Libvirt,而手动编译安装的LIbvirt,没有办法使用sys ...
- 透过systemctl管理mysqld服务
1. 背景 CentOS 7.x 之前的版本,系统启动时,第一支呼叫的程序是 init ,然后 init 去唤起所有的系统所需要的服务,无论是本地服务还是网络服务.所有的服务启动脚本都放置于 /etc ...
随机推荐
- 【开源三方库】Aki:一行代码极简体验JS&C++跨语言交互
开源项目 OpenHarmony 是每个人的 OpenHarmony 一.简介 OpenAtom OpenHarmony(以下简称"OpenHarmony")的前端开发语言是A ...
- K8s技术全景:架构、应用与优化
本文深入探讨了Kubernetes(K8s)的关键方面,包括其架构.容器编排.网络与存储管理.安全与合规.高可用性.灾难恢复以及监控与日志系统. 关注[TechLeadCloud],分享互联网架构.云 ...
- 初学STM32 SDIO (二)
SD命令由主机发出,以广播命令和寻址命令为例,广播命令是针对与SD主机总线连接的所有从设备发送的,寻址命令是指定某个地址设备进行命令传输. 1.SD命令格式 SD命令格式固定为48bit,都是通过 ...
- Prometheus AlertManager 生产实践-直接根据 to_email label 发 alert 到对应邮箱
概述 通过之前的文章 - Prometheus Alertmanager 生产配置趟过的坑总结, 我们已经知道 AlertManager 作为告警平台,是非常强大的,可以去重 (deduplicati ...
- 家庭实验室系列文章-如何迁移树莓派系统到更大的 SD 卡?
前言 其实这个专题很久很久之前就想写了,但是一直因为各种原因拖着没动笔. 因为没有资格,也没有钱在一线城市买房 (); 但是在要结婚之前,婚房又是刚需. 我和太太最终一起在一线城市周边的某二线城市买了 ...
- 如何巧妙应对iOS键盘难题?
前言 写过移动端的同学或多或少都遇到过软键盘带来的各种各样的问题,最典型的就是输入框被软键盘遮挡.fixed元素失效等问题,并且这些问题在iOS上的表现让人难以接受. webview的差异 在移动端上 ...
- szfpga 高云gowin国产开发板GW2AR-18核心板fpga cpld测试板
1. 概述 国产FPGA是最近几年起来的产品,具有性价比高特点.而GOWIN属于国产FPGA成员,在服务和芯片都是比较大的优势,很多用户都用在LED控制,电机控制,PLC设备上,以及用于替换Latti ...
- 力扣389(java)-找不同(简单)
题目: 给定两个字符串 s 和 t ,它们只包含小写字母. 字符串 t 由字符串 s 随机重排,然后在随机位置添加一个字母. 请找出在 t 中被添加的字母. 示例 1: 输入:s = "ab ...
- 疫情带火了这款APP:2600个学生一天点赞70万次
这几天,全国中小学生经历了"过山车"一样的心情. 因为疫情的不断蔓延,1月27日,教育部下发通知,2020年春季学期延期开学. 随后,教育部又提出"利用网络平台,停课 ...
- 一个好的科技公司logo长这样!
简介:一个好的科技logo能体现出行业独有的专业性和技术优势,让你的公司科技感加满! 近年来,越来越多的初创公司崭露头角,其中科技互联网公司的比重非常高.小云也收到很多朋友的留言,询问科技类公司应该 ...