首先调整好路径信息,修改配置文件
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)的更多相关文章

  1. 二进制编译安装nginx并加入systemctl管理服务

    一.安装nginx所需环境 # yum install gcc-c++ pcre pcre-devel zlib zlib-devel openssl openssl-devel -y 二.安装ngi ...

  2. Linux_CentOS 中systemctl 管理服务、防火墙 firewalld 以及 SELinux 配置

    使用 systemctl 管理服务 systemctl 就是 service 和 chkconfig 这两个命令的整合,在 CentOS 7 就开始被使用了,systemctl是系统服务管理器命令,它 ...

  3. Linux学习-透过 systemctl 管理服务

    透过 systemctl 管理单一服务 (service unit) 的启动/开机启动与观察状态 一般来说,服务的启动有两个阶段,一 个是『开机的时候设定要不要启动这个服务』, 以及『你现在要不要启动 ...

  4. RHEL7使用systemctl管理服务

    1.   systemctl命令 说明 systemctl    start   [unit type] 启动服务 systemctl    stop   [unit type] 停止服务 syste ...

  5. 使用systemctl管理服务

    系统服务,开机不需要登录就能运行的程序(相当于开机自启)/usr/lib/systemd/system 用户服务,需要登录后才能运行的程序/usr/lib/systemd/user 目录下又存在两种类 ...

  6. 使用 Centos 7 的 systemctl 管理服务

    CentOS 7的服务systemctl脚本存放在:/usr/lib/systemd/,有系统(system)和用户(user)之分,像需要开机不登陆就能运行的程序,存系统服务里即:/usr/lib/ ...

  7. CentOS7下普通账号通过systemctl管理服务需要输入root密码问题

    问题描述: 使用普通账号test通过systemctl启动系统服务提示需要输入root密码: 解决方案: 根据上面提示得知权限由polkit进行管理,对应的是org.freedesktop.syste ...

  8. CentOS7 使用systemctl来管理服务

    1.建立service文件 以tomcat为例 , 建立tomcat.service文件 #服务说明 [Unit] #服务描述 Description=Tomcat Service #前置需要启动的服 ...

  9. 手动编译安装Libvirt之后利用systemctl管理libvirtd服务

    因为要给特殊的虚拟机关联文件指定selinux标签,而默认的Libvirt没有这个功能,所以需要修改LIbvirt源代码,重新编译安装Libvirt,而手动编译安装的LIbvirt,没有办法使用sys ...

  10. 透过systemctl管理mysqld服务

    1. 背景 CentOS 7.x 之前的版本,系统启动时,第一支呼叫的程序是 init ,然后 init 去唤起所有的系统所需要的服务,无论是本地服务还是网络服务.所有的服务启动脚本都放置于 /etc ...

随机推荐

  1. C语言 03 VSCode开发

    安装好 C 语言的开发环境后,就需要创建项目进行开发了. 使用 IDE(集成开发环境)进行开发了. C 语言的开发工具很多,现在主流的有 Clion.Visual Studio.VSCode. 这里以 ...

  2. Python队列----queue

    import queue # 官网文档:https://docs.python.org/3/library/queue.html a1 = queue.Queue() # 先进先出队列 a2 = qu ...

  3. redis 简单整理——复制配置[二十二]

    前言 在分布式系统中为了解决单点问题,通常会把数据复制多个副本部署到 其他机器,满足故障恢复和负载均衡等需求.Redis也是如此,它为我们提 供了复制功能,实现了相同数据的多个Redis副本.复制功能 ...

  4. docker 应用篇————tomcat例子[七]

    前言 虽然我干的事情和java不多,但是例子是为了熟悉原理,而不是为了例子而例子的,故而整理一下tomcat的例子. 正文 使用官方示例: 然后运行一下. 没有找到然后进行下载了. 可以看到这里就已经 ...

  5. Linux基础-01:Linux命令的基本格式

    2.1.1 命令提示符 在CentOS 7操作系统中,Linux命令提示符就像是你与电脑交流的一个小标志,告诉你系统已经准备好接受你的指令了. 它通常会显示在你打开的终端窗口或控制台的最前面. 让我们 ...

  6. (已解决)安装PyMySQL出现问题--'pip' 不是内部或外部命令,也不是可运行的程序 或批处理文件

    问题描述: 输入cmd,进入命令窗口,输入pip install pymysql时候出现下面的问题: 然后进入python环境中去输入还是报错: 问题原因:环境变量配置出错,cmd下无法调用pip程序 ...

  7. 力扣455(java&python)-分发饼干(简单)

    题目: 假设你是一位很棒的家长,想要给你的孩子们一些小饼干.但是,每个孩子最多只能给一块饼干. 对每个孩子 i,都有一个胃口值 g[i],这是能让孩子们满足胃口的饼干的最小尺寸:并且每块饼干 j,都有 ...

  8. 力扣8(java)-字符串转整数(atoi)(中等)

    题目: 请你来实现一个 myAtoi(string s) 函数,使其能将字符串转换成一个 32 位有符号整数(类似 C/C++ 中的 atoi 函数). 函数 myAtoi(string s) 的算法 ...

  9. 深入浅出eBPF|你要了解的7个核心问题

    简介: 过去一年,ARMS基于eBPF技术打造了Kubernetes监控,提供多语言无侵入的应用性能,系统性能,网络性能观测能力,验证了eBPF技术的有效性.eBPF技术和生态发展很好,未来前景广大, ...

  10. 阿里云EMAS旗下低代码平台Mobi开放定向内测

    ​简介:[低代码深度共创]EMAS旗下低代码平台Mobi开放定向内测名额,限时限量,参与调研先到先得! Mobi是面向全端(Web.Native App.H5.全平台小程序等)场景,模型驱动的低代码开 ...